问题
node
项目通过npm run dev
启动时出现ERROR Failed to compile with 1 errors
错误,具体信息如下:
✖ Webpack
Compiled with some errors in 366.81ms
ERROR Failed to compile with 1 errors 11:42:13
error in ./web/src/pages/.umi-production/umi.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module 'babel-plugin-canyon' from '/Users/xxx/Desktop/work/project/xxx'
at Function.resolveSync [as sync] (/Users/xxx/Desktop/work/project/xxx/node_modules/resolve/lib/sync.js:111:15)
at resolveStandardizedName (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/files/plugins.js:101:31)
at resolvePlugin (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/files/plugins.js:54:10)
at loadPlugin (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/files/plugins.js:62:20)
at createDescriptor (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/config-descriptors.js:154:9)
at /Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/config-descriptors.js:109:50
at Array.map (<anonymous>)
at createDescriptors (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
at createPluginDescriptors (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/config-descriptors.js:105:10)
at /Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/config-descriptors.js:63:49
at cachedFunction (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/caching.js:33:19)
at plugins (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/config-descriptors.js:28:77)
at mergeChainOpts (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/config-chain.js:319:26)
at /Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/config-chain.js:283:7
at buildRootChain (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/config-chain.js:68:29)
at loadPrivatePartialConfig (/Users/xxx/Desktop/work/project/xxx/node_modules/@babel/core/lib/config/partial.js:85:55)
@ multi ./node_modules/umi-build-dev/template/setPublicPath.js ./web/src/pages/.umi-production/umi.js
解决方案
【1】删除项目中原有的node_modules
模块和package-lock.json
文件;
【2】执行 npm i
命令安装项目依赖,它会根据项目根目录下的package.json
文件中的依赖配置,将所需的包下载并安装到node_modules
目录中。
xxx@xxx xxx % npm i
【3】执行npm run build
命令来构建项目的。通常在开发过程中,我们会使用一些构建工具(如Webpack
、Gulp
等)来对项目进行打包、压缩、转译等操作,以生成最终的可部署的代码。通过在package.json
文件的scripts
字段中配置build
命令,我们可以使用npm run build
来执行构建操作。具体的构建逻辑会根据项目的需求和配置而定,例如将源代码转换为浏览器可执行的JavaScript
、CSS
和HTML
文件,或者将代码打包成可发布的文件等。
xxx@xxx xxx % npm run build
【4】执行npm run dev
即可正常启动项目,不会再出现上述错误。