最近尝试使用vite,初始化vue项目之后,运行 npm run dev
报错,问题分析以及解决方案记录一下。
报错内容如下:
报错内容
throw new Error(`esbuild: Failed to install correctly ^ Error: esbuild: Failed to install correctly Make sure you don't have "ignore-scripts" set to true. You can check this with "npm config get ignore-scripts". If that returns true you can reset it back to false using "npm config set ignore-scripts false" and then reinstall esbuild. If you're using npm v7, make sure your package-lock.json file contains either "lockfileVersion": 1 or the code "hasInstallScript": true. If it doesn't have either of those, then it is likely the case that a known bug in npm v7 has corrupted your package-lock.json file. Regenerating your package-lock.json file should fix this issue. ... at Module._compile (internal/modules/cjs/loader.js:1015:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10) at Module.load (internal/modules/cjs/loader.js:879:32) at Function.Module._load (internal/modules/cjs/loader.js:724:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) at internal/main/run_main_module.js:17:47 ... error when starting dev server: Error [ERR_STREAM_DESTROYED]: Cannot call write after a stream was destroyed at doWrite (_stream_writable.js:399:19) at writeOrBuffer (_stream_writable.js:387:5) ... npm ERR! command failed npm ERR! command sh -c vite ...
解读报错
首先确认是esbuild的报错,安装失败。
报错信息其实已经很清楚了
-
针对
npm
版本v7以下- 首先确认
ignore-scripts
是否被设置成true
。 - 运行
npm config get ignore-scripts
,如果是false
则是正确的,否则需要通过运行npm config set ignore-scripts false
将ignore-scripts
设置为false
。 - 通过设置之后,再次运行调试应该是ok的了。
- 首先确认
-
npm
版本v7以上的- 还需要确认一下
package-lock.json
文件的lockfileVersion
字段为1
,或者hasInstallScript
设置为true
了。如果两者都没有被正确设置,则就是一个已知的BUG。解决办法参看后文
- 还需要确认一下
npm > v7
-
通过github查阅vite仓库的相关issue【https://github.com/vitejs/vite/issues/1580】,尤大也很明确的指出这个是esbuild的BUG。
-
问题的解决办法在【https://github.com/evanw/esbuild/issues/462#issuecomment-771328459】。
-
手动运行
node node_modules/esbuild/install.js
来解决esbuild
安装问题。 安装完成之后,再次运行
npm run dev
即可。