Error: Couldn't find preset “es2015” relative to directory “/Users/username”

前端 未结 12 1445
孤城傲影
孤城傲影 2020-12-13 16:59

I get the following error when trying to use gulp-babel:

Error: Couldn\'t find preset \"es2015\" relative to directory \"/Users/username\"

相关标签:
12条回答
  • 2020-12-13 17:15

    the "es2015" in :

        .pipe(babel({
            presets: ['es2015']
        }))
    

    is actually a path - so if you don't have the preset in the /Users/username/es2015 directory you have to point exactly to it like for instance:

    .pipe(babel({
        presets: ['../../gulp/node_modules/babel-preset-es2015']
    }))
    

    it worked for me

    0 讨论(0)
  • 2020-12-13 17:16

    The situation where I encounter this problem is that I have moved the files from xxx to xxx/server. And then under xxx/server I will see the Error: Couldn't find preset "es2015" relative to directory "/Users/username/xxx" error. The real reason is that I have forgotten to move that .babelrc file under xxx. And after I move that .babelrc to xxx/server, the error goes away.

    0 讨论(0)
  • 2020-12-13 17:20

    To fix this issue You should remove .babelrc (hidden) file from "/Users/username" directory.

    0 讨论(0)
  • 2020-12-13 17:20

    Babel 7 update

    From the docs you should now use @babel/preset-env instead of any other preset mention

    The "env" preset has been out for more than a year now, and completely replaces some of the presets we've had/suggested earlier.

    • babel-preset-es2015
    • babel-preset-es2016
    • babel-preset-es2017
    • babel-preset-latest
    • A combination of the above ^
    yarn add @babel/preset-env
    

    or

    npm install @babel/preset-env
    
    0 讨论(0)
  • 2020-12-13 17:21

    Check if you have .babelrc file in the root folder of your Project. If not create .babelrc file and add the following:

    {
      "presets": ["es2015"]
    }
    

    It fixed the issue.

    0 讨论(0)
  • 2020-12-13 17:25

    You could try installing es2015 and stage-2 via

    npm i babel-preset-es2015 --save
    npm i babel-preset-stage-2 --save
    
    0 讨论(0)
提交回复
热议问题