npm中找不到nodemon。

106

我有一个问题:nodemon不能在npm脚本中运行(例如npm start),但是如果在命令行中调用nodemon而不是在npm脚本中,nodemon会正常运行。

$ nodemon server.js
14 Feb 22:59:51 - [nodemon] v1.3.7
14 Feb 22:59:51 - [nodemon] to restart at any time, enter `rs`
14 Feb 22:59:51 - [nodemon] watching: *.*
14 Feb 22:59:51 - [nodemon] starting `node server.js`

在npm脚本中的名称:

package.json

{
...
  "scripts": {
    "start": "nodemon server.js"
  }
}

当运行npm启动脚本时:

$ npm start
> aaa@0.0.1 start /home/akul/Documents/aaa
> nodemon server.js

sh: 1: nodemon: not found

npm ERR! Linux 3.13.0-45-generic
npm ERR! argv "node" "/home/akul/npm-global/bin/npm" "start"
npm ERR! node v0.12.0
npm ERR! npm  v2.5.0
npm ERR! code ELIFECYCLE
npm ERR! aaa@0.0.1 start: `nodemon server.js`
npm ERR! Exit status 127
npm ERR! 
npm ERR! Failed at the aaa@0.0.1 start script 'nodemon server.js'.
npm ERR! This is most likely a problem with the aaa package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     nodemon server.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls aaa
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /home/akul/Documents/aaa/npm-debug.log

我一直在寻找解决方案,但没有找到。

31个回答

152
你可以通过将 nodemon 添加到你的 package.json 文件中来解决此问题:
npm install nodemon --save-dev

nodemon/node_modules/.bin目录下不存在时,就会出现这个问题。

添加--save-dev是因为它仅在开发过程中需要。


4
npm警告:建议使用-g安装nodemon@1.3.7 但现在已经成功了,谢谢。 所以nodemon在一个未使用的全局目录中 :D - akul
1
我也遇到了同样的问题。现在解决方法是运行以下命令 - npm install nodemon --save - Bipon Biswas
8
不要使用带有“--save”标志的nodemon安装程序,因为nodemon仅用于开发。请使用“-g”或“--save-dev”标志。 - grey87

46

尝试检查已安装的全局包npm list -g --depth=0。 如果您没有找到nodemon,请使用标志-g--save-dev进行安装。 不要使用标志--save安装nodemon,因为nodemon仅用于开发


2
npm install -g nodemon - Lord Elrond

37
在您当前的项目目录下,运行:
npm install nodemon --save //save in package.json so that the following code cam find your nodemon

然后在你的package.json文件的"scripts"下面添加"start": "nodemon app.js"(或者是你的入口文件名),
让它看起来像这样:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon app.js"
}

然后运行

npm start

这样可以避免复杂的路径设置,并且在我的Mac上运行良好。
希望能对您有所帮助;)


5
不要使用带有 --save 标志安装 nodemon,因为 nodemon 只用于开发。请使用 -g 或 --save-dev。 - grey87

29

使用以下命令全局安装nodemon。它可以在我的计算机上工作,我相信它也会在您的系统上工作。

npm install nodemon -g --save

有时候你需要获得全局安装的权限。这可以通过使用以下命令轻松完成。

  1. 在LINUX UBUNTU中: sudo npm install nodemon -g --save

  2. 在Fedora中:

    a) su
    b)npm install nodemon -g --save


11

尝试全局安装nodemon。

sudo npm install -g nodemon

6

我是如何修复它的:

当我使用 npm install nodemon -g --save 安装nodemon时,我的全局npm包的路径未出现在PATH变量中。

如果你将其添加到$PATH变量中,它就会得到解决。

编辑您的主文件夹中的~/.bashrc 文件并添加此行:

export PATH=$PATH:~/npm

这里的“npm”是我的全局npm包的路径。请用您系统中的全局路径替换它。


路径应该是npm二进制文件的位置。 这个命令可以正常工作:export PATH=$PATH:~/npm/bin - Because i hate myself

4

我曾经遇到同样的问题并成功解决了。这是我的错误:

npm install -g nodemon
npm WARN checkPermissions Missing write access to /usr/local/lib
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@~2.3.2 (node_modules/nodemon/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib'
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/usr/local/lib\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib' }
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/rayani00/.npm/_logs/2022-01-03T17_50_15_842Z-debug.log
(base) rayani00@rayani00:~/Bureau/my-express-server$ npm cache clear --force
npm WARN using --force I sure hope you know what you are doing.

为了解决这个问题,我只需在nodemon安装命令前加上sudo:

  1. sudo npm install -g nodemon
  2. nodemon server.js

这对我起作用了!


4

Heroku默认运行在生产环境,因此不会安装dev依赖项。

如果您不想将nodemon作为依赖项重新安装,我认为不应该这样做,因为它的正确位置是在devDependencies而不是dependencies中。

相反,您可以创建两个npm脚本来避免此错误,仅在本地主机上运行nodemon,如下所示:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js",
    "start:dev": "nodemon --watch"
},

当您想在本地运行项目时,请在终端中输入npm run start:dev,nodemon会加载app.js。

而在Heroku上,默认情况下会运行npm start命令,并从普通的node命令加载app.js,这样您就可以摆脱那个错误了。


3
你只需要全局安装它。 npm install -g nodemon

这对我也起作用了。然后开始 - #nodemon ./path/to/startfile - Michael Nelles

2

这个解决方案对我有用:

我假设您已经全局安装了nodemon。 如果是这样,请按照以下步骤操作:

打开您的 .bash_profile 文件:

nano .bash_profile

将以下内容粘贴到您的Bash配置文件中以添加新的别名:

alias nodemon='~/.npm-global/lib/node_modules/nodemon/bin/nodemon.js'

现在您可以在任何地方使用nodemon命令。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接