Error write after end on gulp.watch with gulp-connect - gulp

I have a simple watch on gulp that reloads all js files (using gulp-connect) when one of them has changes, but I'm getting the following error whenever the task asociated runs:
[15:14:35] Starting 'livereload:js'...
events.js:160
throw er; // Unhandled 'error' event
^
Error: write after end
at ServerResponse.OutgoingMessage.write (_http_outgoing.js:439:15)
at ServerResponse.res.inject.res.write (C:\dev\test-livereload\node_modules\connect-livereload\index.js:115:37)
at ReadStream.ondata (_stream_readable.js:555:20)
at emitOne (events.js:96:13)
at ReadStream.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at ReadStream.Readable.push (_stream_readable.js:134:10)
at onread (fs.js:1984:12)
at FSReqWrap.wrapper [as oncomplete] (fs.js:681:17)
Here's the code:
gulp.task('livereload:js', function() {
return gulp.src([paths.APP_ROOT + '/**/*.js'])
//.pipe(wait(2000)) // if I add a small wait it doesn't crash as often
.pipe(connect.reload());
});
gulp.task('run-watch', function() {
gulp.watch([paths.APP_ROOT + '/**/*.js'], ['livereload:js']);
});
I have a similar task that checks for changes in scss files and compiles them before reloading that works fine.

I found a workaround. Instead of gulp.watch I use the gulp-watch package to run connect.reload(), which can be used in a pipe.
I think there was a race condition that was triggering the reload before the files were ready.

Related

How can I find which MySQL query is causing errors in my NodeJS app?

I have built an api on top of NodeJS and HapiJS.
The app loads a lot data at startup, and then holds it in memory. I have this app running on a server that has 16 gigs of RAM, so holding 4 gigs of RAM in memory should be possible.
However, every time the app starts up, this error appears 5 times in the log:
{ Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (/home/ec2-user/daemons/liar_lies/node_modules/mysql/lib/Connection.js:425:13)
at Socket.g (events.js:291:16)
at emitNone (events.js:86:13)
at Socket.emit (events.js:185:7)
at Socket._onTimeout (net.js:339:8)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
--------------------
at Protocol._enqueue (/home/ec2-user/daemons/liar_lies/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/home/ec2-user/daemons/liar_lies/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/ec2-user/daemons/liar_lies/node_modules/mysql/lib/Connection.js:136:18)
at /home/ec2-user/daemons/liar_lies/node_modules/knex/lib/dialects/mysql/index.js:106:18
at Promise._execute (/home/ec2-user/daemons/liar_lies/node_modules/bluebird/js/release/debuggability.js:300:9)
at Promise._resolveFromExecutor (/home/ec2-user/daemons/liar_lies/node_modules/bluebird/js/release/promise.js:481:18)
at new Promise (/home/ec2-user/daemons/liar_lies/node_modules/bluebird/js/release/promise.js:77:14)
at Client_MySQL.acquireRawConnection (/home/ec2-user/daemons/liar_lies/node_modules/knex/lib/dialects/mysql/index.js:104:12)
at Object.create (/home/ec2-user/daemons/liar_lies/node_modules/knex/lib/client.js:231:16)
at Pool._createResource (/home/ec2-user/daemons/liar_lies/node_modules/generic-pool/lib/generic-pool.js:326:17)
at Pool.dispense [as _dispense] (/home/ec2-user/daemons/liar_lies/node_modules/generic-pool/lib/generic-pool.js:314:12)
at Pool.acquire (/home/ec2-user/daemons/liar_lies/node_modules/generic-pool/lib/generic-pool.js:392:8)
at /home/ec2-user/daemons/liar_lies/node_modules/knex/lib/client.js:281:19
at Promise._execute (/home/ec2-user/daemons/liar_lies/node_modules/bluebird/js/release/debuggability.js:300:9)
at Promise._resolveFromExecutor (/home/ec2-user/daemons/liar_lies/node_modules/bluebird/js/release/promise.js:481:18)
at new Promise (/home/ec2-user/daemons/liar_lies/node_modules/bluebird/js/release/promise.js:77:14)
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true }
This is the only error, and it happens 5 times whenever the app starts.
And yet, the app does seem to get most of the data that I expect it to get, so whatever query throws this error, it must be fairly obscure.
How can I figure out which query is the one that throws this error?
The stacktrace does not seem to mention the line in my code that causes this. (I find this frustrating.)
These are the modules that I'm using:
"boom": "3.1.2",
"code": "2.1.0",
"fs": "0.0.2",
"glob": "6.0.4",
"glue": "3.1.0",
"good-console": "5.3.0",
"good-file": "5.1.2",
"hapi": "16.0.2",
"html-entities": "1.2.0",
"joi": "7.2.2",
"knex": "0.12.6",
"knex-logger": "0.1.0",
"multiline": "1.0.2",
"mysql": "2.12.0",
"path": "0.12.7",
"plugo": "0.3.1"
There are only 2 functions that I know of that call the database at startup time. This is one of them:
function build_maps_which_index_database_data(query, which_type_of_data_in_which_database_table) {
DB.knex.raw(query).then(
function(result) {
var count_of_database_results = result[0].length;
build_list_of_allowed_fields(result[0][0]);
for(var how_many_records_processed_so_far = 0; how_many_records_processed_so_far < count_of_database_results; how_many_records_processed_so_far++) {
var document = result[0][how_many_records_processed_so_far];
var profile_id = document['profile_id'];
document['which_type_of_data_in_which_database_table'] = which_type_of_data_in_which_database_table;
document['item_id'] = uuidV4();
build_map_of_uuid_ids_to_documents(document);
Object.keys(document).forEach(function(name_of_field_in_database) {
// avoiding name_of_field_in_database = profile_id
if (name_of_field_in_database != 'profile_id') {
var string_from_database = document[name_of_field_in_database];
build_map_of_santized_words_to_uuid_ids(name_of_field_in_database, string_from_database, document['item_id']);
}
});
}
})
.catch(function(e) {
console.log('Error: calling code was get_map_of_santized_words_to_uuid_ids');
console.log('query was: ' + query);
console.error(e);
});
}
Each of these functions has a "catch" clause attached, so I assume if it was the cause of the error, then I would see that in the log.
Errors in question are not caused by the code that you have included in the question.
To find out what is happening when those errors are sent try to run the app with environment variable DEBUG=knex:*. That will show you what is happening in the connection pool and what queries are sent to DB.
export DEBUG=knex:*
cd ~/yourapp
node server.js
or just
cd ~/yourapp
DEBUG=knex:* node server.js
Also try to remove code from your application until you have minimal amount of code in application, which still produces those errors. After that it should be pretty easy to find the root cause of the problem.
One more thing that you can do is to add debug info to
knex.on('query-error', function (err, obj) { ... });
event and check if that catches those errors.

Gulp-kss issues

I am trying to get gulp-kss working on my project, but I am encountering issues when I try to run my task. I get the following error message:
[12:07:28] Using gulpfile ~/Documents/Websites/company/project-uiapp/gulpfile.js
[12:07:28] Starting 'generate-sc'...
[12:07:28] Finished 'generate-sc' after 20 ms
fs.js:549
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open '/Users/Josua/Documents/Websites/company/project-uiapp/node_modules/gulp-kss/node_modules/kss/lib/template/index.html'
at Error (native)
at Object.fs.openSync (fs.js:549:18)
at Object.fs.readFileSync (fs.js:397:15)
at Stream.endStream (/Users/Josua/Documents/Websites/company/project-uiapp/node_modules/gulp-kss/index.js:42:27)
at _end (/Users/Josua/Documents/Websites/company/project-uiapp/node_modules/through/index.js:65:9)
at Stream.stream.end (/Users/Josua/Documents/Websites/company/project-uiapp/node_modules/through/index.js:74:5)
at DestroyableTransform.onend (/Users/Josua/Documents/Websites/company/project-uiapp/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:523:10)
at DestroyableTransform.g (events.js:260:16)
at emitNone (events.js:72:20)
at DestroyableTransform.emit (events.js:166:7)
at /Users/Josua/Documents/Websites/company/project-uiapp/node_modules/vinyl-fs/node_modules/readable-stream/lib/_stream_readable.js:965:16
at doNTCallback0 (node.js:419:9)
at process._tickDomainCallback (node.js:389:13)
How would I resolve this error? I have been searching Google and Stackoverflow but I have not found a solution yet. I am basing my solution off this gulp-kss. I have included my gulp task below:
...
// Generate styleguide with templates
gulp.task('generate-sc', function() {
gulp.src(['build-less'])
.pipe(gulpkss({
overview: __dirname + '/styles/styleguide.md'
}))
.pipe(gulp.dest('styleguide/'));
// Concat and compile all your styles for correct rendering of the styleguide.
gulp.src('./src/less/styles.less')
.pipe(less())
.pipe(gulpconcat('styles/style.css'))
.pipe(gulp.dest('styleguide/'));
...
I finally found the issue. I had to add kss to my package.json. I figured it would of been included with gulp-kss.

babel5 --> babel6: modules property

i'm trying to figure out how to tell babel6 to generate a particular module syntax (amd, commonjs, etc)
this is how i used to specify the module: https://github.com/aurelia/metadata/blob/master/build/tasks/build.js#L48
gulp.task('build-commonjs', function () {
return gulp.src(paths.output + jsName)
.pipe(to5(assign({}, compilerOptions, {modules:'common'})))
.pipe(gulp.dest(paths.output + 'commonjs'));
});
gulp.task('build-amd', function () {
return gulp.src(paths.output + jsName)
.pipe(to5(assign({}, compilerOptions, {modules:'amd'})))
.pipe(gulp.dest(paths.output + 'amd'));
});
babel6 gives me the error: Unknow option: base.modules
appreciate any guidance !! :)
error:
events.js:141
throw er; // Unhandled 'error' event
^
ReferenceError: [BABEL] C:\a\au\metadata\dist\aurelia-metadata.js: Unknown option: base.modules
at Logger.error (C:\a\au\metadata\node_modules\gulp-babel\node_modules\babel-core\lib\transformation\file\logger.js:41:11)
at OptionManager.mergeOptions (C:\a\au\metadata\node_modules\gulp-babel\node_modules\babel-core\lib\transformation\file\options\option-manager.js:262:18)
at OptionManager.init (C:\a\au\metadata\node_modules\gulp-babel\node_modules\babel-core\lib\transformation\file\options\option-manager.js:416:10)
at File.initOptions (C:\a\au\metadata\node_modules\gulp-babel\node_modules\babel-core\lib\transformation\file\index.js:191:75)
at new File (C:\a\au\metadata\node_modules\gulp-babel\node_modules\babel-core\lib\transformation\file\index.js:122:22)
at Pipeline.transform (C:\a\au\metadata\node_modules\gulp-babel\node_modules\babel-core\lib\transformation\pipeline.js:42:16)
at DestroyableTransform._transform (C:\a\au\metadata\node_modules\gulp-babel\index.js:32:20)
at DestroyableTransform.Transform._read (C:\a\au\metadata\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:172:10)
at DestroyableTransform.Transform._write (C:\a\au\metadata\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:160:12)
at doWrite (C:\a\au\metadata\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:335:12)
at writeOrBuffer (C:\a\au\metadata\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:321:5)
at DestroyableTransform.Writable.write (C:\a\au\metadata\node_modules\through2\node_modules\readable-stream\lib\_stream_writable.js:248:11)
at write (C:\a\au\metadata\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:623:24)
at flow (C:\a\au\metadata\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:632:7)
at DestroyableTransform.pipeOnReadable (C:\a\au\metadata\node_modules\gulp\node_modules\vinyl-fs\node_modules\through2\node_modules\readable-stream\lib\_stream_readable.js:664:5)
at emitNone (events.js:67:13)
from #thedark1337 in babeljs slack:
For babel 6 you need to use plugins
https://babeljs.io/docs/plugins/transform-es2015-modules-amd
https://babeljs.io/docs/plugins/transform-es2015-modules-commonjs/
https://babeljs.io/docs/plugins/transform-es2015-modules-systemjs/
also if you haven't, you'll need babel-preset-es2015 as well to transpile from ES2015 to ES5

Browsersync error - Error: spawn EACCES

The part of gulpfile is
gulp.task('serve',['sass'], function() {
browserSync.init({
proxy: "dev.cz/project"
});
gulp.watch(paths.styles.src + '**/*.scss', ['sass']);
gulp.watch("app/*.html").on('change', browserSync.reload);
});
it gives me error
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn EACCES
at errnoException (child_process.js:1000:11)
at Process.ChildProcess._handle.onexit (child_process.js:791:34)
The project is on out local server, I am connected to server via ssh. Everything works fine, until I take broswerync to the party. Any idea ??
This is probably an error because BrowserSync don't have permission to open a browser page for some reason.
adding the property open: false will solve this

How to get output from gulp-ruby-sass task seen by gulp-useref?

I have some CSS that is created by gulp-ruby-sass. The resultant CSS file is referenced in the html used by gulp-useref to get my assets for concatenation, minification, etc. The problem is (I think) that gulp-ruby-sass hasn't written the CSS file to disk yet and gulp-useref can't find the file to include it in the build. So this code:
gulp.task("compile-css", function () {
gulp.src(paths.css.appFiles)
.pipe(gutil.env.debug ? sass({ debugInfo : true }) : sass())
.pipe(gutil.env.debug ? gutil.noop() : cssmin())
.pipe(gulp.dest(paths.css.appDistDir));
});
gulp.task("concatenateAllTheThings", ["compile-css"], function () {
var assets = useref.assets(); // useref package does the magic
return gulp.src(["./index.html", "./login.html", "./reset.html", "./confirm.html"])
.pipe(assets)
.pipe(gulpif('*.js',ngAnnotate()))
.pipe(gulpif('*.js', uglify()))
.pipe(assets.restore())
.pipe(useref())
.pipe(gulp.dest(paths.output.destination));
});
throws this error:
[19:06:32] Starting 'compile-css'...
[19:06:32] Finished 'compile-css' after 13 ms
[19:06:32] Starting 'concatenateAllTheThings'...
[19:06:32] Starting Karma server...
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, no such file or directory 'c....\webui\css\custom.css'
at Object.fs.openSync (fs.js:427:18)
at Object.fs.readFileSync (fs.js:284:15)
at DestroyableTransform.<anonymous> (C....\AppData\Roaming\npm\node_modules\gulp-useref\index.js:81:61)
at Array.forEach (native)
at DestroyableTransform.<anonymous> (C....g\AppData\Roaming\npm\node_modules\gulp-useref\index.js:70:35)
at Array.forEach (native)
at DestroyableTransform.<anonymous> (C....\AppData\Roaming\npm\node_modules\gulp-useref\index.js:46:36)
at Array.forEach (native)
at DestroyableTransform._transform (C....\AppData\Roaming\npm\node_modules\gulp-useref\index.js:43:15)
at DestroyableTransform.Transform._read (C....\AppData\Roaming\npm\node_modules\gulp-useref\node_modules\through2\node_modules\readable-stream\lib\_stream_transform.js:184:10)
How can I get gulp-useref to see the CSS file from gulp-ruby-sass?
To let know gulp when a task is finished, you need to return the stream. This way, a task with a dependency on a task that you return will wait for it to finish.
So change compile-css to
gulp.task("compile-css", function () {
return gulp.src ...
});