babel-loader icon indicating copy to clipboard operation
babel-loader copied to clipboard

Cannot find module 'babel-preset-env'

Open Josh68 opened this issue 8 years ago • 20 comments

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}`));
}

Josh68 avatar May 02 '18 19:05 Josh68

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

loganfsmyth avatar May 02 '18 20:05 loganfsmyth

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

Josh68 avatar May 02 '18 20:05 Josh68

Oh, I just found a node_modules reference to it in caniuse-lite, which is the one project where I already found an issue referencing this problem

Josh68 avatar May 02 '18 20:05 Josh68

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?

loganfsmyth avatar May 02 '18 20:05 loganfsmyth

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.

Josh68 avatar May 02 '18 20:05 Josh68

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?

loganfsmyth avatar May 02 '18 20:05 loganfsmyth

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.

Josh68 avatar May 02 '18 20:05 Josh68

👍

Josh68 avatar May 02 '18 20:05 Josh68

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

was just env

fixed the issue

Josh68 avatar May 02 '18 20:05 Josh68

I was following the docs here https://github.com/babel/karma-babel-preprocessor which set the preset to env, i.e. in my karma.conf.js:

babelPreprocessor: {
            options: {
                presets: ['env'],
                sourceMap: 'inline',
            },
            filename: function (file) {
                return file.originalPath.replace(/\.js$/, '.es5.js')
            },
            sourceFileName: function (file) {
                return file.originalPath;
            },
        },

This would throw the error, changing the ['env'] to ['@babel/present-env'] fixed it, i.e.:

babelPreprocessor: {
            options: {
                presets: ['@babel/preset-env'],
                sourceMap: 'inline',
            },
            filename: function (file) {
                return file.originalPath.replace(/\.js$/, '.es5.js')
            },
            sourceFileName: function (file) {
                return file.originalPath;
            },
        },

For some reason I had a lot of trouble finding that.

kerrermanisNL avatar Dec 13 '18 13:12 kerrermanisNL

Make sure the loader config doesn't have a custom babel preset option.

towry avatar Dec 28 '18 02:12 towry

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

lkulbacki avatar Jan 26 '19 14:01 lkulbacki

@babel/preset-env

Love you man

latitudecol avatar Apr 03 '19 02:04 latitudecol

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

itCaesar avatar Apr 10 '19 02:04 itCaesar

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.

fwextensions avatar May 13 '19 02:05 fwextensions

module.exports = function (api) {
  api.cache.using(() => process.env.NODE_ENV === "test")

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

Still not wokring in docker container

sinneren avatar Oct 18 '19 14:10 sinneren

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

kishorpatel85 avatar Aug 21 '20 11:08 kishorpatel85

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

tmohammad78 avatar Aug 26 '20 19:08 tmohammad78

It Worked for me, I had to change ['env'] to ['@babel/preset-env'] in .bablerc when upgrading from babel 6.x to 7.x File >>> .bablerc { "presets": [ "@babel/preset-env", "@babel/preset-react" ] }

ashishsiddhu avatar Jul 03 '21 22:07 ashishsiddhu

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

What should be done instead so as to fix the issue?

Ndibale avatar Jan 10 '23 19:01 Ndibale