当前位置: 首页 > 工具软件 > Require > 使用案例 >

npm报错Cannot find module ‘webpack‘ Require stack

庄飞
2023-12-01


一、问题描述

执行 yarn build 时报错:

 ERROR  Error: Cannot find module 'webpack'
        Require stack:
        - /home/source/node_modules/compression-webpack-plugin/dist/index.js
        - /home/source/node_modules/compression-webpack-plugin/dist/cjs.js
        - /home/source/shop-ui/manager/vue.config.js
        - /usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/node_modules/@vue/cli-shared-utils/lib/module.js
        - /usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/node_modules/@vue/cli-shared-utils/index.js
        - /usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/bin/vue-cli-service.js

二、解决方法

1.尝试以下方法不行

(1)全局安装 webpack

npm install  --g webpack
npm install  --g webpack-cli

(2)项目下安装 webpack

npm install  --save-dev webpack
npm install  --save-dev webpack-cli

(3)npm link webpack

npm link webpack

2.按第 1 点方法尝试出现的问题

(1)Cannot find module ‘@vue/cli-plugin-babel’

Error: Cannot find module '@vue/cli-plugin-babel'
Require stack:
- /usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js
- /usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/bin/vue-cli-service.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at idToPlugin (/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js:172:14)
    at /usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js:211:20
    at Array.map (<anonymous>)
    at Service.resolvePlugins (/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js:198:10)
    at new Service (/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js:35:25)
    at Object.<anonymous> (/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/bin/vue-cli-service.js:15:17) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js',
    '/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/bin/vue-cli-service.js'
  ]
}
error Command failed with exit code 1.

执行 npm install @vue/cli-plugin-babel --save-dev 又报第(2)点错误。

(2)Cannot find module ‘vue-cli-plugin-style-resources-loader’

Error: Cannot find module 'vue-cli-plugin-style-resources-loader'
Require stack:
- /usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js
- /usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/bin/vue-cli-service.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at idToPlugin (/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js:172:14)
    at /usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js:211:20
    at Array.map (<anonymous>)
    at Service.resolvePlugins (/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js:198:10)
    at new Service (/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js:35:25)
    at Object.<anonymous> (/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/bin/vue-cli-service.js:15:17) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/lib/Service.js',
    '/usr/local/node-v14.18.2-linux-x64/lib/node_modules/@vue/cli-service/bin/vue-cli-service.js'
  ]
}
error Command failed with exit code 1.

执行 npm install vue-cli-plugin-style-resources-loader --save-dev 又重复报第(1)点错误!!!

3.解决

环境变量问题!!将环境变量 NODE_ENV 设置为 production 导致的问题!!
将环境变量 NODE_ENV 设置为 production 后,所有的项目都会处于正式环境中。
此时使用 yarn build 下载依赖包时,只会把 package.json 中的 dependencies 依赖项下载下来,而不会下载 devDependencies 中的依赖包。
解决方法是执行命令 unset NODE_ENV 删除设置的环境变量,再运行 yarn build 命令。

 类似资料: