Skip to content
This repository has been archived by the owner on May 11, 2018. It is now read-only.

Couldn't find preset "env" relative to directory #186

Closed
adlk opened this issue Mar 3, 2017 · 36 comments
Closed

Couldn't find preset "env" relative to directory #186

adlk opened this issue Mar 3, 2017 · 36 comments

Comments

@adlk
Copy link

adlk commented Mar 3, 2017

I'm working with the electron-forge setup and I'm trying to dynamically require an external node_module that is located in /Users/name/plugin Unfortunately babel-preset-env is trying to resolve the preset relative to the required module instead of the local module.

Console output when trying to require the module

Error: Couldn't find preset "env" relative to directory "/Users/name/plugin/7ada6103-7315-4c24-93f3-4a3a6a0fa7ee"
    at /Users/name/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/name/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/Users/name/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/Users/name/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/Users/name/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/name/project/node_modules/babel-core/lib/transformation/file/index.js:216:65)
    at new File (/Users/name/project/node_modules/babel-core/lib/transformation/file/index.js:139:24)
    at Pipeline.transform (/Users/name/project/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at BabelCompiler.compileSync (/Users/name/project/node_modules/electron-compilers/lib/js/babel.js:76:26)

contents of .compilerc

{
  "env": {
    "development": {
      "application/javascript": {
        "presets": [
          [
            "env",
            {
              "targets": {
                "electron": "1.4.15"
              }
            }
          ],
          "react"
        ],
        "plugins": [
          "transform-decorators-legacy",
          "transform-flow-strip-types",
          "transform-class-properties",
          "transform-async-to-generator"
        ],
        "sourceMaps": "inline"
      }
    },
    "production": {
      "application/javascript": {
        "presets": [
          [
            "env",
            {
              "targets": {
                "electron": "1.4.15"
              }
            }
          ],
          "react"
        ],
        "plugins": [
          "transform-decorators-legacy",
          "transform-flow-strip-types",
          "transform-class-properties",
          "transform-async-to-generator"
        ],
        "sourceMaps": "none"
      }
    }
  }
}

How to test this issue:
Use a clean project (electron-forge init) and try to import any random module from a folder that is not within module.paths

@JPeer264
Copy link

JPeer264 commented Mar 7, 2017

@adlk which babel-core version do you use?

@loganfsmyth
Copy link
Member

It sounds like you have no installed babel-preset-env?

@JPeer264
Copy link

JPeer264 commented Mar 7, 2017

@loganfsmyth I got this issue earlier today using gulp-babel which uses an older babel-core version. I installed a newer version with npm i babel-core -D and it was solved. Maybe @adlk got the same

@nashwaan
Copy link

As suggested by @loganfsmyth installing babel-preset-env worked for me.

@existentialism
Copy link
Member

existentialism commented Mar 12, 2017

Closing for now as there doesn't look like there's anything actionable.

@JPeer264 and @nashwaan please let us know if you run into any more issues related to babel-preset-env!

EDIT: Actually, will wait until OP responds.

@adlk
Copy link
Author

adlk commented Mar 12, 2017

@JPeer264 tried it with the latest version of babel-core
@nashwaan was using babel-preset-envv1.1.8 and upgraded to 1.2.1 but this didn't do the trick either.

it's not the greatest workaround but it works for now if I use absolute paths in my compilerc for my presets:

"presets": [
  [
    "/Users/username/project/node_modules/babel-preset-env",
    {
      "targets": {
        "electron": "1.4.15"
      }
    }
  ],
   "/Users/username/project/node_modules/babel-preset-react"
],

@JPeer264
Copy link

@adlk hm strange - are you using babel as cli?

@adlk
Copy link
Author

adlk commented Mar 13, 2017

@JPeer264 yup

@justechn
Copy link

justechn commented Mar 13, 2017

I seem to be having the same problem. I am also using electron-forge and I am trying to include a package that is not in node_modules. I have tried all the above suggestions and the only thing that works is what @adlk suggested. I looked in node_modules/babel-core/package.json and it says "version": "6.24.0"

also happens if I npm link to a repo that is not in the path. Instead of using npm link I have to manually copy the external module into my node_modules directory to get it to work.

@trusktr
Copy link

trusktr commented Mar 19, 2017

I can confirm that I get this problem when using npm v2, but no problems when using v4.

duplicate: #219

You can reproduce with this simple babelrc:

{
  "presets": [
      "env"
  ],
  "plugins": [
      "transform-es2015-modules-umd"
  ]
}

make a simple js file in src/app.js then run the following:

npm install -g "npm@^2"
npm install babel-cli babel-preset-env babel-plugin-transform-es2015-modules-umd
./node_modules/.bin/babel src -d dist

That will cause the error.

Then, if you update to npm v4, the error will go away.

@adlk
Copy link
Author

adlk commented Mar 19, 2017

@trusktr hmm that's weird. I'm running npm 4.1.2

@trusktr
Copy link

trusktr commented Mar 19, 2017

Same error for me, same line and column number, when using npm v2.

Here's a physical repro: https://github.com/trusktr/tween.js/tree/ddba437d51b8af9d4f1b84dec4ca4533e0a4d09e

You can clone that and checkout that commit, then just run npm install with npm v2 which eventually triggers the error.

@trusktr
Copy link

trusktr commented Mar 19, 2017

@adlk I'm not getting the error with npm v4.4

@trusktr
Copy link

trusktr commented Mar 19, 2017

@adlk Hmmm, something is fishy, I was on npm v4, and I was able to run babel just fine with no errors just a moment ago, but now I can not. Investigating...

@adlk
Copy link
Author

adlk commented Mar 19, 2017

Yep, I've just updated to the latest npm version and I'm still getting the error :(

@trusktr
Copy link

trusktr commented Mar 19, 2017

Oh, oops, I accidentally deleted babel-preset-envfrom devDependencies.

So, it is working for me with npm v4.4 and a fresh install.

I just double checked (each time I deleted node_modules, and ran npm install, which runs babel, see my previous link), and it works every time with npm v4.4.

@adlk
Copy link
Author

adlk commented Mar 19, 2017

Great! I'll do some tests and will get back to you asap.

@trusktr
Copy link

trusktr commented Mar 19, 2017

Ah, false alarm, I forgot to use --save-dev when I originally installed babel-plugin-env with npm v4, then when I removed node_modules and switched to npm v2, it failed, so I thought it was because of npm v2. Oops!

So, it was due to the package actually missing. It works for me on npm v2 and v4 right now.

I would be glad to check yours out. Can you can push it somewhere?

@ericnkatz
Copy link

ericnkatz commented Mar 23, 2017

Running into the same issue for my project where I have other modules npm linked. Same issue if I just tell webpack to resolve them from their project folders directly. npm 4.1.2

@ericnkatz
Copy link

ericnkatz commented Mar 23, 2017

@yavorsky - #219 (comment) ,
So this worked for me in the individual packages: npm install babel-preset-env

@yavorsky
Copy link
Member

yavorsky commented Mar 23, 2017

@ericnkatz Good! But did it work for you just with babel-preset-env in dependencies in packge.json and then npm install?

@adlk Did you have success with changes according to #186 (comment)?

@ericnkatz
Copy link

ericnkatz commented Mar 23, 2017

@yavorsky in my case the package does not actually use babel at all so I don't add it to the package.json it is a module I've forked but want to maintain in the source of my existing project until I can PR back to its original and then get rid of the custom tailoring I've done while in development.

For clarity

  • Primary Project -> node_modules (uses babel/env-preset)
  • Forked Module (doesn't use babel) -> webpack resolve -> exits w/ "Couldn't find preset "env" relative to directory"

Solution was to cd to Forked Module and install babel-env-preset even though the module itself doesn't need or use it (although for team members I may have to add this to package.json and just remember to remove when I PR back)

@ThomasBrierley
Copy link

This is annoying

I'm using babel as part of a build system that can be used either globally or locally, as a result of this I have to do more messing around and construct the node_modules path for babel:

const mod = (__dirname.includes(process.cwd()) ? process.cwd() : __dirname) + '/node_modules/';
// ...
.pipe(plugin.babel({presets: [[mod + 'babel-preset-env']]}))

Does anyone have an explanation for how this makes sense? plugins are probably always node modules, why make them relative to the input source? and for the cases when they aren't source relative paths could still be checked.

@loganfsmyth
Copy link
Member

In Babel 7.x, plugins and presets will be resolved relative to the cwd rather than relative to the file. We agree the current behavior is not something anyone wants.

@ThomasBrierley
Copy link

Excellent, good news, thanks @loganfsmyth 😄

@xtuc
Copy link
Member

xtuc commented Apr 28, 2017

@ThomasBrierley do you mind if we close this? This breaking change is also in the upgrade guide.

@ThomasBrierley
Copy link

I'd say it's ok to close, however @adlk originated this issue not me.

@NorikDavtian
Copy link

came to the same issue because of the documentation for the CLI option
http://babeljs.io/docs/setup/#installation
should be
npm install --save-dev babel-cli babel-preset-env

is missing the babel-preset-env

@mo
Copy link

mo commented Jul 23, 2017

I ran into the problem as well, everything was working fine when I were developing on project X normally. However, as soon as I did "npm link foo" inside X, then the build would fail there with the error:

Module build failed: Error: Couldn't find preset "es2015" relative to directory "/path/to/project/foo"

The workaround that fixed it for me was to install all presets from "project X" into project "foo" (without --save or changing the package.json). In my case those were:

npm install babel-preset-es2015 babel-preset-react babel-preset-react-hmre

So I didn't even have babel-preset-env install in any of my project; the the bug is in babel itself. Hopefully it will be fixed in babel7 like @loganfsmyth said.

@mo
Copy link

mo commented Jul 23, 2017

The cleanest workaround to this problem is probably to prefix all the presets with "babel-preset-" and then do .map(require.resolve) on the array directly in the webpack config:

       {
         test: /\.js$/,
         loader: 'babel-loader',
         exclude: /node_modules/,
         options: {
-          presets: ['es2015', 'react', 'react-hmre']
+          presets: [
+            'babel-preset-es2015',
+            'babel-preset-react',
+            'babel-preset-react-hmre'
+          ].map(require.resolve)
         },

This is explained here:

@5265liu
Copy link

5265liu commented Jul 28, 2017

@loganfsmyth thankyou your answers is very helpful for me;my english is very poor ; haha

contolini added a commit to cfpb/cfpb-chart-builder that referenced this issue Aug 8, 2017
Along with fixing the entry point path, babel-preset-env has to be
installed in order for `npm link` to work with cfgov-refresh due to
the below babel bug.

babel/babel-preset-env#186
@hzoo
Copy link
Member

hzoo commented Aug 29, 2017

make sure you clear node_modules, use a later npm version. I'm having a really hard time understanding all these issues/errors so closing (make a new issue, or better just ask in slack)

npm install --save-dev babel-cli babel-preset-env

@hzoo hzoo closed this as completed Aug 29, 2017
@yhnbgfd
Copy link

yhnbgfd commented Sep 4, 2017

It works for me,modify file ".babelrc":
【Old】
{
"presets": [
["env", { "modules": false }]
]
}
【New】
{
"presets": [
["latest", {
"es2015": { "modules": false }
}]
]
}

@ltfschoen
Copy link

ltfschoen commented Oct 12, 2017

I resolved this error by first creating a .babelrc file with the following contents in the project root folder:

{
  "presets": [
    [
      "env",  { "modules": false }
    ]
  ]
}

I then ran the following command to install this development dependency and save it to the package.json

npm install --save-dev babel-preset-env

@zanona
Copy link

zanona commented Oct 25, 2017

This is still an issue when using babelify with the option --global-transform rather than --transform it will try to search for the preset relative to the nested module being converted.
This will happen when you link a module that's located elsewhere locally and not flattened under /node_modules

Cross-reference: babel/babelify#134

@rwakos
Copy link

rwakos commented Oct 27, 2017

Run:
npm update
This worked for me!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests