node.js 10.26 will rightfully throw an error when you try to require a file that is not valid javascript (or JSON).
My problem is that it also outputs to stderr:
borken.js - know to be broken javascript file
,,>Z>badfile!=-csa&asd;'asdl ds]=}{ADS}DAS:LMFSV'
test.js
try {
var borken = require('./borken');
} catch (e) {} finally {
console.log('finally!');
}
Expected
$ node test.js
finally!
Actual
$ node test.js
/path/to/borken.js:1
(function (exports, require, module, __filename, __dirname) { asd;'asdl
^^^^^
finally!
The first three lines are output to stderr (running as node test.js 2> /dev/null suppresses them).
Is there any way to get rid of them?
What I've done so far:
I debugged while following the source and narrowed down the culprit to:
lib/module.js:439
var compiledWrapper = runInThisContext(wrapper, filename, true);
runInThisContext is defined in lib/vm.js, which unfortunately is a C++ wrapper, so I couldn't really follow what is going on through there.
I guess I'm looking for a magic "don't spam stderr" flag or any knowledge of this being fixed in any latest versions.
Apparently this has been fixed in the dev branch (0.11.x) but not in the latest stable (0.10.29)
Github issue: Syntax errors are printed to stderr, even when wrapped with try/catch #6920
Related
I'm not sure why, but since upgrading to Node v16 from Node v14, I get this error only when I run node like node -r esm and there is a ReferenceError like:
ReferenceError: myVar is not defined
It causes a massive, 5000 line output of the esm.js module. How can I get rid of this?
const __global__ = this;(function (require, module, __shared__) { var __shared__;const e=module,t={Array:global.Array,Buffer:global.Buffer,Error:g
lobal.Error,EvalError:global.EvalError,Function:global.Function,JSON:global.JSON,Object:global.Object,Promise:global.Promise,RangeError:global.Ran
geError,ReferenceError:global.ReferenceError,Reflect:global.Reflect,SyntaxError:global.SyntaxError,TypeError:global.TypeError,URIError:global.URIE
rror,eval:global.eval},r=global.console;module.exports=(function(e){var t={};function r(i){if(t[i])return t[i].exports;var n=t[i]={i:i,l:!1,export
s:{}};return e[i].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.d=function(e,t,r){Reflect.defineProperty(e,t,{configurable:!0,enumerable
:!0,get:r})},r.n=function(e){return e.a=e,function(){return e}},r(r.s=2)})([(function(e,t){var r;t=e.exports=$,"object"==typeof process&&process,r
=function(){},t.SEMVER_SPEC_VERSION="2.0.0";var i=256,n=Number.MAX_SAFE_INTEGER||9007199254740991,s=t.re=[],a=t.src=[],o=0,u=o++;a[u]="0|[1-9]\\d*
";var l=o++;a[l]="[0-9]+";var c=o++;a[c]="\\d*[a-zA-Z-][a-zA-Z0-9-]*";var p=o++;a[p]="("+a[u]+")\\.("+a[u]+")\\.("+a[u]+")";var h=o++;a[h]="("+a[l
]+")\\.("+a[l]+")\\.("+a[l]+")";var f=o++;a[f]="(?:"+a[u]+"|"+a[c]+")";var d=o++;a[d]="(?:"+a[l]+"|"+a[c]+")";var m=o++;a[m]="(?:-("+a[f]+"(?:\\."
+a[f]+")*))";var v=o++;a[v]="(?:-?("+a[d]+"(?:\\."+a[d]+")*))";var g=o++;a[g]="[0-9A-Za-z-]+";var y=o++;a[y]="(?:\\+("+a[g]+"(?:\\."+a[g]+")*))";v
ar x=o++,b="v?"+a[p]+a[m]+"?"+a[y]+"?";a[x]="^"+b+"$";var w="[v=\\s]*"+a[h]+a[v]+"?"+a[y]+"?",E=o++;a[E]="^"+w+"$";var S=o++;a[S]="((?:<|>)?=?)";v
ar R=o++;a[R]=a[l]+"|x|X|\\*";var P=o++;a[P]=a[u]+"|x|X|\\*";var _=o++;a[_]="[v=\\s]*("+a[P]+")(?:\\.("+a[P]+")(?:\\.("+a[P]+")(?:"+a[m]+")?"+a[y]
+"?)?)?";var k=o++;a[k]="[v=\\s]*("+a[R]+")(?:\\.("+a[R]+")(?:\\.("+a[R]+")(?:"+a[v]+")?"+a[y]+"?)?)?";var I=o++;a[I]="^"+a[S]+"\\s*"+a[_]+"$";var
A=o++;a[A]="^"+a[S]+"\\s*"+a[k]+"$";var N=o++;a[N]="(?:^|[^\\d])(\\d{1,16})(?:\\.(\\d{1,16}))?(?:\\.(\\d{1,16}))?(?:$|[^\\d])";var C=o++;a[C]="(?
:~>?)";var O=o++;a[O]="(\\s*)"+a[C]+"\\s+",s[O]=RegExp(a[O],"g");var T=o++;a[T]="^"+a[C]+a[_]+"$";var M=o++;a[M]="^"+a[C]+a[k]+"$";var L=o++;a[L]=
"(?:\\^)";var D=o++;a[D]="(\\s*)"+a[L]+"\\s+",s[D]=RegExp(a[D],"g");var F=o++;a[F]="^"+a[L]+a[_]+"$";var j=o++;a[j]="^"+a[L]+a[k]+"$";var V=o++;a[
V]="^"+a[S]+"\\s*("+w+")$|^$";var G=o++;a[G]="^"+a[S]+"\\s*("+b+")$|^$";var B=o++;a[B]="(\\s*)"+a[S]+"\\s*("+w+"|"+a[_]+")",s[B]=RegExp(a[B],"g");
v..........
How can I stop node from printing this to the console?
Try resetting your computer. It works for me.
I have a simple hello world js file I run from terminal with node, and everything works fine. When I try to run the exact same thing from my makefile it prints the hello world part but then fails. Here is the relevant target from makefile:
run: ${JS_DIR}/main.js
node ${JS_DIR}/main.js
And here is the error I get:
$ make run
node /some/dir/main.js
Hello World
makefile:44: recipe for target 'run' failed
make: *** [run] Error 254
What's going on here?
A make target will fail if any of its recipes return an error code (i.e., non-zero). node is returning a non-zero. Try running
node /some/dir/main.js; echo $?
to confirm.
You could either update main.js to not return an error (I believe this is done by calling process.exit() with no parameters or a 0 parameter, but I'm not a node or js expert, so take that with a grain of salt).
Alternatively, you could make make ignore the return code by adding a - to the beginning of the recipe:
run: ${JS_DIR}/main.js
-node ${JS_DIR}/main.js
So I am trying to create a WebAssembly Module from an ArrayBuffer.
C code:
#include <stdio.h>
int main() {
printf("hello, world!\n");
return 0;
}
I compile it like so:
$ emcc -O2 hello.c -s WASM=1 -o hello.html
I start a local http server.
And I try to load it in my browser like so:
fetch('hello.wasm')
.then(res => res.arrayBuffer())
.then(buff => WebAssembly.Module(buff));
And I get the following error:
Uncaught (in promise) RangeError: WebAssembly.Module(): Wasm compilation exceeds internal limits in this context for the provided arguments
at fetch.then.then.buff (:1:77)
at
I don't what to make of this error and I can't find anything via web searches.
Any help is kindly appreciated
Thanks!
WebAssembly.Module is synchronous and some browsers don't allow large modules on the main thread to avoid having compilation block the main thread.
Try this instead:
fetch('hello.wasm').then(response =>
response.arrayBuffer()
).then(buffer =>
WebAssembly.instantiate(buffer, importObj)
).then(({module, instance}) =>
instance.exports.f()
);
It's better to use WebAssembly.instantiate because it does compilation and instantiation together and allows the engine to keep at the importObject to make sure things look OK (especially, the WebAssembly.Memory).
Here I assume you want more than main, and instead want to invoke your module's exported function f.
I'm facing an issue while trying to get javascript unit tests to work at the command line using qunit.
Here's some sample code to reproduce the error:
file util.js:
function abc() {
return 'abc';
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
abc: abc
};
}
file util-tests.js
var qunit = require("qunit");
test("Test abc function", function () {
equal(util.abc(), 'abc');
});
With these files, I can run tests using the following command (gives a table-like output in the shell with the test results):
qunit -c util:util.js -t util-tests.js
Now it breaks if I add the following to util.js
$(document).ready(function () {
/* some code here */
});
Here's the error output:
qunit -c util:util.js -t util-tests.js
Testing /home/mfrere/jstst/util.js ... [Error: Uncaught exception in child process.]
same problem with:
var a = $;
or:
var a = document;
So this makes me think that I need to import jQuery somehow, so I thought about adding jquery.js as a dependency to the command, like this:
qunit -c util:util.js -t util-tests.js -d jquery.js
The above command gives me the same 'Uncaught exception' error, even if util.js doesn't contain any reference to '$'.
I'll probably need to do something else to get qunit to recognize 'document' as well, but I don't know what or how.
Now here's my question: what should I do to get this to work? It is important to keep in mind I want to test my files at the command line, not in a browser.
Just in case I did something wrong in the setup process, this is how I installed node/qunit (under ubuntu):
git clone git://github.com/creationix/nvm.git ~/.nvm
in .bashrc, I added the following line:
source ~/.nvm/nvm.sh
picked a specific version of node
nvm install v0.9.2
nvm alias default 0.9
and installed qunit
npm install -g qunit
finally I had to add this in .bashrc as well:
export NODE_PATH=~/.nvm/v0.9.2/lib/node_modules
You haven't imported jQuery:
$ = require('jquery'),
jQuery = require('jquery');
If you're using browserify, change that to 'jquery-browserify'.
Are there equivalent to perl -c syntax check for JavaScript from command? Given that I have NodeJS installed?
JSLint is not considered as it is not a real parser. I think YUI compressor is possible but I don't want to install Java on production machines, so I am checking if Node.JS already provided this syntax check mechanism.
If you want to perform a syntax check like that way we do in perl ( another scripting language) you can simply use node -c <js file-name>
e.g. a JS file as test.js has:
let x = 30
if ( x == 30 ) {
console.log("hello");
else {
console.log( "world");
}
now type in node -c test.js
it will show you
test.js:5
else {
^^^^
SyntaxError: Unexpected token else
at startup (bootstrap_node.js:144:11)
at bootstrap_node.js:509:3
Now after fixing the syntax issue as
let x = 30
if ( x == 30 ) {
console.log("hello");
} else {
console.log( "world");
}
check syntax - node -c test.js will show no syntax error!!
Note - we can even use it to check syntax for all files in a dir. - node -c *.js
Try uglify. You can install it via npm.
Edit: The package name has changed. It is uglify-js.
nodejs --help
explains the -p switch: it evaluates the supplied code and prints the results. So using nodejs -p < /path/to/file.js would be a disastrous way to check the validity of node.js code on your server. One possible solution is the one indicated in this SO thread. The one thing not so good about it - the syntax error messages it reports are not terribly helpful. For instance, it tell you something is wrong but without telling you where it is wrong.