#Browserify エラーを出す
gulpでbrowserifyを使いbuildしてエラーだった時
events.js:85
throw er; // Unhandled 'error' event
と表示される
これだとどこがエラーなのかわからないので、
エラーを適切に出力したい
gulpfile.js
'use strict';
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var gulp = require('gulp');
gulp.task('build', function(){
browserify({
entries: 'index.js',
debug: true
})
.transform('babelify')
.bundle()
.on('error', function(err){ //ここからエラーだった時の記述
console.log(err.message);
console.log(err.stack);
})
.pipe( source('bundle.js') )
.pipe( gulp.dest('build') );
});
gulp.task('default', ['build']);
エラーになるjsファイルを用意
index.js
alert('A');
alert('a'F');
gulpを実行
[18:23:38] Using gulpfile /node_sample/gulpfile.js
[18:23:38] Starting 'build'...
[18:23:38] Finished 'build' after 37 ms
[18:23:38] Starting 'default'...
[18:23:38] Finished 'default' after 6.42 μs
/node_sample/index.js: Unexpected token (3:9) while parsing file: /node_sample/index.js
SyntaxError: /node_sample/index.js: Unexpected token (3:9)
1 | alert('A');
2 |
> 3 | alert('a'F');
| ^
4 |
at Parser.pp.raise (/node_sample/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/location.js:24:13)
at Parser.pp.unexpected (/node_sample/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/util.js:91:8)
at Parser.pp.expect (/node_sample/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/util.js:85:33)
at Parser.pp.parseCallExpressionArguments (/node_sample/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:334:12)
at Parser.pp.parseSubscripts (/node_sample/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:306:29)
at Parser.pp.parseExprSubscripts (/node_sample/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:277:15)
at Parser.pp.parseMaybeUnary (/node_sample/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:247:19)
at Parser.pp.parseExprOps (/node_sample/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:178:19)
at Parser.pp.parseMaybeConditional (/node_sample/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:160:19)
at Parser.pp.parseMaybeAssign (/node_sample/node_modules/babelify/node_modules/babel-core/node_modules/babylon/lib/parser/expression.js:123:19)
エラーの箇所が出た
OK
#####ちなみにpackage.jsonと.babelrcとnodeのversionです
package.json
{
"name": "node_sample",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"babelify": "^7.2.0",
"browserify": "^12.0.1",
"gulp": "^3.9.0",
"vinyl-source-stream": "^1.1.0"
}
}
.babelrc
.babelrc
{
"presets": ["es2015", "react"]
}
% node -v
v5.0.0