Reputation: 13029
I'm trying to create a configuration in my launch.json which will run npm test
in the folder in which the .js
file resides. Running npm test
manually in a terminal works fine, taking the relevant command from the scripts
part of my package.json
:
"scripts": {
"start": "node --experimental-json-modules nodeserver.js",
"test": "export MY_VAR=abc && node --experimental-json-modules nodeserver.js"
},
In particular, when running npm test
directly in a terminal, the env var specified in the test
script line takes effect and the --experimental-json-modules
flag is passed to node
.
This is my launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
}
]
}
This is pretty much as-is from one of the predefined options suggested in the editor, and is very similar to this.
But when I run this configuration on the nodeserver.js
file, I get:
It seems to be running node
without the flag I specified in the configuration? What am I misunderstanding about how this launch.json
scheme works?
EDIT the more I've played around, the more it seems as if the configuration is just being completely ignored, so that it is using the default node.js configuration... I'm selecting the config from the drop-down and pressing the play icon:
Should that work?
Apart from running npm start
in a terminal, the only "automatic" way of getting this to work is by opening the package.json
and clicking on the little Debug button which appears by the scripts
tag:
But I'd like to figure out how to use launch.json
properly so that I can pass environments variables etc via that instead.
Upvotes: 5
Views: 10014
Reputation: 6278
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\index.js"
},
{
"type": "pwa-node",
"request": "launch",
"name": "Run Test",
"skipFiles":
[
"<node_internals>/**"
],
// You can specify enviorment variables per config here
// using key value pairs
"env":
{
"test_variable": "test value"
},
// You can also specify a .env file that contains them
"envFile": "${workspaceFolder}/.env",
// Here you specify the file you want launched
"program": "${workspaceFolder}\\test.js",
// add args to nodejs here
"runtimeArgs":
[
"--experimental-json-modules"
],
}
]
}
For reference: https://code.visualstudio.com/docs/nodejs/nodejs-debugging
Upvotes: 0
Reputation: 388
You can try to create the npm test script directly in your launch.json as above:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Run npm test",
"request": "launch",
"type": "node",
"args": ["--experimental-json-modules", "${workspaceFolder}/nodeserver"],
"env": {
"MY_VAR": "abc"
}
}
]
}
Upvotes: 2