Gulp task not running when package.json file is present

480 views Asked by At

I have the following Gulp task:

var gulp = require('gulp');
var browserify = require('browserify');
var reactify = require('reactify');
var source = require('vinyl-source-stream');
var uglify = require('gulp-uglify');
var buffer = require('vinyl-buffer');
var derequire = require('gulp-derequire');

gulp.task('build-scripts', function () {
    var b = browserify();
    b.transform(reactify);
    b.add('./Scripts/app/app.js');

    return b.bundle()
    .pipe(source('./Scripts/build/bundle.js'))
    .pipe(derequire())
    .pipe(buffer())
    .pipe(uglify())
    .pipe(gulp.dest('.'))

});

gulp.task('default', ['build-scripts']);

I run this through Task Run Explorer in VS2013. When I install all dependencies through npm cli and run the task, everything works as expected. Once I add the package.json I get the following error:

\ReactAccordion\gulpfile.js" build-scripts
[15:56:24] Using gulpfile ~\Documents\Visual Studio 2013\Projects\ReactAccordion\ReactAccordion\gulpfile.js
[15:56:24] Starting 'build-scripts'...
events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: SyntaxError: Unexpected token  while parsing json file ReactAccordion\ReactAccordion\package.json
    at ReactAccordion\node_modules\browserify\node_modules\module-deps\index.js:477:30
    at fs.js:334:14
    at FSReqWrap.oncomplete (fs.js:95:15)
Process terminated with code 1.

If after restoring packages I remove the package.json file from the solution, the task completes fine again. I tried searching but can't find a similar problem reported. From looking at the JS files, everything looks fine.

This is my package.json file:

{
    "name": "ReactAccordion",
    "version": "1.0.0",
    "private": true,
    "dependencies": {
        "react": "0.13.3"
    },
    "devDependencies": {
        "gulp": "3.9.0",
        "browserify": "~10.2.4",
        "reactify": "1.1.1",
        "vinyl-source-stream": "1.1.0",
        "gulp-uglify": "1.2.0",
        "vinyl-buffer": "1.0.0",
        "gulp-derequire": "2.1.0"
    }
}
1

There are 1 answers

1
graham mendick On BEST ANSWER

If you create a json file using Visual Studio then it will have a BOM (byte order marking) added to it which makes it invalid JSON, npm install cannot read package.json

Try creating the package.json file using Notepad, instead.