如何修复 webpack 依赖错误

Posted

技术标签:

【中文标题】如何修复 webpack 依赖错误【英文标题】:How to fix webpack dependency errors 【发布时间】:2022-01-21 12:34:44 【问题描述】:

我一直在尝试升级节点版本,并且有点状态。我试过多次删除 node_modules 并重新安装、更新等。这是怎么回事?

ERROR in ./src/app/index.jsx
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: [BABEL]: Cannot find module '../lib/statuses'
Require stack:
- /var/app/node_modules/caniuse-lite/dist/unpacker/feature.js
- /var/app/node_modules/browserslist/node.js
- /var/app/node_modules/browserslist/index.js
- /var/app/node_modules/@babel/helper-compilation-targets/lib/index.js
- /var/app/node_modules/@babel/preset-env/lib/debug.js
- /var/app/node_modules/@babel/preset-env/lib/index.js
- /var/app/node_modules/@babel/core/lib/config/files/module-types.js
- /var/app/node_modules/@babel/core/lib/config/files/configuration.js
- /var/app/node_modules/@babel/core/lib/config/files/index.js
- /var/app/node_modules/@babel/core/lib/index.js
- /var/app/node_modules/babel-loader/lib/index.js
- /home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/webpack/node_modules/loader-runner/lib/loadLoader.js
- /home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/webpack/node_modules/loader-runner/lib/LoaderRunner.js
- /home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/webpack/lib/NormalModuleFactory.js
- /home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/webpack/lib/Compiler.js
- /home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/webpack/lib/webpack.js
- /home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/webpack/lib/index.js
- /home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/webpack-cli/lib/webpack-cli.js
- /home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/webpack-cli/lib/bootstrap.js
- /home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/webpack-cli/bin/cli.js
- /home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/webpack/bin/webpack.js (While processing: /var/app/node_modules/@babel/preset-env/lib/index.js)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/var/app/node_modules/caniuse-lite/dist/unpacker/feature.js:3:18)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)

webpack 5.65.0 compiled with 1 error in 15668 ms

编辑 - 似乎这不是我唯一的问题 :( 不确定是否相关:

 Error: Cannot find module 'core-js/library/fn/object/get-prototype-of'
 Require stack:
 - /var/app/node_modules/babel-runtime/core-js/object/get-prototype-of.js
 - /var/app/node_modules/fb/lib/index.js
 - /var/app/src/controllers/app.js
 - /var/app/src/index.js
     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
     at Module.Hook._require.Module.require (/home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:61:29)
     at require (node:internal/modules/cjs/helpers:102:18)
     at Object.<anonymous> (/var/app/node_modules/babel-runtime/core-js/object/get-prototype-of.js:1:31)
     at Module._compile (node:internal/modules/cjs/loader:1101:14)
     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
     at Module.load (node:internal/modules/cjs/loader:981:32)
     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
     at Module.require (node:internal/modules/cjs/loader:1005:19)
     at Module.Hook._require.Module.require (/home/app_admin/.nvm/versions/node/v16.13.1/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:80:39) 
   code: 'MODULE_NOT_FOUND',
   requireStack: [
     '/var/app/node_modules/babel-runtime/core-js/object/get-prototype-of.js',
     '/var/app/node_modules/fb/lib/index.js',
     '/var/app/src/controllers/app.js',
     '/var/app/src/index.js'
   ]

不完全确定还要添加什么 - 这些是我遇到的错误

【问题讨论】:

当您删除 node_modules 并尝试完全卸载时,同时删除 package-lock.json 试过了。然后它说找不到aws-sdk(它在那里,并且在package.json中。所以我安装了它。然后“锐利”图像工具开始播放。所以我修复了它。然后node-libcurl ... 全局安装可能存在某种冲突/干扰。例如。你已经全局安装了 webpack,并且在你自己的 package.json 中也有一个依赖项 知道我应该怎么做吗?我尝试在全局和本地删除,然后仅在本地或全局安装。甚至尝试了 npm install statuses 看看是否有帮助!似乎没有什么可以解决对 ../lib/statuses 的需求 /var/app/node_modules/caniuse-lite/dist/lib 文件夹是否存在,是否存在名为 statuses.js 的文件是吗? 【参考方案1】:

好的,这就是我最后所做的,似乎解决了它:

npm uninstall --save-dev webpack webpack-cli
npm install --save caniuse-lite
npm install -g webpack@latest webpack-cli@latest
npm install -D babel-loader @babel/core @babel/preset-env @babel/preset-react webpack

然后另外编辑 .babelrc 以删除旧的 es2015 预设并最终得到:


    "presets": ["@babel/preset-env", "@babel/preset-react"],
    "plugins": [["@babel/plugin-proposal-class-properties", ]]

感谢@tromgy 的帮助以及此链接: Cannot find module babel-preset-es2015

【讨论】:

以上是关于如何修复 webpack 依赖错误的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 npm-start 解决 react webpack 问题

在代码中包含 @popperjs/core 和 eslint 时如何修复意外的 Webpack 错误

如何修复material-kit-react'找不到模块:webpack loader错误'

如何修复错误“找不到此相关模块:* ./src/main.js in multi (webpack)-dev-server”在 npm run serve in vue3.x with typescr

如何使用 Webpack 3 配置 Ant Design

Vue 3 和 webpack 5 - 错误:“模块属性已从依赖项中删除”