MacOS M1 Monterey系统下Vue2/NuxtJS项目npm依赖安装报错求助
Hey there! Let's figure out why you're running into errors when installing dependencies for your Vue2/NuxtJS project on your M1 Mac with macOS Monterey. The sudo npm i command is often the first red flag here, so let's break down the common issues and fixes step by step:
1. You probably don't need sudo (and it's causing more harm than good)
Running sudo npm i can mess up file permissions in your project and create conflicts between your user-level Node.js environment (which might be managed via tools like nvm or Homebrew) and the root-level system environment. M1 Macs typically have Node.js installed in user-specific paths, so using sudo bypasses that and uses the outdated default system Node version—this is a super common cause of dependency errors.
Fix:
First, fix your npm directory permissions so you never need sudo for installs again:
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Then try running npm i without sudo.
2. M1 architecture compatibility issues (legacy packages hate arm64)
A lot of older npm packages used in Vue2/NuxtJS stacks (like node-sass, fibers, or native add-ons) weren't built for Apple's arm64 architecture. Since Vue2 and Nuxt v2 are mature frameworks, you're likely pulling in dependencies that haven't been updated to support M1 natively.
Fixes to try:
- Use Rosetta 2 for your terminal: Right-click Terminal in Spotlight > Get Info > Check "Open using Rosetta". Restart your terminal, then run
npm iagain. This runs the terminal in x86_64 mode, which most legacy packages support. - Force x86_64 installs directly: If Rosetta isn't your vibe, run this command to install dependencies in x86 mode:
arch -x86_64 npm i - Swap out problematic packages: For example,
node-sassis deprecated and has known M1 issues. Replace it withsass(the modern Dart Sass implementation) in yourpackage.json:
Nuxt 2 should automatically use the new package, but double-check your// Replace this line: "node-sass": "^4.14.1", // With this: "sass": "^1.50.0"nuxt.config.jsfor any hardcoded references tonode-sassorsass-loader.
3. Your Node.js version might be too new
Vue2 and NuxtJS v2 have strict Node.js version requirements—they work best with Node 14.x or 16.x LTS. If you're running Node 18+ on your M1 Mac, that could be causing compatibility breaks with older dependencies.
Fix:
Use a version manager like nvm to install and switch to the correct Node version:
- Install nvm via Homebrew (if you have it set up):
Follow the post-install instructions to add nvm to your shell config (likebrew install nvm.zshrcor.bashrc). - Install Node 16 LTS (the most stable version for Nuxt 2):
nvm install 16 nvm use 16 - Now run
npm iagain.
4. Corrupted npm cache
Sometimes your npm cache gets filled with corrupted or architecture-specific files that cause installation failures, especially if you've switched between x86 and arm64 modes.
Fix:
Clear the cache and start fresh:
npm cache clean --force rm -rf node_modules package-lock.json npm i
Start with the first fix (ditching sudo) since that's the most likely culprit, then work through the others if needed. If you hit a specific error message, feel free to share it—I can help narrow it down further!
内容的提问来源于stack exchange,提问作者R.Batyrshin

