-
Notifications
You must be signed in to change notification settings - Fork 29.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add launch option that allows run node project by npm script #2726
Comments
@Ciget a VS Code 'launch' configuration is used to launch 'node' in debug mode listening on a specific port. I'm not aware of a way to pass the debug-port to node via an "npm start". If you know a way how to do this, please let me know and we can consider to support "npm" as a launch option. Thanks. |
@weinand, thanks for quick reply. I`m asking this, because most of top solutions use this approach to run. So, does it means, that using VS Code we will do own way to develop app? |
@Ciget no, you don't have to change the way of developing an app. To start your app in debug mode you can use "npm dev" from the command line. I assume that "npm dev" runs your app in debug mode like this: "scripts": {
"dev": "node --debug=12345 server.js"
} In VS Code create an 'attach' configuration: {
"name": "Attach to server.js",
"type": "node",
"request": "attach",
"port": 12345
} Make sure that the debug port is identical to the one used in the "dev" script (or is 5858 if no port is specified in the dev script). |
I was about to write the same, I've just used a more complex script as an example. "scripts": {
"build_test": "rm -rf dist/ && npm run lint && npm run build_cjs && jasmine",
"build_test_debug": "rm -rf dist/ && npm run lint && npm run build_cjs && jasmine --debug-brk
} This requires still two steps 1) npm run build_test_debug 2) F5 to attach. |
@egamma yes, it makes sense to run anything (including npm scripts) as a |
I'd also be interested in a way for the Launch feature to execute an npm task. 👍 Also, FWIW, to run an arbitrary npm script like "dev" that isn't a part of the standard scripts use npm-run-script:
|
I had posted on Stack Overflow about this exactly. It would be nice if this was more easily supported. |
You all seem to have a clear picture how this feature should work. A VS Code 'launch' configuration needs to pass the standard node debug argument '--debug-brk' and a port number to the runtime (e.g. 'node'). If the runtime is launched via an npm script, I still need a mechanism to pass the debug port into that script. What is the way to do this? |
I wonder if there is an environment variable alternative to Update:
|
@jpierson I don't see how node-inspector can help here? I'm looking for a way to pass a port number to an npm script. Ideally something like this:
VS Code would use this to pass a debug port to node and subsequently VS Code would try to attach to that port. And the npm script would look like this: "scripts": {
"debug": "node --debug-brk=${port} program.js",
}, |
I think with So how about
|
In my package.json:
In my launch.json:
I'm wondering if I can debug "npm start", "npm run debug" or not? |
Alright, +1 to this. I'm writing Electron application and launch via Any way to apply this to |
@Z-AX until I find the time to add this feature here is a setup for OS X and linux that uses an npm script to launch a program in debug mode. Unpack this project zip and make the included script Open the project with VS Code and set a breakpoint in hello.js:2. Start debugging: the breakpoint should be hit. You will have to adapt the script Two things are important:
|
@weinand, thanks for reply, but until it's supported, will better work as before :) |
Since the port number might make it difficult to adopt my approach from above, I will add support for a I've created this feature request for this. |
For reference/inspiration, here is a npm runner, with debugging, that I created as a Custom Runner for Cloud9 (a competing IDE). Their feature is in turn inspired by the Sublime build system.
|
Saw the feature request is fulfilled and closed. So how to use it? Still can't get all the pieces together. How will the launch.json looks like if I want to run |
@unional no, this issue is not closed. |
Thanks. I thought the update on microsoft/vscode-node-debug#60 would enable this. Look forward for this feature to land! 😄 I also running into difficulty in attaching debug (or launch) to Thanks, |
The problem is to run the script with the debugger already attached. |
Try this: In you package.json add a 'debug' script, e.g.: "scripts": {
"start": "node ./bin/www",
"debug": "node --nolazy --debug-brk=5858"
}, In your launch config set attribute {
"name": "Launch via NPM",
"type": "node",
"request": "launch",
"stopOnEntry": true,
"program": "${workspaceRoot}/bin/www",
"args": [],
"cwd": "${workspaceRoot}",
"runtimeExecutable": "/Users/xxxxxx/.npm-packages/bin/npm",
"runtimeArgs": [
"run-script", "debug"
],
"port": 5858
} Please note that since the |
REDACTED I think that it's important to note. Not everyone on dev teams will use the same tooling. I don't think it can be expected to alter the launch scripts in lieu of a single IDE that doesn't support it. |
@omayhemo |
No it doesn't work for me, unless I'm missing something. Putting the runtime path to the npm location results in the ENOENT error. I've tried several different paths here, all ultimately ending in failure. Additionally our teams make heavy use of gulp serve and browserify. |
@omayhemo if all other IDEs support npm scripts for launching the target in debug mode, then you can probably help me understanding how these IDEs run the target in debug mode through npm. And in what ENOENT error are you running? If you use |
@omayhemo for the gulp serve and browserify issues I suggest to create a new issues so that we can track them individually. |
npm-which helped me correct the path. Thanks for that suggestion. That did indeed allow me to launch with your prescribed solution. Minus, of course, the task runners. I've seen some documentation on task runners in vs code, though i wasn't sure if it was applicable to our situation given our tasks actual 'compile' and serve. It warrants a deeper look before I open an issue. |
@omayhemo good to hear that my prescribed (workaround) solution was not a complete failure ;-) |
You can now use any program as the So if your package.json has a 'debug' script, e.g.: "scripts": {
"debug": "node --nolazy --debug-brk=5858 myProgram.js"
}, the corresponding launch config could look like this: {
"name": "Launch via NPM",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script", "debug"
],
"port": 5858
} |
This would be awesome to document @weinand . I can open a PR for this if it makes sense to add |
Any example with meteor.js? for example using the command npm start. Check my scripts please https://github.com/jdnichollsc/Meteor-Starter-Template/blob/master/package.json Regards, Nicholls |
@jdnichollsc try wrapping the meteor script with |
@Yahkob The following error appeared: npm run-script debug
> example@1.0.0 debug /FrontEnd
> node --debug-brk -- meteor --settings settings.json --port 8890
Debugger listening on [::]:5858
module.js:471
throw err;
^
Error: Cannot find module '/FrontEnd/meteor'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Timeout.Module.runMain [as _onTimeout] (module.js:604:10)
at ontimeout (timers.js:365:14)
at tryOnTimeout (timers.js:237:5)
at Timer.listOnTimeout (timers.js:207:5)
npm ERR! Darwin 15.6.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run-script" "debug"
npm ERR! node v6.9.1
npm ERR! npm v3.10.8
npm ERR! code ELIFECYCLE
npm ERR! example@1.0.0 debug: `node --debug-brk -- meteor --settings settings.json --port 8890`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the example@1.0.0 debug script 'node --debug-brk -- meteor --settings settings.json --port 8890'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the bidenergy package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node --debug-brk -- meteor --settings settings.json --port 8890
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs example
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls example
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /FrontEnd/npm-debug.log Using the following configuration
{
"name": "example",
"version": "1.0.0",
"description": "Application description.",
"scripts": {
"start": "meteor --settings settings.json --port 8890",
"debug": "node --debug-brk -- meteor --settings settings.json --port 8890"
}
}
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch via NPM",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run-script", "debug"
],
"port": 5858
}
]
} Thanks in advance, Nicholls |
You'll need to provide an absolute path to meteor (i.e. |
Exist any other cross-platform option? (Mac, Linux, Windows) |
On Windows, the "runtimeExecutable" has to be "npm.cmd", not "npm". Otherwise, the Is there a way to make this cross-plateform? |
It would be nice to have a "${runtimeExtension}" variable which would be ".cmd" or "" depending on the platform, so we could simply use : |
@electrotype a "${runtimeExtension}" variable would not be a general solution for all possible task runners because ".cmd" is not the only possible extension on Windows. The following solution is already possible and works in all cases: "runtimeExecutable": "npm",
"windows": {
"runtimeExecutable": "npm.cmd"
}, |
@weinand I don't want to be too much off-topic, but with everything I've learned in this thread, I'm still not able to properly debug a Node.js application based on TypeScript with auto recompilation and application restart in VSCode. I asked my question on StackOverflow, with a project example. If you have some time to check it out, it would be really appreciated!! |
@electrotype I've commented on StackOverflow. BTW, I've created a feature request for your scenario. |
Hi all,
Looks like current trend to run NodeJs app via npm start or npm dev.
By default, launch.json does not allow us to do this. It`s very unusable. My suggection - to add this feature, that allow easily cconfigure how to run app and not just by parameter "node" and entry point to the site.
Thanks.
The text was updated successfully, but these errors were encountered: