You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel环境下无法执行npm install与npm run dev请求技术协助

Hey Luciano, let’s work through this npm issue with your fresh Laravel install step by step—this kind of problem is super common, so we’ll nail it down quickly.

1. First, Check Node.js & npm Version Compatibility

Latest Laravel versions (10/11) require specific Node.js/npm versions to play nice. Laravel typically recommends Node.js 16.x+ (LTS is best) and npm 8.x+. Let’s verify your current versions:

node -v
npm -v

If your versions are out of range, head to the Node.js official site to grab the latest LTS release (18.x or 20.x are solid picks). Restart your terminal after installing, then try again.

2. Wipe Cache & Leftover Files (Critical Step!)

Residual cache or broken lock files often cause install failures. Let’s clean everything up:

  • Delete your project’s node_modules folder and package-lock.json file (right-click delete, or use terminal commands):
rm -rf node_modules package-lock.json
  • Clear npm’s global cache to remove corrupted data:
npm cache clean --force

Now navigate back to your project root and run npm install again.

3. Validate Your package.json Dependencies

If your package.json has invalid or outdated dependencies, npm will throw errors. Double-check that your devDependencies match Laravel’s default setup for the version you installed. For example, Laravel 11’s default looks like this:

{
    "private": true,
    "type": "module",
    "scripts": {
        "dev": "vite",
        "build": "vite build"
    },
    "devDependencies": {
        "@vitejs/plugin-laravel": "^0.8.0",
        "vite": "^5.0.0"
    }
}

If yours has mismatched version numbers or missing packages, replace it with the official default from Laravel’s base setup, then re-run npm install.

4. Fix Permissions (Linux/macOS Users)

Permission issues are a frequent culprit on Unix-based systems. Try setting proper ownership for your npm folders and project:

sudo chown -R $USER:$USER ~/.npm
sudo chown -R $USER:$USER /path/to/your/laravel/project

Alternatively, use nvm (Node Version Manager) to install Node.js—it keeps packages in your user directory, so you won’t need sudo for installs.

5. Try npm ci Instead of npm install

If npm install still fails, npm ci is a stricter alternative that installs exactly what’s in your package-lock.json (no version mismatches). Run:

npm ci
6. Rule Out Network Issues

If the install hangs on downloading packages, your network might be blocking npm’s registry. Try switching to a mirror temporarily:

npm config set registry https://registry.npmmirror.com/

Run npm install again. If this works, you can switch back to the official registry later with:

npm config set registry https://registry.npmjs.org/
7. Troubleshoot npm run dev Failures

If npm install succeeds but npm run dev breaks, it’s usually a Vite configuration issue:

  • Confirm your vite.config.js matches Laravel’s default:
import { defineConfig } from 'vite';
import laravel from '@vitejs/plugin-laravel';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
});
  • Check if the default Vite port (5173) is occupied. You can specify a different port with:
npm run dev -- --port 5174

If none of these steps fix the problem, share the exact error messages you’re getting (from both npm install and npm run dev)—that will let us pinpoint the exact issue.


内容的提问来源于stack exchange,提问作者Luciano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:17:54