How to find the location of a javascript syntax error programmatically? - javascript

I am loading a Node.js module using require. This throws a Syntax Error (an Error object).
Is there a way to get the location in the file where the error occurred?
I can see the stack (below) but I don't have any location information. I know I can use substack's node-syntax-error, but is there a way to get similar location info from the Javascript Error object?
SyntaxError: Unexpected identifier
at Module._compile (module.js:437:25)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
... // the rest is in my code
EDIT:
According to Vadim's comment below, yes that is what I am tempted to do. I see that node throws nice errors. Having a test.js file that contains only abc you get an error from node saying
ReferenceError: abc is not defined
at Object.<anonymous> (test.js:1:63)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
Now the question is, is there a node API to do get this error (that the node module.js module uses) such that I don't have to child_process.spawn('node', ['test.js'])?

You can simple run node with file that contains SyntaxError and see console output.

Error.stack
Errors have a "stack" property that stores the stack trace.
try {
throw new Error("with some message");
}
catch(err) {
// print error's stack trace to stder
console.error(err.stack);
}
running this on a file called "/home/myusername/test.js"
node /home/myusername/test.js
will output the following
Error: with some message
at Object.<anonymous> (/home/myusername/test.js:2:11)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)

#laconbass answer works for me.
err.stack
I tried a blog framework and I got : 500 Internal Server Error. Hopefully I put a console :
app.use(function(error, req, res, next) {
res.status(500).render('500');
console.log(error);
});
and I could see the error which was not descriptive:
TypeError: Cannot read property 'replace' of undefined
After several minuts of research I found this answers, So I tried :
app.use(function(error, req, res, next) {
res.status(500).render('500');
console.log(error.stack);
});
and I could see the source point of error : (Object.exports.replace)
TypeError: Cannot read property 'replace' of undefined
at Object.exports.replace (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/filters.js:411:15)
at eval (eval at <anonymous> (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:498:13), <anonymous>:41:63)
at Object.exports.each (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/utils.js:45:11)
at eval (eval at <anonymous> (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:498:13), <anonymous>:26:10)
at Object.eval [as tpl] (eval at <anonymous> (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:498:13), <anonymous>:85:3)
at compiled (/.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:619:18)
at /.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:559:20
at /.../some_folder/node.js-blog-engine/node_modules/swig/lib/swig.js:690:9
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:380:3)
I know that this is not a syntax error, but could be works to find the location of a javascript/node error.
I am a novice in nodejs.
I hope this helps.

You can use the stacktracey library (note: I'm the author of this library).
It prints nice callstacks along with source code lines, and also parses the SyntaxError output (in Node higher than v4).
For example, when trying to require this file (named test_files/syntax_error.js):
// next line contains a syntax error (not a valid JavaScript)
foo->bar ()
...the pretty printed call stack for the error thrown would be:
at (syntax error) test_files/syntax_error.js:2 foo->bar ()
at it test.js:184 try { require ('./test_files/syntax_error.js') }
at runCallback timers.js:781
at tryOnImmediate timers.js:743
at processImmediate [as _immediat timers.js:714
...where the first line is generated from parsing the raw output from the util.inspect call in Node.
The library also provides the full API access to the contents of a parsed call stack, so you can tune the output to whatever you want.

Related

remote function returns undefined or null in electron renderer process

I'm currently very interested in the comprehensive opportunites granted by electron.js and its modules. Unfortunately, I keep getting the same error in my renderer process (named 'connector.js') when trying to start my application.
Here is the error:
App threw an error during load
TypeError: Cannot match against 'undefined' or 'null'.
at Object.<anonymous> (D:\Eigene Dateien\Desktop\Coding\DesktopApps\EVT\extFunctions\connector\connector.js:2:44)
at Object.<anonymous> (D:\Eigene Dateien\Desktop\Coding\DesktopApps\EVT\extFunctions\connector\connector.js:22:3)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Eigene Dateien\Desktop\Coding\DesktopApps\EVT\main.js:9:1)
And here is my connector.js:
const $ = require('jquery');
const {BrowserWindow} = require('electron').remote;
let Remotewin = remote.getFocusedWindow();
$("#minimize").click(function(){
Remotewin.minimize();
});
$("#maximize").click(function(){
if(!Remotewin.isMaximized()){
Remotewin.maximize();
}else{
Remotewin.unmaximize();
}
});
$("#close").click(function(){
Remotewin.close();
});
As you can see clearly, I wanted to create my own menubar at the top frame of the window, but the functionality is getting seemingly demolished by this error. I already searched half of the internet and stackoverflow, but the every answer I found refered to a webpack and/or electron bug they couldn't directly influence.
That's why I want to clearly point out, that I am NOT using webpack in this project. Only external module I added is jquery, as you can see in the code.
So my question; Have you experiended this error in this context and do you maybe even know a solution? Or can you refer to someone with similiar problems?
Thank you in advance, J0nny
Since getFocusedWindow() is a a static method of BrowserWindow,
let Remotewin = remote.getFocusedWindow();
should be:
let Remotewin = BrowserWindow.getFocusedWindow();

alternative to 'new Image()' of browser implementation into node-webgl javascript implementation?

Currently I am using a webGL based browser implementation code at client end. It is working perfectly. But, I want to use the same code at server end. Yes, this is not browser based, pure javascript code using node-webgl library.
While doing thisI am facing a problem.
new Image() is identified by browser, but at server side I am getting error Image is not defined. Below is the error I have attached. There are also additional two errors I am looking into it.
C:\Users\z003npra\Desktop\node>node exp.js
Status: Using GLEW 1.13.0
Linking ./simple.vert+./simple.frag
------------
Vertex info
-----------
(0) : error C5145: must write to gl_Position
Linking ./raycast.vert+./raycast-color.frag
------------
Vertex info
-----------
(0) : error C5145: must write to gl_Position
undefined:346
gl.tf_img = new Image();
^
ReferenceError: Image is not defined
at initTexture (eval at <anonymous> (C:\Users\z003npra\Desktop\node\exp.js:1
5:9), <anonymous>:346:18)
at volumerc_main (eval at <anonymous> (C:\Users\z003npra\Desktop\node\exp.js
:15:9), <anonymous>:392:2)
at Object.<anonymous> (C:\Users\z003npra\Desktop\node\exp.js:17:1)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3
I tried another method installing canvas i.e npm install canvas and access it's Image object element: However, I am getting other error, looks like it is not compatible. At start I have defined
var Canvas1 = require('canvas')
, Image = Canvas1.Image;
The error is below,
C:\Users\z003npra\Desktop\node>node exp.js
Status: Using GLEW 1.13.0
Linking ./simple.vert+./simple.frag
------------
Vertex info
-----------
(0) : error C5145: must write to gl_Position
Linking ./raycast.vert+./raycast-color.frag
------------
Vertex info
-----------
(0) : error C5145: must write to gl_Position
C:\Users\z003npra\Desktop\node\node_modules\node-webgl\lib\webgl.js:806
throw new TypeError('Expected texImage2D(number target, number level, numb
er internalformat, number format, number type, Image pixels)');
^
TypeError: Expected texImage2D(number target, number level, number internalforma
t, number format, number type, Image pixels)
at Object.texImage2D (C:\Users\z003npra\Desktop\node\node_modules\node-webgl
\lib\webgl.js:806:13)
at handleLoadedTexture (eval at <anonymous> (C:\Users\z003npra\Desktop\node\
exp.js:15:9), <anonymous>:330:5)
at gl.tf_img.onload (eval at <anonymous> (C:\Users\z003npra\Desktop\node\exp
.js:15:9), <anonymous>:349:3)
at Image.src (C:\Users\z003npra\Desktop\node\node_modules\canvas\lib\image.j
s:30:17)
at initTexture (eval at <anonymous> (C:\Users\z003npra\Desktop\node\exp.js:1
5:9), <anonymous>:351:16)
at volumerc_main (eval at <anonymous> (C:\Users\z003npra\Desktop\node\exp.js
:15:9), <anonymous>:392:2)
at Object.<anonymous> (C:\Users\z003npra\Desktop\node\exp.js:17:1)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
Any other alternative to imitate the Image type in node-webgl based implementation?
Use Image from node-webgl library: var Image = require("node-webgl").Image;

Meaning of "TypeError: string is not a function"

What does the error "TypeError: string is not a function" mean exactly?
Which conditions trigger the error?
A little context to show where the error appears.
The following program
"use strict;"
((console["log"])(42));
gives the error
/private/var/folders/k6/grq8nv093hj78x5m172d725m0000gn/T/tmp14388866091438886609658.js:2
((console["log"])(42));
^
TypeError: string is not a function
at Object.<anonymous> (/private/var/folders/k6/grq8nv093hj78x5m172d725m0000gn/T/tmp14388866091438886609658.js:2:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
in Node. But console["log"] aka console.log ought to be a function.
However this program
"use strict;"
function displayln(v){return ((console["log"])(v));};
(displayln(42));
runs without any errors.
I think it thinks you're trying to execute "use strict;" as a function. Try adding a semi colon after it
"use strict";
((console["log"])(42));

Getting "cannot set property Error of undefined" when trying to run the .js output of a simple hello world clojurescript

I'm compiling:
(ns example.hello)
(js/console.log "Hello from ClojureScript!")
With this configuration:
(defproject lein-cljsbuild-example "1.2.3"
:plugins [[lein-cljsbuild "0.2.9"]]
:cljsbuild {
:builds [{
:source-path "src-cljs"
:compiler {
:output-to "war/javascripts/mainz.js" ; default: main.js in current directory
;:optimizations :simple
:target :nodejs
;:pretty-print true
}}]})
Which outputs a file that is too big to put here, but gives the error:
goog.debug.Error = function(opt_msg) {
^
TypeError: Cannot set property 'Error' of undefined
at Object.<anonymous> (/Users/myuser/Clojure/cljstest/war/javascripts/mainz.js:503:18)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Well, in the project config you give, your :optimizations :simple clause is commented out. This means that it will not have any Google Closure optimizations, meaning that the output JavaScript will not be in one sufficient file, but broken into many files. Which also means you must explicitly include base.js from the Google Closure library.
It looks like that's what's happening here, although there might be other stuff going on as well... I'm actually not that familiar with the node.js output for ClojureScript.
The error has been solved by reinstalling leiningen and doing a clean build.

Node.js Cannot find module 'tcp'

node is crashing at the following line:
var tcp = require('tcp'),
error text:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'tcp'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.<anonymous> (C:\Program Files\nodejs\websocket\websocket.js:11:11)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Module.require (module.js:357:17)
What is the problem? I found the source on the Internet, and the author, and the visitors also can run it...
Try require('net') instead:
$ node
> var tcp = require('tcp');
The 'tcp' module is now called 'net'. Otherwise it should have a similar interface.
> var tcp = require('net');
> $
Others are able to run, may be because they are using Node module when 'tcp' was there...
Now its called 'net', but its all the same thing no need of checking..
If you want to cross check for your more information, here are the links:
1.http://nodejs.org/api/net.html
2.https://github.com/joyent/node/blob/master/lib/net.js

Categories