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}`));
}
-
What is the expected behavior? See above. If this isn't experienced by anyone else, I will close the issue, but I want to know if anyone is seeing this with the suggested configuration for webpack 3.x | babel-loader 8.x | babel 7.x
-
What is the motivation / use case for changing the behavior? N/A
Sounds like you probably have a .babelrc that is still referencing babel-preset-env.
Should have added that there's no .babelrc. I could try outsourcing the configuration to a new one if that might fix it.
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
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?
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.
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?
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.
👍
"babel": {
"presets": [
[
"@babel/preset-env",
{
"modules": "commonjs",
"targets": {
"node": "current"
}
}
]
]
},
was just env
fixed the issue
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.
Make sure the loader config doesn't have a custom babel preset option.
Worked for me, had to change ['env'] to ['@babel/preset-env'] in .bablerc when upgrading from babel 6.x to 7.x - thanks!
@babel/preset-env
Love you man
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
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.
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
try npm install --save-dev @babel/preset-env
I had same issue and i solved it with removing env from my presets in options for babel-loader in the webpack
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" ] }
Sounds like you probably have a
.babelrcthat is still referencingbabel-preset-env.
What should be done instead so as to fix the issue?