28

So far I have been unsuccessful in an attempt to use the env preset. I browsed the git issues that others have raised such as this, and implemented some of the suggestions that appeared to work for others, but no luck so far.

Here's what I'm working with:

package.json

"bundle": "browserify ./client/app.js -d -o ./public/o.js -t [ babelify --presets [ env ] ]",
"devDependencies": {
  "@babel/cli": "^7.0.0-beta.40",
  "@babel/preset-env": "^7.0.0-beta.40",
  "babel-core": "^6.26.0",
  "babelify": "^8.0.0"
...

And here is the error:

Error: Couldn't find preset "env" relative to directory "/Users/user/Documents/git/ts/client" while parsing file: /Users/user/Documents/git/ts/client/app.js

Anything I could be missing?

Machavity
  • 30,841
  • 27
  • 92
  • 100
skwny
  • 2,930
  • 4
  • 25
  • 45

5 Answers5

37

Many of the github issues relating to this suggest that babel-preset-env is not installed.

Indeed it doesn't seem to be there in your package.json. Add & install it by doing this:

npm install babel-preset-env --save

Phil
  • 2,238
  • 2
  • 23
  • 34
  • 1
    Hard to tell what the issue actually was but the preset-env is installed. I did not, however, have `@babel/core": "^7.0.0-beta.40` installed. After installing that and using babel (instead of babelify), it started working. – skwny Feb 23 '18 at 18:30
9

Though the earlier answers do provide the right solution and it works. But this should be installed as a dev dependency not as the core one. As this is needed only for development work. If you are using npm you can use:

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

or if you are using yarn as package manager then use:

yarn add --dev babel-preset-env

Manit
  • 1,087
  • 11
  • 17
5

You just need to install babel-preset-env and your code will compile properly.

npm install babel-preset-env

Marten
  • 1,376
  • 5
  • 28
  • 49
2

Basically, this type of error occurs due to mismatch in versions of babel-core, babel-preset-env and babel-loader. Below are matched and tested devDependencies, which worked for me.

"devDependencies": {
    "@babel/core": "^7.11.6",
    "@babel/preset-env": "^7.11.5",
    "babel-loader": "^8.1.0",
    "html-webpack-plugin": "^4.5.0",
    "install": "^0.13.0",
    "npm": "^6.14.8",
    "regenerator-runtime": "^0.13.7",
    "webpack": "^4.44.2",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  },
  "dependencies": {
    "babel-polyfill": "^6.26.0"
  }
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
RHimanshu
  • 41
  • 4
-1

You can check whether .babelrc file exists in the directory above or above the directory. Just delete the file is OK

KevinSu
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 14 '22 at 19:19