Eclipse Node Debugger require() exception - javascript

I am using windows machine with the latest eclipse.
I followed the instructions over https://github.com/nodejs/node-v0.x-archive/wiki/Using-Eclipse-as-Node-Applications-Debugger.
On the command line I execute node --debug-brk server.js. When I run eclipse debugger, it connects to the node. The first line in my code is require(), and when the debugger visits this line, it throws an exception:
line 1: uncaught JavaScript runtime exception: ReferenceError: "require" is not defined.
I tried to have a breakpoint after that line, but whenever the debugger starts, it visits the first (require()) line.
How can I pass this line, and continue debugging?

quote from https://github.com/joyent/node/wiki/Using-Eclipse-as-Node-Applications-Debugger
Note that V8 engine debugger is not behaving very good when it steps over or steps into >require() method (it will crash), so try to set up first breakpoint past the initial module >loading. This will also enable you to set breakpoints in any of those modules as well.
try
node --debug server.js

Related

Why do I get a bash: syntax error near unexpected token on every bit of code with VS Code?

I am very new to coding, and have just installed VS Code, and installed Node.js as well as Git Bash. I was working on a project, but couldn't console.log anything as I always received the following error when using Git Bash:
bash: syntax error near unexpected token `validateCred' (One of the
variables I used)
I created a new JS file, and ran the following code:
const hello = 'hello world';
I still received the same bash error. I have tried replacing my code with code that I know works and still receive the same errors.I have also tried using Powershell as my terminal, but receive the following errors every time:
batch : The term 'batch' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again.
I am at a loss, and I'm sure it's something really simple, but I can't seem to figure it out!
A Bash shell expects you to enter Bash code and not JavaScript code.
If you want to run JavaScript code then you need to run it in Node.js and not in Bash.
Generally, the command node will launch Node.js in a Bash shell. (Assuming it is installed on the $PATH).

Debug a node.js process forked via child_process.fork in VSCode

I'm trying to debug a Node script forked using child_process's fork method using VSCode.
I have read almost all of the related docs, discussions but without any success. I'm not sure what am I doing wrong.
For your reference, I have posted a my sample code as a gist and is available at: https://gist.github.com/dpnishant/43096af3fecde4ec9fdea7c1d81cf543
It contains the three files: parent.js, child.js and launch.json (vscode configuration). I'm trying to setup 2 breakpoints: one at https://gist.github.com/dpnishant/43096af3fecde4ec9fdea7c1d81cf543#file-child-js-L4 and another at https://gist.github.com/dpnishant/43096af3fecde4ec9fdea7c1d81cf543#file-parent-js-L16.
Case-1
When I click "Start Debugging" using the "Launch Program" config, I'm able to hit the line 16 breakpoint on parent.js but the other breakpoint in child.js is never hit.
Case-2
When I click "Start Debugging" using the "Attach" config, I get the following error:
Debugging with legacy protocol because Node.js version could not be determined (Error: connect ECONNREFUSED 127.0.0.1:9999)
Case-3
When I start node parent.js in Terminal and immediately switch to VSCode to click "Start Debugging" using the "Attach to Process" config and choose the node process with the "child.js" script, I'm able to hit breakpoint on line 4 in child.js but by that time, the loop has already started and it's already late.
So my requirement is a method through which I can start my parent.js script and hit the desired breakpoint in child.js as soon as it starts execution.
Can someone point out to me where am I doing wrong? And what would be a optimum solution for my use-case?

node js returning Syntax error: Unexpected identifier

I downloaded and installed node.js on Windows and I'm following a simple tutorial from nodebeginner.org.
I've created a file called HelloWorld.js which contains just:
console.log("Hello World");
When I type node HelloWorld.js in the node.js console I get:
SyntaxError: Unexpected identifier
I checked my classpath variable and it has the C:\Program Files\nodejs\ on it.
HelloWorld.js is still open in Notepad++ for editing.
What am I doing wrong?
I think you are already in the the console.
Just follow the steps to fix the error:
1) Try doing CTRL + C couple of times. See if you exit the console
2) Then do node HelloWorld.js
I think you will get your output
When in your node console already, you can simply do require("./HelloWorld.js") to get the output. (Given that you are in the directory that contains this file)
When I type node HelloWorld.js in the node.js console I get
You should type JavaScript into the Node.js console.
node is a program name. HelloWorld.js is a command line argument. They are not JavaScript.
You should type those into your shell (e.g. Windows Powershell or bash).
I had the same issue when following an online course, my mistake was that i did not safe the file i was following as .js in the name when saving.
Therefore my Hello.js did not open because it was only Hello
If people are facing below-mentioned error: Uncaught SyntaxError: Unexpected identifier at the time of running, console.log("Hello World"); code with command, node HelloWorld.js, in VS code editor
Problem :
node HelloWorld.js ^^^^^ Uncaught SyntaxError: Unexpected identifier
Solution :
(1) Just install Babel JavaScript extension in VS code editor
(2) After Babel JavaScript extension is installed, save your Program and then run your program with the command, node HelloWorld.js
Definitely will get the expected result.
I'm on linux and I'd the same issue
what I was writing in terminal is :
node
node file.js
all what I had to do is to write node file.js from the start without writing node first .
Although the question is old, I just solved it. So for anyone who still likes an answer: Apparently Node.Js installs two different consoles or executables. There is "Node.js" and there is "Node.js command prompt". Use the latter and it will work
To clarify, I used another tutorial in Dutch. Use the Javascript code in there and then in your web browser type http://localhost:3000. There you will see the Hello World output.
A little late but I figured this out as I'm learning it as well. You are not in the correct Node.js command window:
You are probably trying to run Node.js, ie. the one with the red arrow. This gives you the "Unexpected identifier" error. You want the Node.js command prompt, or the one shown with a green arrow.
Craig
On windows hit CTRL + D to exit REPL and then run HelloWorld.js again

Run JavaScript in a Windows Command Line

This is something I only found out about today is that JavaScript can be run through a windows command line.
So I found out that to run a javascript file in windows cmd.exe you use cscript.
My hworld.js file only has one line
print('hello world');
I try to run this through the command line with
cscript /Prog/hworld.js
It didn't run with the error
Microsoft JScript runtime error: Object Expected
Are there steps i need to follow before simply cscript running a one line javascript file.
I was under the impression that JavaScript will just run out the box.
PS. Java is the development environment set up for the computer I am trying this on, installed and functional
The right command to print text in the console is
WScript.echo("your text");
The rest works pretty much like javascript.

node-inspector looking for <module>.js when 'require'd

I am getting started with Node Inspector (Linux Mint 14). When I try to debug a simple .js file - console.log("Hello World") - with:
node-debug app.js
Node Inspector starts as expected, breaks on the first line and runs to completion on 'resume'.
However if I include a 'require' in my app.js:
require('someModule');
console.log('Hello World);
NI breaks ok, but when I 'resume' it throws a 'file not found' error as it can't find a .js file with the node module name in node_modules, i.e. ./node_modules/someModule.js. Although there is a ./node_modules/someModule/someModule.js.
Can anyone see what I'm missing?
Doh! Whilst I was clicking around I'd clicked on the "pause on all exceptions" button in the bottom left corner. Clicked on this again and it's now ignoring all the exceptions thrown as it looks for the module files.

Categories