尝试全局安装 Nodemon 但出现错误

时间:2021-05-15 09:48:47

标签: javascript node.js nodemon

我曾尝试将 Nodemon 本地安装到 Node js 和全局。当我在本地创建时,CLI 不接受 nodemon 作为命令;所以我尝试全局安装 Nodemon,这给出了这个错误:

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/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!     /Users/akashdeep/.npm/_logs/2021-05-15T09_44_48_698Z-debug.log

3 个答案:

答案 0 :(得分:2)

永远不要使用 sudo 来使用 npm。它会弄乱 node_modules 文件夹的权限。

首先通过运行命令检查路径/usr/local/lib/node_modules的文件夹权限

ls -la /usr/local/lib/

您很可能会看到该文件夹​​的所有权属于“root”

您需要将所有权从 root 更改为当前用户。

sudo chown -R <username> /usr/local/lib/node_modules

这里的用户名是您当前的用户名。 运行命令后,再次检查权限。 它应该从 root 更改为当前用户。 然后尝试全局安装 nodemon,它不应该抛出任何错误。 永远不要使用 sudo 命令运行 npm。

答案 1 :(得分:1)

在全局范围内,尝试使用 sudo 安装

sudo npm i -g nodemon

对于本地,在您的 package.json

向脚本添加命令

{
   "scripts": {
       "nodemon": "nodemon index.js" 
     }
}

其中 index.js 是您的任何名称的启动 js 文件

然后在 cli 运行

npm run nodemon

答案 2 :(得分:1)

您必须以 root 权限运行它(如 Alan Tishin)。

如果您不想在没有 root 权限的情况下全局安装软件包,请按照以下步骤操作: