Error: Cannot resolve module 'babel-loader'

Error: Cannot resolve module 'babel-loader'

当我推送到 heroku 时,我正在尝试 运行 在我的 package.json 安装后脚本中使用 webpack,但出现以下错误。

ERROR in Entry module not found: Error: Cannot resolve module 'babel-loader' in /tmp/build_6cb4b10367d9382367ab72f2e2f33118

当我在本地 运行 命令时,我没有遇到任何问题。下面是我的 webpack 配置 - 我已经尝试使用 resolveLoader 来解决解决问题但无济于事?

var path = require('path');
var webpack = require('webpack');

var config = {
  entry: path.resolve(__dirname, './app/main.js'),
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader'
      },
      {
        test: /\.less$/,
        loader: 'style!css!less'
      }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx', '.less'],
    modulesDirectories: [
      'node_modules'
    ]
  },
  resolveLoader: {
    root: path.resolve(__dirname, 'node_modules')
  },
  plugins: [
    new webpack.optimize.UglifyJsPlugin({minimize: true})
  ]
};

module.exports = config;

有什么建议吗?谢谢

我找到原因了。我的 package.json 中没有 babel 或 babel-core。添加它们修复了错误。

  "devDependencies": {
    "babel": "^5.8.23",
    "babel-core": "^5.0.0",
    "babel-loader": "^5.3.2"
}

就我而言,我在安装加载程序时拼写错误,所以请确保安装

babel-loader

没有

bable-loader

就我而言,我尝试了命令:

$ npm install babel-loader --save

并根据控制台提示继续修复,问题已解决:

"ERROR in Entry module not found: Error: Can't resolve 'babel-loader'"

在某些情况下,当部署到生产环境时(例如使用 Rails Webpacker),不会加载开发依赖项。所以在 devDependencies 中安装 babel-loader 是行不通的。

事实上,将 babel-loader 放在 dependencies 而不是 devDependencies 中是有道理的,因为它在生产代码本身中使用。唯一应该在 devDependencies 中的包是那些 运行 正在开发中的包,例如测试和 linters。

我在 devDependencies 中有我的,但它不起作用,我将它切换到 dependencies,它终于起作用了!

我删除了 yarn.lock 和 node_modules 文件夹,然后在 package.json 的 devDependencies 中省略了 babel-loader,然后我重新运行 yarn,它起作用了。

我在 rails + React 项目中使用 yarn 和 webpacker。

我知道不是每个人都能在不破坏任何东西的情况下升级所有依赖项,但对我来说,添加 运行 yarn upgrade 修复了这个错误。

我的 dependencies 配置中只有 @babel/core,因为 babel-loader 作为 webpacker 的依赖项包含在内。

当使用 yarn 2 时,webpack 4 无法解析 loader。或者更新到 webpack 5

我必须使用 PnPify 才能让它工作。

yarn pnpify webpack

我在处理 Rails 6 应用程序时遇到了类似的错误。

我认为问题是 Babel-loader 节点包未正确安装或应用程序无法找到可执行文件。

我所要做的就是通过 运行:

升级应用程序中的节点包
yarn upgrade

这是我的 package.json 文件中的 devDependencies

"devDependencies": {
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0

注意:我不必在 devDependencies 列表中包含 Babel-loader 节点包就可以工作。

在我的例子中,react-scripts 导入了 babel-loader 作为依赖项。它工作了一段时间,因为它在 package-lock.json 中。如果您的部门中有 react-scripts,请尝试删除 node_modules、package-lock.json 并再次执行 npm install。