Gulp-Sourcemaps,SyntaxError:Unexpected token>

吞/ npm noobie在这里。

我尝试使用gulp-sourcemaps,出于某种原因,它在var sourcemaps = require('sourcemaps')崩溃(只有当这行在文件中时才会崩溃)

gulpfile:

 var gulp = require('gulp'); var uglify = require('gulp-uglify'); var concat = require('gulp-concat'); var sourcemaps = require('gulp-sourcemaps'); gulp.task('generateApp', function () { return gulp.src([some paths...]) .pipe(sourcemaps.init()) .pipe(concat('app.min.js')) .pipe(uglify()) .pipe(sourcemaps.write()) .pipe(gulp.dest(path...)); }); 

错误:

 C:\Projets\node_modules\strip-bom\index.js:2 module.exports = x => { ^ SyntaxError: Unexpected token > at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.require (module.js:364:17) at require (module.js:380:17) at Object.<anonymous> (C:\Projets\node_modules\gulp-sourcemaps\src\init.js:10:14) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) 

有没有人遇到这种types的错误? 我试图谷歌,没有任何成功。

我刚刚开始得到相同的错误,并通过用以下代码replaceC:\Projects\node_modules\strip-bom\index.js的代码来修复它:

 'use strict'; module.exports = function (x) { if (typeof x !== 'string') { throw new TypeError('Expected a string, got ' + typeof x); } // Catches EFBBBF (UTF-8 BOM) because the buffer-to-string // conversion translates it to FEFF (UTF-16 BOM) if (x.charCodeAt(0) === 0xFEFF) { return x.slice(1); } return x; }; 

然后,我不得不运行npm rebuild node-sass让它再次工作。 这似乎是Strip-bom节点模块的旧版本的问题。

欲了解更多信息,请查看: https : //github.com/sindresorhus/strip-bom/commit/e2a3c3b83706ee5baac284f3862d3f6b9e1564e5

更新的答案:

此错误是由于使用旧版本的节点造成的。 Strip-bom模块现在使用需要Node 5.0+的ES2015(ES6)语法。 (请参阅此处节点的ES2015支持列表)

要testing您的Node版本,请运行:

node -v

如果它小于5.0,则需要更新它。 您可以在这里下载最新版本的Node:

https://nodejs.org/en/

在安装新版本的Node之后,我仍然需要运行npm rebuild node-sass来让Gulp重新运行。

如果您不想更新您的Node版本,前面的答案仍然可以工作,但正如Louis指出的那样,手动编辑节点模块文件不是最佳做法。