is it possible to debug node repl - javascript

Is is it possible debug NODE REPL. node cmd bring the repl prompt >. within the prompt is it possible to start the debugger. say for eg
> hello = function(){
....debugger;
....console.log('hello');
}
>hello() --> should run in the debugger..

Yes
New way: node 8 >=
node --debug is marked for deprecation.
[DEP0062] DeprecationWarning: node --debug and node --debug-brk are invalid. Please use node --inspect or node --inspect-brk instead.
node --inspect is the way moving forward.
Old way
Start the repl with node --debug. Then you can can use node-inspector (an npm package) from a separate terminal, and debug in your browser with the chrome developer tools by opening http://localhost:8080/debug?port=5858 in chrome. You can, for example, set a breakpoint in the repl.js file in the complete function, and if you go to the node repl and hit TAB, it will trigger the breakpoint and you can debug it.
If you want the coffee-script repl, it's coffee --nodejs --debug.

Related

Using Node.js on my fish / cygwin shell

I am following a course and on the learning process so apologies if any of this is simple!
I have installed the fish shell on cygwin through the cygwin installer as an option. I also have node (and npm) installed.
However, when typing 'node' in the shell, it just hangs and does not show the next line to use node instead?
Any help to get this working would be really appreciated!
Please see the image of my terminal response here
node run by itself drops you into an interpreter.
Does ctrl c print this message?
(To exit, press ^C again or type .exit)
What if you do this:
node -e 'console.log("foo")'
I get:
node -e 'console.log("foo")'
foo
What about if you just start typing
node
[press enter]
type in:
console.log('foo')
[press enter]

How to use Node 8 inspector with Chrome?

I am familiar with using the --inspect option since node 7 or something. Now on node 8, it's just not working. Today I asked node to use the inspector, as usual:
$ node --inspect --debug-brk node_modules/mocha/bin/_mocha o/**/*.test.js
It responds with this:
Debugger listening on ws://127.0.0.1:9229/97a6264d-a751-4467-ac36-172ff3ebaac1
For help see https://nodejs.org/en/docs/inspector
If I try to open that link, Chrome says:
This site can’t be reached
The webpage at ws://127.0.0.1:9229/97a6264d-a751-4467-ac36-172ff3ebaac1 might be temporarily down or it may have moved permanently to a new web address.
ERR_DISALLOWED_URL_SCHEME
Before it used to be a "chrome-devtools://" link, which worked splendidly.
What gives?
Searching around, I can't find anything I'm supposed to do with this ws:// link.
There is information from nodejs docs
Option 1: Open chrome://inspect in a Chromium-based browser. Click
the Configure button and ensure your target host and port are listed.
Then select your Node.js app from the list.
Option 2: Install the
Chrome Extension NIM (Node Inspector Manager)
I prefer option 2.
Also --debug-brk is deprecated. Node.js 8.x uses --inspect-brk

node.js can't check version after installation on windows 10

I installed node.js version 4.4.1 on windows 10 and I tried to code with tutorial from internet. The first thing recommended was to write "$ node -v", but after that i'm getting following error:
You're already in the Node REPL, not in Windows' cmd (see the Node icon in the title bar?). You can directly write JS in that.
To get the version, run node -v on Windows' cmd.
you cant check node version inside node REPL to check the node version you need to exit Node REPL, ctrl+c x2 then type node -v or lunch cmd and type node -v

where do I enter my node commands and why?

Node n00b here. I just installed node on my windows desktop and I'm wondering where I should enter my node commands and why... I have three options (see below). Oh, and if one of these (i.e. node.exe) is not intended for entering node commands, what is it for?
I have looked at the nodejs.org docs and I don't see a clear overview/explanation of what each of these are for and why it's recommended to use one over the other.
Thanks for any insight.
================================================
1) Windows Command Line:
2) Node.js command prompt:
3) Node exe
#1 This is simply window's cmd, you can type node --help there to get a general overview of what you can do with node
#2 Also cmd but with some extra configuration, when you view the properties you'll see it's; C:\Windows\System32\cmd.exe /k "C:\Program Files\nodejs\nodevars.bat" Which basically means it runs cmd with a .bat script, that sets some environment variables, changes the title and prints some welcome message.
#3 This is NodeJS' REPL which evaluates JavaScript within NodeJS' context. ( This is the same as running node from cmd )
Usually you don't enter your code directly, but put it in a file instead. Create this file with the name hello.js:
console.log("Hello World!");
Switch to the directory of your file:
cd C:\Users\yourname\yourdirectory
Then run it with the node command:
node hello.js
And you should get the following output:
Hello World!

What's the right way to enable the node debugger with mocha's --debug-brk switch?

I have some debugger statements in my module under test and want to run mocha with --debug-brk set and hit my breakpoint so that I can inspect the state of my module. Unfortunately, whenever I run mocha with this option, I end up with a blank cursor on the next line. I can enter text, but there's nothing that appears to be processing my commands (it certainly doesn't look like the node debugger):
$ mocha --debug-brk tests.js -R spec
debugger listening on port 5858
[BLANK CURSOR]
Am I doing something wrong with how I'm launching mocha?
UPDATE
As of mocha 7.0.0, --debug-brk has been removed in favor of --inspect-brk
Using a recent version of nodejs (>=v6.3.0) and mocha (>=3.1.0), you can use V8 inspector integration.
V8 Inspector integration allows attaching Chrome DevTools to Node.js
instances for debugging and profiling
Usage
--inspect activates V8 inspector integration, and --debug-brk adds a breakpoint at the beginning. Since nodejs v7.6.0 and mocha v3.3.0, you can use the --inspect-brk shorthand for --inspect --debug-brk
$ mocha --debug-brk --inspect
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/remote/serve_file/#62cd277117e6f8ec53e31b1be58290a6f7ab42ef/inspector.html?experiments=true&v8only=true&ws=localhost:9229/node
With npm scripts
If you have a npm test script that uses mocha, you can pass the options from npm to your mocha script by using the end of option delimiter --:
$ npm test -- --inspect --debug-brk
Chrome tip
Instead of copy-pasting the url each time, go to chrome://inspect and click the appropriate link in the "Remote target" section.
To answer the original question, even though I also suggest you look into node-inspector: you can use the CLI debugger built into node through mocha with the debug option, instead of the --debug or --debug-brk flags. (Notice the lack of dashes.)
In your example, instead, it would be:
$ mocha debug tests.js -R spec
debugger listening on port 5858
connecting... ok
break in node_modules/mocha/bin/_mocha:7
5 */
6
7 var program = require('commander')
8 , sprintf = require('util').format
9 , path = require('path')
debug> [CURSOR]
Again, debug as above in bold, with no dashes. (=
Relevant: https://github.com/visionmedia/mocha/issues/247
I was able to get this to work using node-inspector. I run my test like you show in one shell:
mocha --debug-brk mocha/test.js
and then run node-inspector in a second shell:
node-inspector
Bringing up the URL that node-inspector spits out in a browser allows me to debug with the web inspector.
http://127.0.0.1:8080/debug?port=5858
If you have node-insector installed you can debug you Mocha tests without actually running node-inspector first. The easiest way is to
node-debug _mocha
That should start debugging the node tests in chrome (also works on Safari)
One reason I have seen that the tests do not work is sometimes you gave to try http://localhost:8080/debug?port=5858 or http://127.0.0.1:8080/debug?port=5858
run mocha with flag --inspect-brk and click 'open dedicated DevTools for node' in chrome from page chrome://inspect. In dedicated DevTools window add connection localhost:9229 to connect automatically.
Also add a debugger statement to the file you want debug.
(this is using latest versions of node and chrome as of October 2017)

Categories