macOS High Sierra环境下Node.js服务器无法启动及权限问题求助
Hey there, let’s work through your Node.js server issue and that annoying permission error step by step.
First: Resolve the "Permission Denied" when deleting node_modules
The /usr/local/lib/node_modules directory is owned by root by default on macOS, which is why you can’t delete it with a regular rm command. Here are two reliable fixes:
- Use
sudoto temporarily gain root privileges:
Run this command in iTerm, then enter your macOS user password (note: nothing will show on screen as you type—this is normal):sudo rm -rf /usr/local/lib/node_modules - Alternatively, take ownership of the directory first (so you won’t need sudo for future operations here):
After running this, you can delete the directory with a regularsudo chown -R $(whoami) /usr/local/lib/node_modulesrm -rf /usr/local/lib/node_modulescommand without permission issues.
Next: Troubleshoot Node.js Server Startup Failure
Deleting node_modules might fix dependency-related issues, but let’s cover other common causes too:
Verify Node.js is installed correctly:
Run these commands to check if Node and npm are properly set up:node -v npm -vIf either command doesn’t return a version number, you’ll need to reinstall Node.js (download the installer from the official site, or use a version manager like nvm for easier future management).
Check for startup errors:
When you try to start your server, pay close attention to the error messages in iTerm. Common issues include:- Port conflicts (another app is using the port your server tries to listen on)
- Syntax errors in your Node.js code
- Missing dependencies (even after reinstalling, double-check that all required packages are listed in
package.json)
Reinstall dependencies after deleting node_modules:
Once you’ve removed the old node_modules folder, navigate to your project directory in iTerm and run:npm installThis will rebuild all dependencies based on your
package.jsonfile.
内容的提问来源于stack exchange,提问作者rollback

