eliocs
eliocs

Reputation: 18827

Can I add a debug script to NPM?

I have edited my package.json to customize the "start" script so it adds the --debug flag to node:

  "scripts": {
    "start": "node --debug server.js"
  }

Is there a way of adding new scripts for example a debug script that would do what my customized "start" is doing right now?

I'm looking to be able to execute:

npm debug

Upvotes: 52

Views: 110205

Answers (5)

Aidin
Aidin

Reputation: 30067

  1. In VS Code hit cmd+shift+p (or View > Command Palette...)
  2. Type npm and select Debug: Debug npm Script
  3. Hit enter and pick the script that you want from the scripts of package.json.

enter image description here

Upvotes: 3

Liuer Hei
Liuer Hei

Reputation: 179

for remote debug get npm-cli.js path

> npm --verbose
npm info it worked if it ends with ok
npm verb cli [
npm verb cli   'C:\\Program Files\\nodejs\\node.exe',
npm verb cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
npm verb cli   '--verbose'
npm verb cli ]
npm info using [email protected]
npm info using [email protected]

then

node --inspect-brk "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" run your-npm-script

for mac or linux

➜  ~ npm --verbose
npm info it worked if it ends with ok
npm verb cli [
npm verb cli   '/usr/local/Cellar/node@14/14.18.2/bin/node',
npm verb cli   '/usr/local/bin/npm',
npm verb cli   '--verbose'
npm verb cli ]
npm info using [email protected]
npm info using [email protected]
node --inspect-brk "/usr/local/bin/npm" run your-npm-script

Upvotes: 1

KyleMit
KyleMit

Reputation: 30107

VS Code adds a debug button inline in the package.json file

  1. Open package.json and click debug above scripts section

    code npm package debug

  2. Select script to debug

    code npm package debug options

  3. Debugger should be attached

    code debugger

Upvotes: 22

Dan Midwood
Dan Midwood

Reputation: 19444

In your package.json define the script

"scripts": {
  "debug": "node --inspect server.js"
}

And then you can use npm's run-script

npm run-script debug

or the shorter version

npm run debug

Upvotes: 114

MatayoshiMariano
MatayoshiMariano

Reputation: 2116

From the nodejs docs:

The legacy debugger has been deprecated as of Node 7.7.0. Please use --inspect and Inspector instead.

So starting from Node 7.7.0v use --inspect

Upvotes: 16

Related Questions