如何修复“找不到模块'gulp-babel'”?

时间:2019-07-29 11:23:29

标签: ecmascript-6 gulp babel ecmascript-5

我在《学习JavaScript。第三版》一书中学习JS,当涉及到编译器时,我所做的一切与本书完全相同,但是在Bash中键入gulp时仍然出现错误。有什么我可以解决的吗?

尝试安装@ babel / core,babel-env 尝试对gulpfile.js进行小的更改 搜索了互联网,但找不到答案...

package.json

"author": "",
"license": "ISC",
"dependencies": {
"underscore": "^1.9.1"
},
"devDependencies": {
 "@babel/cli": "^7.5.5",
 "@babel/core": "^7.5.5",
 "@babel/preset-env": "^7.5.5",
 "babel": "^6.23.0",
 "babel-preset-es2015": "^6.24.1",
 "gulp": "^4.0.2",
 "gulp-babel": "^8.0.0"
 }
 }

gulpfile.js

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

gulp.task('default', function() {

 gulp.src("es6/**/*.js")
    .pipe(babel({
        presets: ['@babael/env']
        }))
    .pipe(gulp.dest("dist"));

gulp.src("public/es6/**/*.js")
    .pipe(babel())
    .pipe(gulp.dest("public/dist"))
});

babelrc

{ 
"presets": ["es2015"]
}

错误是:     内部/模块/ cjs / loader.js:628     犯错     ^

Error: Cannot find module 'gulp-babel'
Require stack:
- C:\Users\archi\test\lj\gulpfile.js
- C:\Users\archi\AppData\Roaming\npm\node_modules\gulp- 
  cli\lib\versioned\^4.0.0\index.js
- C:\Users\archi\AppData\Roaming\npm\node_modules\gulp-cli\index.js
- C:\Users\archi\AppData\Roaming\npm\node_modules\gulp-cli\bin\gulp.js
at Function.Module._resolveFilename 
(internal/modules/cjs/loader.js:625:15)
at Function.Module._load (internal/modules/cjs/loader.js:527:27)
at Module.require (internal/modules/cjs/loader.js:683:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object.<anonymous> (C:\Users\archi\test\lj\gulpfile.js:2:15)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:643:32)
at Function.Module._load (internal/modules/cjs/loader.js:556:12)
at Module.require (internal/modules/cjs/loader.js:683:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\archi\\test\\lj\\gulpfile.js',
'C:\\Users\\archi\\AppData\\Roaming\\npm\\node_modules\\gulp-cli\\lib\\versioned\\^4.0.0\\index.js',
'C:\\Users\\archi\\AppData\\Roaming\\npm\\node_modules\\gulp-cli\\index.js',
'C:\\Users\\archi\\AppData\\Roaming\\npm\\node_modules\\gulp-cli\\bin\\gulp.js'
]
}

2 个答案:

答案 0 :(得分:0)

您需要的babel核心:

使用npm:

npm install --save-dev @babel/core

有纱:

yarn add @babel/core --dev

答案 1 :(得分:0)

按照以下步骤,gulp文件中的任务会成功运行(没有警告):

  1. 安装 gulp-babel、babel-core 和 preset-env
$ npm install --save-dev gulp-babel @babel/core @babel/preset-env
  1. 修改 .babelrc 如下:
// .babelrc
{"presets": ["@babel/env"]}
  1. 为 gulp 任务添加回调
const gulp = require('gulp');
const babel = require('gulp-babel');

gulp.task('default', function(callback) {
    // Node source
    gulp.src("es6/**/*.js")
        .pipe(babel())
        .pipe(gulp.dest("dist"));

    // browser source
    gulp.src("public/es6/**/*.js")
        .pipe(babel())
        .pipe(gulp.dest("public/dist"));
    
    callback();
});