为什么在终端中键入 npm install 时会出现这些错误?

Why do I get these errors when I type npm install in the terminal?

每当我使用 npm 安装任何东西时,我都会收到以下错误列表:

npm ERR! code EPERM
npm ERR! syscall mkdir
npm ERR! path C:\Users\Firstname~1
npm ERR! errno -4048
npm ERR! Error: EPERM: operation not permitted, mkdir 'C:\Users\Firstname~1'
npm ERR!  [OperationalError: EPERM: operation not permitted, mkdir 'C:\Users\Firstname~1'] {
npm ERR!   cause: [Error: EPERM: operation not permitted, mkdir 'C:\Users\Firstname~1'] {
npm ERR!     errno: -4048,
npm ERR!     code: 'EPERM',
npm ERR!     syscall: 'mkdir',
npm ERR!     path: 'C:\Users\Firstname~1'
npm ERR!   },
npm ERR!   isOperational: true,
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: 'C:\Users\Firstname~1'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It's possible that the file was already in use (by a text editor or antivirus),
npm ERR! or that you lack permissions to access it.
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?

看起来像是 npm 配置错误问题。

理想情况下,您可以将其配置为仅使用您的用户有权访问的目录,这样您就不需要管理员权限来安装全局包。

首先,使用类似

的命令检查 npm 前缀是什么
npm config ls -l

我打赌它会显示类似

的内容
prefix = "C:\Users\"

尝试在 C:\Users{username}\ 中创建一个 .npmrc 文件,其中 {username} 是您自己的,条目如下

prefix = "C:\Users\{username}\some_dir_you_have_access"

然后像 npm install -g package 一样使用 -g,这样 npm 会尝试在您定义的“前缀”指定的路径中安装全局包。

npm 文件夹记录在此处 https://docs.npmjs.com/cli/v7/configuring-npm/folders