Javascript 错误:找不到相对于目录“/Users/username”的预设“es2015”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34819473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:54:08  来源:igfitidea点击:

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

javascriptnode.jsecmascript-6gulpbabeljs

提问by Brian Douglas

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

尝试使用 gulp-babel 时出现以下错误:

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

错误:找不到相对于目录“/Users/username”的预设“es2015”

I have the es2015 preset installed globally and locally so can't see why this would be an issue.

我在全球和本地都安装了 es2015 预设,所以不明白为什么会出现这个问题。

Below is my gulp set up and package.json.

下面是我的 gulp 设置和 package.json。

var babel = require('gulp-babel');
var es2015 = require('babel-preset-es2015');

gulp.task('babel', function() {
    return gulp.src('./app/main.js')
    .pipe(babel({
        presets: [es2015]
    }))
    .pipe(gulp.dest('dist'));
});

Package.json

包.json

  "devDependencies": {
    "babel-preset-es2015": "^6.3.13",
    "babel-preset-es2015-node5": "^1.1.1",
    "browser-sync": "^2.11.0",
    "gulp": "^3.9.0",
    "gulp-babel": "^6.1.1",
    "gulp-stylus": "^2.2.0"
  }

I am using node v5.1.0 and babel v6.4.0

我正在使用节点 v5.1.0 和 babel v6.4.0

Here is the terminal ouput

这是终端输出

terminal output

终端输出

回答by 88mary256

You only need to install babel-preset-es2015:

您只需要安装babel-preset-es2015

CLI usage example:

CLI 使用示例:

npm install babel-cli babel-preset-es2015

回答by Picard

the "es2015" in :

中的“es2015”:

    .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:

实际上是一个路径 - 所以如果您在 /Users/username/es2015 目录中没有预设,您必须完全指向它,例如:

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

it worked for me

它对我有用

回答by Ihor Khomiak

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

要解决此问题,您应该从“/Users/username”目录中删除 .babelrc(隐藏)文件。

回答by Andrei CACIO

I just used this exact gulpfile.js

我只是使用了这个确切的 gulpfile.js

var babel = require('gulp-babel');
var es2015 = require('babel-preset-es2015');
var gulp = require('gulp');

gulp.task('babel', function() {
    return gulp.src('./app/main.js')
    .pipe(babel({
        presets: [es2015]
    }))
    .pipe(gulp.dest('dist'));
});

and it worked for me. I only installed babel, babel-preset-es2015and gulp-babel.

它对我有用。我只安装了babel,babel-preset-es2015gulp-babel.

回答by Jeyenth

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

检查项目的根文件夹中是否有 .babelrc 文件。如果没有创建 .babelrc 文件并添加以下内容:

{
  "presets": ["es2015"]
}

It fixed the issue.

它解决了这个问题。

回答by sareek

You could try installing es2015and stage-2via

您可以尝试安装es2015stage-2通过

npm i babel-preset-es2015 --save
npm i babel-preset-stage-2 --save

回答by Tom Van Rompaey

I encountered the same issue and it was because I had a .babelrcfile in the root of my directory.

我遇到了同样的问题,这是因为我.babelrc的目录根目录中有一个文件。

To fix this add babelrc: falseinside the babel options:

babelrc: false在 babel 选项中修复此添加:

var babel = require('gulp-babel');

gulp.task('babel', function() {
    return gulp.src('./app/main.js')
    .pipe(babel({
        babelrc: false,
        presets: ['babel-preset-es2015']
    }))
    .pipe(gulp.dest('dist'));
});

回答by ArchNoob

I had the same issue, and this second suggestion helped me noticing my problem and maybe it's yours too.

我有同样的问题,第二个建议帮助我注意到我的问题,也许它也是你的问题。

I npm install gulp-babel-es2015then didn't include it in the gulpfile at all.

npm install gulp-babel-es2015然后我根本没有将它包含在 gulpfile 中。

Then babel({presets: ['es2015']})option is just a string as shown in the examples here https://www.npmjs.com/package/gulp-babel.

然后babel({presets: ['es2015']})option 只是一个字符串,如https://www.npmjs.com/package/gulp-babel示例中所示。

Here is my gulpfile.

这是我的 gulpfile。

var gulp = require('gulp'),
    babel = require('gulp-babel');

gulp.task('babelify', () => {
    gulp.src('js/*.js')
        .pipe(babel({
            presets: ['es2015']
        }))
    .pipe(gulp.dest('alljs'));
});

gulp.task('default', ['babelify']);

Also, as from this issue here, https://github.com/laravel/elixir/issues/354

另外,从这里的这个问题开始,https://github.com/laravel/elixir/issues/354

Suggestions are you should update node to version 5.x.x and npm to 3.x.x.

建议您应该将 node 更新到 5.xx 版本,将 npm 更新到 3.xx

回答by sunkehappy

The situation where I encounter this problem is that I have moved the files from xxxto xxx/server. And then under xxx/serverI 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 .babelrcfile under xxx. And after I move that .babelrcto xxx/server, the error goes away.

我遇到这个问题的情况是我已经将文件从xxxxxx/server. 然后在xxx/server我下面会看到Error: Couldn't find preset "es2015" relative to directory "/Users/username/xxx"错误。真正的原因是我忘记将该.babelrc文件移动到xxx. 在我将其.babelrc移至 后xxx/server,错误消失了。

回答by manuman94

My problem was that other program was using a file involved in the compilation process (probably the .babelrc). Closing several apps solved my problem.

我的问题是其他程序正在使用编译过程中涉及的文件(可能是 .babelrc)。关闭几个应用程序解决了我的问题。

For me was Dropbox or even Brackets Editor with eqFTP extension.

对我来说是 Dropbox 甚至带有 eqFTP 扩展的 Brackets Editor。

Greetings

你好