Gulp-Sourcemaps, SyntaxError: Unexpected token >
Asked Answered
B

1

5

Gulp / npm noobie here.

I'm trying to use gulp-sourcemaps, and for some reason, it crashes on var sourcemaps = require('sourcemaps').(It crash only when this line's in the file)

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...));
});

Error :

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)

Has anyone encounter this type of error? I tried to google it, without any success.

Bobker answered 24/10, 2016 at 14:4 Comment(2)
What is your node version?Upstroke
Node : v6.8.1 gulp v3.9.1Bobker
D
12

I just started getting the same error and fixed it by replacing the code in C:\Projects\node_modules\strip-bom\index.js with this:

'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;
};

Then, I had to run npm rebuild node-sass to get it to work again. It seems to be an issue with an older version of the Strip-bom node module.

For more info, check this out: https://github.com/sindresorhus/strip-bom/commit/e2a3c3b83706ee5baac284f3862d3f6b9e1564e5

UPDATED ANSWER:

This error is caused by using an older version of Node. The Strip-bom module is now using ES2015 (ES6) syntax which requires Node 5.0+. (See Node's ES2015 support list here)

To test your version of Node, run:

node -v

If it's less than 5.0, you'll need to update it. You can download the newest version of Node here:

https://nodejs.org/en/

After installing the new version of Node, I still needed to run npm rebuild node-sass to get Gulp up and running again.

The former answer will still work if you don't want to update your Node version, however, as Louis pointed out, manually editing node module files is not a best-practice.

Displant answered 24/10, 2016 at 23:54 Comment(3)
This comment put me on the right path. I didn't wanted to hand-modify the file it self, so I installed an older version of gulp-sourcemaps (version 1.5.0) and it fixed the issue.Bobker
Was receiving the same error & this post also put me on the right track, but was running node v0.wontTellYou ;) ~ so, decided to do a full upgrade to node v7 & the npm rebuild node-sass sorted the issue out too. There's probably some kind of dependency documented somewhere, but hey, it's fixed now.Enrika
I'm getting the same error on Debian, but on my Windows machine it seems to be working completely fine even with a fresh npm install. I wonder if anyone knows what might be happening?Snuggle

© 2022 - 2024 — McMap. All rights reserved.