即使使用 sudo,Yeoman 也会给出权限错误

Yeoman giving permission errors even with sudo

我正在关注 this tutorial 并安装了 Yeoman 和 Azure 的生成器。

所以,当我这样做时:sudo yo azuresfcontainer,它给了我以下错误:

/usr/local/lib/node_modules/yo/node_modules/fast-glob/out/providers/reader-sync.js:45
            throw err;
            ^

Error: EACCES: permission denied, scandir '/usr/lib/ssl/private'
    at Object.fs.readdirSync (fs.js:875:3)
    at exports.readdir (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/sync/fs.js:18:20)
    at Object.safeCall [as safe] (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/call.js:24:8)
    at DirectoryReader.readNextDirectory (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/directory-reader.js:78:10)
    at Readable.DirectoryReader.stream._read (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/directory-reader.js:57:18)
    at Readable.read (_stream_readable.js:455:10)
    at readdirSync (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/sync/index.js:27:21)
    at Function.readdirSyncStat (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/index.js:34:10)
    at ReaderSync.dynamicApi (/usr/local/lib/node_modules/yo/node_modules/fast-glob/out/providers/reader-sync.js:61:24)
    at ReaderSync.api (/usr/local/lib/node_modules/yo/node_modules/fast-glob/out/providers/reader-sync.js:53:25)
Emitted 'error' event at:
    at DirectoryReader.emit (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/directory-reader.js:365:14)
    at call.safe (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/directory-reader.js:81:14)
    at onceWrapper (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/call.js:45:17)
    at onceWrapper (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/call.js:45:17)
    at exports.readdir (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/sync/fs.js:22:5)
    at Object.safeCall [as safe] (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/call.js:24:8)
    [... lines matching original stack trace ...]
    at readdirSync (/usr/local/lib/node_modules/yo/node_modules/@mrmlnc/readdir-enhanced/lib/sync/index.js:27:21)

我哪里错了?

我认为您没有做错任何事,看起来 yeoman 的依赖破坏了 yeoman。它是 yeoman-environment 依赖项,这个问题已经打开:https://github.com/yeoman/environment/issues/97 复制粘贴下面的问题:

While running yo, the following error occurs: Error: EACCES: permission denied, scandir '/usr/sbin/authserver (MacOS / High Sierra)

Culprit is here:

  // Adds support for generator resolving when yeoman-generator has been linked
  if (process.argv[1]) {
        paths.push(path.join(path.dirname(process.argv[1]), '../..'));
  }

inside getNpmPaths(). My yo is at /usr/local/bin/yo, and this adds the whole /usr directory to the search path => globby.sync inside resolver.findsGeneratorIn will throw if some directories are not user readable.

Something like this fixes the issue:

try {
      modules = modules.concat(globby.sync(
        ['generator-*', '@*/generator-*'],
        {cwd: root, onlyFiles: false, absolute: true}
      ));
} catch(err) {
     debug( 'Could not access %s (%s)', root, err); 
}

我可以通过将全局包安装到不同的文件夹来解决这个问题。 https://docs.npmjs.com/getting-started/fixing-npm-permissions

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, it will be a hidden directory on your home folder.

Back-up your computer before you start.

Make a directory for global installations:

mkdir ~/.npm-global 

Configure npm to use the new directory path:

npm config set prefix '~/.npm-global'

Open or create a ~/.profile file and add this line:

export PATH=~/.npm-global/bin:$PATH

Back on the command line, update your system variables:

source ~/.profile

Test: Download a package globally without using sudo.

npm install -g jshint

Instead of steps 2-4, you can use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global