通过 npm 安装无服务器所需的权限

permission needed to install serverless via npm

我正在尝试通过 npm (npm install -g serverless) 安装无服务器,但出现以下错误:

npm ERR! code EACCES
npm ERR! syscall rename
npm ERR! path /usr/local/lib/node_modules/serverless
npm ERR! dest /usr/local/lib/node_modules/.serverless-Ls3sFIzE
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/serverless' -> '/usr/local/lib/node_modules/.serverless-Ls3sFIzE'
npm ERR!  [Error: EACCES: permission denied, rename '/usr/local/lib/node_modules/serverless' -> '/usr/local/lib/node_modules/.serverless-Ls3sFIzE'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'rename',
npm ERR!   path: '/usr/local/lib/node_modules/serverless',
npm ERR!   dest: '/usr/local/lib/node_modules/.serverless-Ls3sFIzE'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

有谁知道如何在不切换您正在使用的节点版本的情况下执行此操作?

编辑: 当我 运行 使用 sudo 时,我得到以下输出:

npm WARN deprecated request-promise-native@1.0.9: request-promise-native has been deprecated because it extends the now deprecated request package, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@3.3.2: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142

changed 672 packages, and audited 673 packages in 25s

50 packages are looking for funding
  run npm fund for details

found 0 vulnerabilities

但是,当我尝试 运行 serverless invoke local --function f1 我得到 zsh: command not found: serverless - 在我使用 sudo 安装后,我需要做什么才能识别命令?

您的权限被搞砸了。一般来说,您不想使用 sudo 从 npm 安装任何东西。

你可以sudo chown yourUser:yourGroup -R /usr/local/lib/node_modules。 (如果你在 macOS 上,你的组可能是 staff。你可以通过 运行ning ls -l 在你的主目录中找到你的组,并查看你的用户名旁边的标签)

无论如何,一个简单的替代方案是最佳实践 - 安装无服务器框架作为项目的开发依赖项:

npm i -D serverless

然后 运行ning serverless deploy 你可以 运行 npx serverless deploy.

然后当您将 package.json 文件签入版本控制时,其他协作者将使用相同版本的无服务器框架。