Laravel 5.6执行npm install报错:npm不是内部或外部命令
Hey there, let's get that annoying npm error sorted out quickly. That message pops up because your system can't find the npm command—usually because Node.js isn't installed, or it's not properly added to your system's environment variables. Here's how to fix it step by step:
1. Check if Node.js & npm are installed
First, let's confirm whether Node.js and npm are even on your system. Open up your terminal (Command Prompt on Windows, Terminal app on Mac/Linux) and run these two commands:
node -v npm -v
If either command throws an error or doesn't show a version number, you need to install Node.js. Grab the LTS (Long-Term Support) version from the official Node.js site—it's the most stable choice for Laravel projects. When installing, make sure to check the box that says "Add to PATH" (this automatically configures your system to recognize node and npm commands everywhere).
2. Fix environment variables if Node.js is already installed
If you already have Node.js installed but still see the error, the problem is likely that your system's PATH doesn't include the Node.js directory.
- Windows: Right-click "This PC" → "Properties" → "Advanced System Settings" → "Environment Variables". Under "System Variables", find the
Pathentry, click "Edit", and add the path to your Node.js installation folder (usuallyC:\Program Files\nodejs\). Save your changes, then restart your Command Prompt. - Mac/Linux: Open your terminal and check if the Node.js path is in your shell config file (like
~/.bashrcor~/.zshrc). If not, add this line (replace the path with your actual Node.js install location):
Save the file, then runexport PATH="/usr/local/bin/node:$PATH"source ~/.bashrc(orsource ~/.zshrc) to apply the changes, or just restart your terminal.
3. Verify your Laravel project's package.json
While your composer.json looks fine, Laravel relies on a package.json file to manage frontend dependencies. If this file is missing from your project root, run:
npm init
Follow the prompts to generate a new package.json, or you can copy the default package.json content from a fresh Laravel 5.6 project (it should include dependencies like laravel-mix, vue, etc.).
Quick extra tip
After installing Node.js, it's a good idea to update npm to the latest version:
npm install -g npm
Once you've done these steps, go back to your Laravel project root and run npm install again—it should work without that error now!
内容的提问来源于stack exchange,提问作者Rehman Maqsood

