npm本地安装nodemon后运行nodemon server.js提示命令未识别如何解决
nodemon : The term 'nodemon' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
出现该报错是因为你执行的是本地安装nodemon的命令,没有将nodemon注册为系统全局可调用的命令,可按以下任意一种方式处理:
- 方法1:直接使用npx调用本地已安装的nodemon
直接执行命令:npx nodemon server.js即可,无需额外修改配置,适合临时调试使用。 - 方法2:全局安装nodemon
执行全局安装命令:npm install -g nodemon,安装完成后就可以直接运行nodemon server.js。
如果全局安装后仍提示找不到命令,需要将npm全局包存储路径添加到系统环境变量的Path项中,Windows系统默认路径为C:\Users\你的用户名\AppData\Roaming\npm,Mac/Linux系统默认路径为/usr/local/bin,添加完成后重启终端即可生效。
注:Mac/Linux系统全局安装时若提示权限错误,可执行sudo npm install -g nodemon输入开机密码完成安装。 - 方法3:通过package.json脚本调用
在项目的package.json文件的scripts字段中添加配置:
之后执行"scripts": { "dev": "nodemon server.js" }npm run dev即可启动服务,npm会自动查找项目本地node_modules中的nodemon包,无需全局安装,适合项目内固定使用。
内容的提问来源于stack exchange,提问作者Faizan Ansari
相关产品推荐
相关产品推荐

