繁体   English   中英

错误:无法解析模块“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

不是

装载机

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

$ npm install babel-loader --save

并根据控制台的提示继续修复其余部分,并修复了问题:

“找不到条目模块中的错误:错误:无法解析'babel-loader'”

在处理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 节点包就可以工作。

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

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

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

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

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

我在devDependencies中有我的但它没有用,我将它切换到依赖项并且它终于起作用了!

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

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

我不得不使用 PnPify 来让它工作。

yarn pnpify webpack

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM