一、问题原因
运行yarn serve报错如下:
yarn run v1.22.22
$ vue-cli-service serve
INFO Starting development server...
ERROR Failed to compile with 1 error 11:24:17
error in ./src/views/Integral/IntegralOrder.vue?vue&type=script&lang=js
Module not found: Error: Can't resolve 'os' in 'F:\study\vue\vue2-element-shop\src\views\Integral'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "os": require.resolve("os-browserify/browser") }'
- install 'os-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "os": false }
ERROR in ./src/views/Integral/IntegralOrder.vue?vue&type=script&lang=js (./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/@vue/vue-loader-v15/lib/index.js??vue-loader-options!./src/views/Integral/IntegralOrder.vue?vue&type=script&lang=js) 7:0-31
Module not found: Error: Can't resolve 'os' in 'F:\study\vue\vue2-element-shop\src\views\Integral'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "os": require.resolve("os-browserify/browser") }'
- install 'os-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "os": false }
@ ./src/views/Integral/IntegralOrder.vue?vue&type=script&lang=js 1:0-212 1:228-231 1:233-442 1:233-442
@ ./src/views/Integral/IntegralOrder.vue 2:0-64 3:0-59 3:0-59 10:2-8
@ ./src/router/index.js 42:0-64 282:15-28
@ ./src/main.js 3:0-30 46:4-18 59:0-17 74:2-8
webpack compiled with 1 error
默认情况下,Webpack 5不再包含Node.js核心模块的polyfills(就像你的例子中的os)。在您的项目中,该模块似乎正在尝试解析操作系统,但Webpack找不到它。
二、解决方法
1、安装os-browserify
使用npm
npm install os-browserify
或使用yarn
yarn add os-browserify
2、修改 vue.config.js
const path = require('path'); module.exports = { configureWebpack: { resolve: { fallback: { os: require.resolve('os-browserify/browser'), }, }, }, };
三、重新运行项目
使用npm
npm run serve
或使用yarn
yarn serve
文章评论(0)