babel: Couldn't find preset "@babel/env" relative to directory

Edit by @loganfsmyth

For others coming to this issue, please see my comment in https://github.com/babel/babel/issues/6838#issuecomment-345095252

Original Issue:

This is a bug

Latest @babel/core with @babel/env doesn’t work on windows. I’m using gulp-babel with simple .babelrc file

Babel/Babylon Configuration (.babelrc, package.json, cli command)

{
  "presets": [
    ["@babel/env", {
      "targets": {
        "browsers": ["last 2 versions", "safari >= 7"]
      },
      "useBuiltIns": "usage",
      "debug": true
    }]
  ]
}

Expected Behavior

I’m expecting the code to compile (transform) through babel.

Current Behavior

Instead I get following error:

Error: Couldn't find preset "@babel/env" relative to directory "PROJECT_DIR"
    at PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:293:19
    at Array.map (native)
    at OptionManager.resolvePresets (PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:275:20)
    at OptionManager.mergePresets (PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:264:10)
    at OptionManager.mergeOptions (PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:249:14)
    at OptionManager.init (PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:368:12)
    at compile (PROJECT_DIR\node_modules\babel-register\lib\node.js:103:45)
    at loader (PROJECT_DIR\node_modules\babel-register\lib\node.js:144:14)
    at Object.require.extensions.(anonymous function) [as .js] (D:\web\html\stels-html\node_modules\babel-register\lib\node.js:154:7)
    at Module.load (module.js:503:32)

If I change @babel/env in .babelrc to @babel/preset-env, I get:

TypeError: Cannot read property 'loose' of undefined (While processing preset: "PROJECT_DIR\\node_modules\\@babel\\preset-env\\lib\\index.js")
    at _default (PROJECT_DIR\node_modules\@babel\plugin-transform-modules-commonjs\lib\index.js:19:22)
    at Function.memoisePluginContainer (PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:113:13)
    at Function.normalisePlugin (PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:146:32)
    at PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:184:30
    at Array.map (native)
    at Function.normalisePlugins (PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:158:20)
    at OptionManager.mergeOptions (PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:234:36)
    at PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:265:14
    at PROJECT_DIR\node_modules\babel-core\lib\transformation\file\options\option-manager.js:323:22
    at Array.map (native)

Your Environment

software version(s)
Babel 7.0.0-beta.32 (both @babel/core and @babel/env)
gulp-babel 8.0.0-beta.0
node 8.1.3
npm 5.5.1
Operating System Windows 10 (1709)

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 42
  • Comments: 34 (8 by maintainers)

Most upvoted comments

changed the babel config to options: {presets: [‘env’]}

For users getting this error, it is 100% because something in your build is still running Babel 6. I understand that people want to use the beta, but do remember that we’re still beta for a reason. Not all of the tooling integration supports 7.x yet, and a lot of the time if they do, it’s via an opt-in @next version on npm, like babelify@next, not the version you’ll get installed by default. There also some tools that just don’t plan to update yet, like Ava, so on Ava for instance you cannot use babel: "inherit", otherwise it’ll load your Babel 7 config in Ava’s internal Babel 6 .

I am creating a Vue and hapi.js project. I am using Webpack as my bundler and nodemon to watch server changes with the babel-node command. I got this same error when I tried to install and configure highcharts-vue: Couldn't find preset "@babel/env" relative to directory.

This is what I had to do to get things working again:

Install these packages:

yarn add @babel/cli @babel/core @babel/preset-env @babel/node babel-cli@next babel-core@next babel-preset-env@next babel-loader@next

NOTE: Make sure that you do not have @babel/plugin-transform-runtime installed. That gave me a huge console error.

Then in your .babelrc file, change this:

{
  "presets": ["env"]
}

To this:

{
  "presets": ["@babel/env"]
}

It seems strange to me that I should have two versions of Babel installed for many of those packages, but I kept getting errors when I tried to uninstall one version or the other. I would like to understand why that is, so if someone knows please tell me.

Also, I was using babel-polyfill at the top of my server.js file (for hapi.js) so I could use ES6 imports and exports. After making the above changes, I kept getting the following server error: Error: only one instance of babel/polyfill is allowed. I commented out the babel-polyfill import and then I got errors related to the ES6 imports/exports. I could not figure out how to fix the only one instance of babel/polyfill is allowed error, so I removed the import "babel-polyfill" at the top of my server.js file and switched all of my imports/exports back to CommonJS syntax (e.g., require("moduleName"), module.exports = {}) and that fixed my problems.

I hope this helps!


I have some updates that might help to explain some things.

I think what is going on here is that you have to upgrade every npm package that is related to babel to at least version 7.0.0 (or at least as many packages as can be upgraded). However, the Babel 7.X.X versions (and beyond) use the newer namespaced versions (e.g., @babel/cli as opposed to babel-cli). This will require a few tweaks in some of your config files, like .babelrc (as I explained above). Many of the namespaced versions are already set to version 7.0.0 or higher, although some of those packages might still be in beta. The beta versions should be fine, though.

NOTE: Some Babel packages might not have a newer namespaced version, so you will have to do some research on how to use those older packages correctly.

After finding out which Babel packages have namespaced versions, remove the old packages like this:

yarn remove babel-cli

…and replace them with the new packages like this:

yarn add @babel/cli


NOTE: Some of the older versions that don’t yet have a newer, namespaced version can be installed like this:

yarn add babel-cli@^7.0.0

If you install an older version like that, then you might be asked to select a version. If so, just select the latest version from the list, even if it is a beta version.


Additional Notes:

  • If you use the babel-node command in your npm scripts for dev and production builds of a Node.js server: In Babel 7, the babel-node command comes from @babel/node instead of babel-cli. See the Upgrade to Babel 7 guide.
  • If you configure a production build for your Node.js server with Babel, then make sure to use the newer @babel/cli package.
  • You can use @babel/polyfill at the top of your server.js file, but you might have to use it like this to prevent build errors:
if (env === "production") {
  require("@babel/polyfill");
}

@with-a-k

{
  "presets": ["env"]
}

should be

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

Hey, thanks for the note. I should have mentioned, I am using babel 7 beta 44. it is installed in the local folder of the package in the lerna project

@Skaronator Your stack trace mentions serverless-offline which enables babel-register if you tell it to, so that would appear to be loading Babel 6, causing your issue.

Might be relevant: if you are using the register, do “@babel/register” (not “babel-register”).