Javascript 错误:无法解析模块“babel-loader”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34538466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:32:01  来源:igfitidea点击:

Error: Cannot resolve module 'babel-loader'

javascriptnode.jsherokuwebpack

提问by Geraint

I'm trying to run webpack on my postinstall script in my package.json when I push to heroku but I am getting the following error.

当我推送到 heroku 时,我试图在我的 package.json 中的 postinstall 脚本上运行 webpack,但出现以下错误。

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

When I run the command locally I get no issues. Below is my webpack config - i have tried using resolveLoader to fix the resolving issue but to no avail?

当我在本地运行命令时,我没有遇到任何问题。下面是我的 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;

Any suggestions? Thanks

有什么建议?谢谢

回答by Geraint

I found out why. I didn't have babel or babel-core in my package.json. Add them fixed the error.

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

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

回答by Prakash Tiwari

In my case, I had mis-spelled the loader while installing it, so make sure you install

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

babel-loader

babel-loader

NOT

不是

bable-loader

装载机

回答by Hailin Tan

In my case, I tried the command:

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

$ npm install babel-loader --save

and continued to fix the rest based on the reminder from the console, and it fixed the issue:

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

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

“未找到输入模块中的错误:错误:无法解析‘babel-loader’”

回答by lucas

I'm using yarn and webpacker for a rails + react project.

我正在将 yarn 和 webpacker 用于 rails + react 项目。

I know not everyone can upgrade all their dependencies without breaking things, but for me, adding running yarn upgradefixed this error.

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

That was with only @babel/corein my dependenciesconfig, since babel-loaderis included as a dependency of webpacker.

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

回答by Evan

When using yarn 2, webpack 4 is unable to resolve the loader. or update to webpack 5

使用 yarn 2 时,webpack 4 无法解析加载器。或更新到 webpack 5

I had to use PnPify to make it work.

我不得不使用 PnPify 使其工作。

yarn pnpify webpack

回答by Robin Daugherty

In some cases, when deploying to production (for example with Rails Webpacker), dev dependencies are not loaded. So having babel-loader in devDependencieswill not work.

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

In fact, it makes sense that babel-loader would be placed in dependencies, not devDependencies, because it's used in the production code itself. The only packages that should be in devDependenciesare those that are run in development, such as tests and linters.

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

回答by Gio

I had mine in devDependenciesand it didn't work, I switched it to dependenciesand it finally worked!

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

回答by howdyhyber

I deleted the yarn.lock and node_modules folder then omit babel-loader in your devDependencies in package.json, then i rerun yarn, and it works.

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