Babel-loader: Cannot find module '@babel/preset-env'

Created on 21 Oct 2019  路  2Comments  路  Source: babel/babel-loader

Delete what isn't applicable below
I'm submitting a bug report

Webpack Version:
1.12.11

Babel Core Version:
7.6.4 (@babel/core)

Babel Loader Version:
8.0.6

Please tell us about your environment:
A docker container.

My webpack config contains:

        , {
          test: /\.js$/,
          exclude: /(node_modules|bower_components)/,
          loader: 'babel-loader',
          query: {
            presets: ['@babel/preset-env']
          }
        }

Env preset is installed as @babel/preset-env@^7.6.3:

# ls node_modules/@babel/preset-env
CONTRIBUTING.md  LICENSE  README.md  data  lib  node_modules  package.json

Current behavior:

Webpack/babel can't find preset module:

ERROR in /mstf/stf/res/app/app.js
Module build failed: Error: Cannot find module '@babel/preset-env' from '/'
    at Function.module.exports [as sync] (/mstf/stf/node_modules/resolve/lib/sync.js:40:15)
    at resolveStandardizedName (/mstf/stf/node_modules/@babel/core/lib/config/files/plugins.js:101:31)
    at resolvePreset (/mstf/stf/node_modules/@babel/core/lib/config/files/plugins.js:58:10)
    at loadPreset (/mstf/stf/node_modules/@babel/core/lib/config/files/plugins.js:77:20)
    ...etc ...etc

This happened after upgrading the following packages:

-    "babel-core": "^6.18.2",
-    "babel-loader": "^6.2.7",
-    "babel-preset-es2015": "^6.18.0",
+    "babel-loader": "^8.0.6",
+    "@babel/core": "^7.6.4",
+    "@babel/preset-env": "^7.6.3",

And changing preset from es2015 to @babel/preset-env

Expected/desired behavior:

Everything builds fine.

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.

If really necessary, I can try, but not sure that I can find time. Can this be happening because I'm upgrading everything except Webpack, and Webpack stays far, far behind?

Most helpful comment

@nicolo-ribaudo thanks a lot, that helped :)

Fixed it by doing:

         , {
           test: /\.js$/,
           exclude: /(node_modules|bower_components)/,
           loader: 'babel-loader',
           query: {
+            cwd: __dirname,
             presets: ['@babel/preset-env']
           }
         }

...as you suggested. process.cwd() === '/' in my case, apparently. Probably due to how Docker entrypoint is set up.

All 2 comments

How are you running webpack? Babel starts searching from process.cwd() by default.

If for some reason the cwd is wrong, you can pass it as an option to babel-loader (i.e. cwd: __dirname).

@nicolo-ribaudo thanks a lot, that helped :)

Fixed it by doing:

         , {
           test: /\.js$/,
           exclude: /(node_modules|bower_components)/,
           loader: 'babel-loader',
           query: {
+            cwd: __dirname,
             presets: ['@babel/preset-env']
           }
         }

...as you suggested. process.cwd() === '/' in my case, apparently. Probably due to how Docker entrypoint is set up.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  23Comments

NXTaar picture NXTaar  路  22Comments

ritz078 picture ritz078  路  29Comments

jesprider picture jesprider  路  34Comments

eoinmurray picture eoinmurray  路  20Comments