以下终端运行结果:
v1.0* $ nodemon 02.,模块化路由.js
zsh: permission denied: nodemon
yangqing@KimdeMac-Book-air ~/VS code/6.Node/day4/code
v1.0* $ sudo nodemon 02.,模块化路由.js [9:25:46]
Password:
Sorry, try again.
Password:
sudo: nodemon: command not found
直接安装命令npm install -g nodemon,报错:
npm install -g nodemon
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/nodemon
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/nodemon'
npm ERR! [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/nodemon'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'mkdir',
npm ERR! path: '/usr/local/lib/node_modules/nodemon'
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/yangqing/.npm/_logs/2023-04-11T06_20_42_305Z-debug-0.log
可以使用以下两种方式在Mac上安装Node.js:
1.通过官方安装程序安装
前往 Node.js 官网 https://nodejs.org/ 下载 Node.js 官方安装程序,然后双击下载的 .pkg 文件,按照提示完成安装即可。
2.使用Homebrew安装
如果您使用了 Homebrew,那么可以通过以下命令在终端中安装Node.js:
安装完成后,可以输入以下命令来检查 Node.js 是否成功安装:
如果输出了 Node.js 的版本号,则说明 Node.js 已经安装成功了。
注意:如果您系统中同时安装了多个版本的 Node.js,可以使用 n 或 nvm 等版本管理工具来管理不同版本的 Node.js。
在 Mac 上,你可以通过以下命令全局安装 Nodemon:
在安装完成后,在控制台输入
nodemon
命令即可使用 Nodemon 工具了。这个错误提示看起来是安装后没有加入环境变量导致的。请按照以下步骤操作:
确认nodemon是否已加入环境变量。可以在命令行中输入以下命令查看nodemon是否在环境变量中:
如果路径中没有包含
/usr/local/bin
,可以尝试手动加入到环境变量中:或者将上述命令加入到
.bashrc
文件中,使其永久生效:然后再次尝试运行nodemon命令即可。