webpack build报错

执行npm run build时报错如下:

D:\MyProjects\react_ssr_demo>npm run build

> react_ssr_demo@1.0.0 build D:\MyProjects\react_ssr_demo
> webpack --config build/webpack.config.js

Hash: 0b1003a436390263970e
Version: webpack 4.29.3
Time: 633ms
Built at: 2019-02-09 15:46:04
 1 asset
Entrypoint app = app.0b1003a436390263970e.js
[2] ./client/App.jsx 3 KiB {0} [not cacheable] [built] [failed] [1 error]
[3] ./client/app.js 100 bytes {0} [built]
[9] (webpack)/buildin/global.js 472 bytes {0} [built]
    + 7 hidden modules

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for th
is value. Set 'mode' option to 'development' or 'production' to enable defaults
for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https
://webpack.js.org/concepts/mode/

ERROR in ./client/App.jsx
Module build failed (from ./node_modules/_babel-loader@8.0.5@babel-loader/lib/in
dex.js):
Error: Cannot find module '@babel/core'
 babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to
 use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (D:\MyProjects\react_ssr_demo\node_modules\_v8-compile-cache@2.0.
2@v8-compile-cache\v8-compile-cache.js:159:20)
    at Object.<anonymous> (D:\MyProjects\react_ssr_demo\node_modules\_babel-load
er@8.0.5@babel-loader\lib\index.js:10:11)
    at Module._compile (D:\MyProjects\react_ssr_demo\node_modules\_v8-compile-ca
che@2.0.2@v8-compile-cache\v8-compile-cache.js:178:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (D:\MyProjects\react_ssr_demo\node_modules\_v8-compile-cache@2.0.
2@v8-compile-cache\v8-compile-cache.js:159:20)
    at loadLoader (D:\MyProjects\react_ssr_demo\node_modules\_loader-runner@2.4.
0@loader-runner\lib\loadLoader.js:18:17)
    at iteratePitchingLoaders (D:\MyProjects\react_ssr_demo\node_modules\_loader
-runner@2.4.0@loader-runner\lib\LoaderRunner.js:169:2)
    at runLoaders (D:\MyProjects\react_ssr_demo\node_modules\_loader-runner@2.4.
0@loader-runner\lib\LoaderRunner.js:365:2)
    at NormalModule.doBuild (D:\MyProjects\react_ssr_demo\node_modules\_webpack@
4.29.3@webpack\lib\NormalModule.js:280:3)
    at NormalModule.build (D:\MyProjects\react_ssr_demo\node_modules\_webpack@4.
29.3@webpack\lib\NormalModule.js:427:15)
    at Compilation.buildModule (D:\MyProjects\react_ssr_demo\node_modules\_webpa
ck@4.29.3@webpack\lib\Compilation.js:635:10)
    at factory.create (D:\MyProjects\react_ssr_demo\node_modules\_webpack@4.29.3
@webpack\lib\Compilation.js:884:14)
    at factory (D:\MyProjects\react_ssr_demo\node_modules\_webpack@4.29.3@webpac
k\lib\NormalModuleFactory.js:405:6)
    at hooks.afterResolve.callAsync (D:\MyProjects\react_ssr_demo\node_modules\_
webpack@4.29.3@webpack\lib\NormalModuleFactory.js:155:13)
    at AsyncSeriesWaterfallHook.eval [as callAsync] (eval at create (D:\MyProjec
ts\react_ssr_demo\node_modules\_tapable@1.1.1@tapable\lib\HookCodeFactory.js:32:
10), <anonymous>:6:1)
    at resolver (D:\MyProjects\react_ssr_demo\node_modules\_webpack@4.29.3@webpa
ck\lib\NormalModuleFactory.js:138:29)
    at process.nextTick (D:\MyProjects\react_ssr_demo\node_modules\_webpack@4.29
.3@webpack\lib\NormalModuleFactory.js:342:9)
    at process._tickCallback (internal/process/next_tick.js:61:11)
 @ ./client/app.js 2:0-27 4:16-19
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! react_ssr_demo@1.0.0 build: `webpack --config build/webpack.config.js`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the react_ssr_demo@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional log
ging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Administrator.PC-201812050004\AppData\Roaming\npm-cache\_l
ogs\2019-02-09T07_46_04_537Z-debug.log
复制代码

这里面有两个问题

  1. WARNING in configuration

解决方法:在webpack.config.js下增加mode: ‘none’ module.exports = { mode: ‘none’,

  1. ERROR in ./client/App.jsx Module build failed (from ./node_modules/_babel-loader@8.0.5@babel-loader/lib/in dex.js):

Error: Cannot find module '@babel/core' babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.

解决方法:npm i babel-loader@7 -D

### 解决 `yarn build` 过程中的常见错误 #### 1. TypeScript 类型检查问题 当执行 `yarn build` 出现与TypeScript相关的报错时,通常是因为某些文件不符合定义的类型规则。为了修正这类问题,可以尝试更新项目的依赖库到最新版本,并确保所有的 `.ts` 和 `.tsx` 文件都遵循严格的类型约束[^1]。 对于具体的TS检测错误,建议仔细阅读编译器给出的具体提示信息来定位具体位置并调整代码逻辑或声明缺失的接口/类等结构体;另外也可以通过修改 tsconfig.json 中的一些选项如 "skipLibCheck": true 来跳过对外部库类型的验证从而绕开一些不必要的警告。 #### 2. 缺失模块或路径不存在 针对在不同环境中运行构建命令时遇到的不同目录下找不到特定文件夹或是无法解析指定模块的情况,这可能是由于环境变量设置不当、相对路径使用失误造成的。确认所有必要的资源都被正确引入并且存在于预期的位置非常重要[^2]。 一种有效的排查方式是从头开始重新创建整个开发环境——即彻底清除现有的 node_modules 并再次执行 npm install 或者 yarn 安装最新的包列表。同时注意查看 package.json 文件内的 scripts 字段是否有误写入了不适合当前系统的指令集。 #### 3. 特定工具链组件冲突 有时即使完成了上述操作仍然会碰到诸如 node-sass 不兼容等问题引发的新一轮异常状况。此时应考虑是否存在多个相似功能却互相排斥的技术栈共存于单一项目之中。例如,在同一个工程内既用了 sass 又单独引入了 node-sass,则可能造成二者间产生矛盾进而影响正常工作流程[^3]。 解决办法之一便是依据官方文档推荐的最佳实践挑选最合适的预处理器及其配套加载程序进行集成,必要时候还需同步升级 Node.js 的核心版本号以获得更好的支持度。 #### 4. 插件配置遗漏 最后还有一种情况是忘记为 Webpack 等构建工具添加必需的插件而导致失败的结果。虽然可以通过移除引起争议的部分暂时缓解症状,但这并不是长久之计。更优的选择是在对应的配置文件(比如 vue.config.js 对 Vue CLI 而言)中显式地注册所需的扩展服务[^4]。 ```javascript // 示例:向 webpack 添加 html-webpack-plugin 插件 const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { plugins: [ new HtmlWebpackPlugin({ template: './src/index.html' }) ] }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值