I have an express nodejs app running with nodemon, however when app crashed, there is no error message logged in console:
Here is my index.js file:
/* eslint-disable */
require("babel-register");
require('./server.js');
I don't think this is a nodemon problem because when I remove babel, console would show error message that 'import' is not defined:
Currently for babel I use
{
"presets": ["env"],
"plugins": [
["transform-runtime", {
"helpers": false,
"polyfill": false,
"regenerator": true,
"moduleName": "babel-runtime"
}]
]
}
because of the ReferenceError: regeneratorRuntime is not defined
I tried switching node version, didn't work. Current node version v8.10.0
Would really appreciate if somebody can help me out. It is really a pain to debug without the error message.
for those who encounter the same problem, I ended up solving the problem by this:
/* eslint-disable */
require("babel-register");
try{
require('./server.js');
} catch (e) {
console.log(e)
}
If this still doesn't work, then listen to unhandledRejection and uncaughtException as well by:
process
.on('unhandledRejection', (reason, p) => {
console.error(reason, 'Unhandled Rejection at Promise', p);
})
.on('uncaughtException', err => {
console.error(err, 'Uncaught Exception thrown');
process.exit(1);
});
Related
Using an automated test should allow to find programming errors inside the called code. One programming error is to have an unhandled exception in an RxJS subscribe callback. However, in the following example, jest will simply ignore the error and pass the test.
import { of } from "rxjs";
test("Observable subscription should fail", async () => {
const testObservable$ = of(0, 1, 2, 3);
testObservable$.subscribe((_) => {
throw new Error("Something went wrong");
});
});
The test uses the following minimal jest.config.js:
module.exports = {
preset: "ts-jest/presets/js-with-ts",
transform: { "^.+\\.ts?$": "ts-jest" },
testEnvironment: "node",
testRegex: ".*\\.spec?\\.ts$",
silent: false,
verbose: true,
};
How can I adjust either the test, or the Jest configuration, to make the test fail on an error within the subscription?
I have the following code in index.js
const winston = require('winston');
require('winston-mongodb');
process.on('uncaughtException', (ex) => {
console.log('WE GOT AN UNCAUGHT EXCEPTION');
winston.error(ex.message, ex);
});
Here's how I expect it to work. The index.js will raise an uncaught exception.Then the console.log will print the message and the winston library will log the error and will store it in it's logfile.
In reality however none of the above works.Not even the console.log. So I suppose that the error is never raised.
I use: node -> v10.15.1
npm -> 6.4.1
winston ->2.4.0
winston-mongodb ->3.0.0
I'm puppeteering (could this be a verb?) an app built with webpack and on page.on('error') I get errors like this:
TypeError: __WEBPACK_IMPORTED_MODULE_0_babel_runtime_core_js_promise___default.a.map is not a function
at _default.<anonymous> (....../app.js:23858:89)
at Generator.next (<anonymous>)
at step (....../app.js:35041:30)
at ....../app.js:35052:13
Is there a way, and if so how could I get these errors correctly sourcemapped?
(I know for sure I'm generating the sourcemaps)
I did it in the following way... A bit delayed but someone might find it useful.
Once you have navigated to the page throwing the error you inject this lib: https://github.com/novocaine/sourcemapped-stacktrace
await page.addScriptTag({
url: 'https://cdn.jsdelivr.net/npm/sourcemapped-stacktrace#1.1.8/dist/sourcemapped-stacktrace.js',
});
Then you listen for the error events:
page.on('pageerror', logStackTrace);
page.on('error', logStackTrace);
In the logStackTrace function you extract the sourcemapped stacktrace as follows and log it:
const logStackTrace = async (error) => {
page.evaluate(stack => new Promise(resolve =>
window.sourceMappedStackTrace.mapStackTrace(stack, (newStack) => {
resolve(newStack);
})
), typeof error.stack === 'string' ? error.stack : error.stack.join('\n'))
.then((result) => {
console.log('ERROR:', error.message, result[0]);
});
};
It only correctly maps the first line of the stack for me though. Still infinitely more useful.
If you use one of the eval variants for your webpack devtool then Puppeteer should pick it up:
// webpack.config.js
module.exports = {
// ...
mode: 'development',
devtool: 'eval-source-map',
};
when I run this function in Node (compiled by Babel):
(async function test2(){
let pr = await new Promise(function (resolve, reject){
setTimeout(()=>reject(new Error('reason')))
});
try {
await pr;
} catch (e) {
console.log(e);
}
})();
You can run code in Babel REPL. Both in Babel REPL and in Node my error is not caught (you can see that in browser dev tools in Babel REPL). When I start listening for unhandled rejections in Node (process.on('unhandledRejection'...) I got at least some notification:
unhandled at Promise {
_c: [],
_a: [],
_s: 2,
_d: true,
_v: [Error: reason],
_h: 0,
_n: false } because of [Error: reason]
I would like to catch those errors from try/catch - or any other way except process.on('unhandledRejection' , ...). How can I do that?
I have setup a node.js gulp file to use the babel transform allowing me to utilize ES6 features in browser scripts.
When I add an intentional syntax error to the input file I am not seeing any error messages being output by babelify despite having subscribed to the 'error' event as suggested in the Using Babel with Browserify guide.
Example of invalid syntax:
immmmport $ from 'jquery';
Instead of showing an error in the CLI it just silently fails.
Here is how I have configured my gulp task:
gulp.task('build', () => {
browserify(options)
.transform(babelify)
.add(sourceFilePath)
.bundle()
.pipe(source(outputFileName))
.pipe(buffer())
.pipe(gulpif(!argv.production, sourcemaps.init({ loadMaps: true })))
.pipe(gulpif(argv.production, streamify(uglify())))
.pipe(gulpif(!argv.production, sourcemaps.write('/')))
.pipe(gulp.dest(outputDirName));
})
Am I missing any steps here?
The source of this problem was that I needed to add the following line:
.on("error", err => { gutil.log("Browserify Error", gutil.colors.red(err.message)) })
So that I have this:
gulp.task('build', () => {
browserify(options)
.transform(babelify)
.add(sourceFilePath)
.bundle()
.on("error", err => { gutil.log("Browserify Error", gutil.colors.red(err.message)) })
.pipe(source(outputFileName))
.pipe(buffer())
.pipe(gulpif(!argv.production, sourcemaps.init({ loadMaps: true })))
.pipe(gulpif(argv.production, streamify(uglify())))
.pipe(gulpif(!argv.production, sourcemaps.write('/')))
.pipe(gulp.dest(outputDirName));
})
The following page was useful in diagnosing this issue:
https://github.com/substack/node-browserify/issues/1044