如何在 AWS CloudShell 中安装 npm?

How to npm install in AWS CloudShell?

我正在尝试在新的 AWS CloudShell(pre-configured Node.js support 附带)中安装 npm 程序包 - 但我遇到 EACCESS 错误。


npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/lib/node_modules'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/lib/node_modules'
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.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/cloudshell-user/.npm/_logs/2021-01-06T02_18_33_584Z-debug.log

在 AWS CloudShell 中安装 npm 包的最佳方式是什么?

为了解决这个问题,我遵循了 https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally:

中的说明

第一个:

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

在您首选的文本编辑器中,打开或创建一个 ~/.profile 文件并添加此行:

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

然后:

source ~/.profile