babel-loader: Cannot find module 'babel-preset-env'

I’m submitting a bug report

Webpack Version: 3.12

Babel Core Version: 7.0.0-beta.46 (also “@babel/preset-env”: “^7.0.0-beta.46”)

Babel Loader Version: 8.0.0-beta.2

Please tell us about your environment: Windows 10

Current behavior: Error: “Cannot find module ‘babel-preset-env’ from (root of my project)”

Expected/desired behavior:

         use: [{
            loader: 'babel-loader',
            options: {
              presets: [
                ['@babel/preset-env', { modules: false }]
              ]
            }
          }]

should load correctly, given the loader and babel versions

  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem along with a gist/jsbin of your webpack configuration.

Snippet. My config is very basic, otherwise

module: {
      rules: [
        {
          test: /\.js$/,
          exclude: resolve('node_modules'),
          use: [{
            loader: 'babel-loader',
            options: {
              presets: [
                ['@babel/preset-env']
              ]
            }
          }]
        }
      ]
    }

Webpack being called from a node script in this way:

const webpackConfig = require('./webpack.config.babel')('dev');

function webpackInit() {
  const compiler = webpack(webpackConfig);
  return new Promise((resolve, reject) => {
    compiler.run((err, stats) => {
      console.log(`compiler has been started`);
      if (err || stats.hasErrors()) {
        const _err = err ? err : JSON.stringify(stats.toJson('errors-only'));
        reject(_err);
      }
      resolve('webpack has been started');
    });
  })
    .then(resp => console.log(response))
    .catch(e => console.error(`Error : ${e}`));
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 9
  • Comments: 20 (3 by maintainers)

Most upvoted comments

Sounds like you probably have a .babelrc that is still referencing babel-preset-env.

"babel": {
    "presets": [
      [
        "@babel/preset-env",
        {
          "modules": "commonjs",
          "targets": {
            "node": "current"
          }
        }
      ]
    ]
  },

was just env

fixed the issue

Worked for me, had to change ['env'] to ['@babel/preset-env'] in .bablerc when upgrading from babel 6.x to 7.x - thanks!

Oh, now I’ve found it, and you’re right, it’s a reference to env, not babel-preset-env (hence not found in my code searches). It’s in a holdover from an old package.json, so I need to update that configuration. Will close this for now, assuming that will fix it. Thanks for your help.

If you’re sure you don’t have a .babelrc, maybe you have a package.json with a "babel" field with configuration data in it?

Should have added that there’s no .babelrc. I could try outsourcing the configuration to a new one if that might fix it.

👍

In case it helps anyone else: I was updating to the latest babel and jest when I ran into this error. Changing this in package.json (I have a test item in the scripts section):

"babel": {
  "env": {
    "test": {
      "presets": [
        "env"
      ]
    }
  }
},

to:

"babel": {
  "env": {
    "test": {
      "presets": [
        "@babel/preset-env"
      ]
    }
  }
},

fixed the issue for me.

try npm install --save-dev @babel/preset-env

My problem was solved, thank you , Sir. Change [‘env’] to [‘@babel/preset-env’] in the .babelrc file, I don’t konw why ,but it’s working. @loganfsmyth

I had same issue and i solved it with removing env from my presets in options for babel-loader in the webpack

Tried that change and also changing the env param passed to my webpack module exports function to be environment, but doesn’t fix it. I have no symlinked node_modules AFAIK.

Can you change

      exclude: resolve('node_modules'),

to

      exclude: /node_modules/,

to see if that helps? If it doesn’t, there must be something in your code still referencing either “babel-preset-env” or maybe just “env”. Alternatively, do you have any other packages connected in with symlinks into node_modules?