关于运行vue项目报错Module not found: Error: Can't resolve 'os' 问题解决方法

2天前   阅读:92   类型:前端   分类:Vue    标签: node.js Vue

一、问题原因

运行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


【腾讯云】618年中盛惠,2核2G5M云服务器低至 68元/年

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://tpxhm.com/fdetail/1148.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×