Error: Cannot resolve module 'babel-loader'

只愿长相守 提交于 2020-01-12 06:25:31

问题


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.

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?

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


回答1:


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

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



回答2:


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

babel-loader

NOT

bable-loader




回答3:


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'"




回答4:


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

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

That was with only @babel/core in my dependencies config, since babel-loader is included as a dependency of webpacker.




回答5:


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

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 devDependencies are those that are run in development, such as tests and linters.




回答6:


I had mine in devDependencies and it didn't work, I switched it to dependencies and it finally worked!




回答7:


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.



来源:https://stackoverflow.com/questions/34538466/error-cannot-resolve-module-babel-loader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!