5

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:

enter image description here

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:

enter image description here

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:

enter image description here

But I'd like to figure out how to use launch.json properly so that I can pass environments variables etc via that instead.

drmrbrewer
  • 11,491
  • 21
  • 85
  • 181

2 Answers2

1

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

John
  • 5,942
  • 3
  • 42
  • 79
  • Exactly the same happens with this `launch.json` too... it seems as if the configuration (including the `--experimental-json-modules` arg) is just being completely ignored, so that it is using the default node.js launch configuration, without any args. Maybe I'm just launching this incorrectly... how are you doing it? The way I'm doing it is in the original post. – drmrbrewer Dec 02 '21 at 14:33
  • @drmrbrewer I select the profile and then press F5. I just gave that switch a test and it worked. https://i.imgur.com/CiRXX6X.png – John Dec 02 '21 at 15:34
  • Yep. F5 is the equivalent of what I was doing... pressing the "play" button next to the profile name... still I get the same error and the runtime args specified are just being completely ignored... no idea what's going on. – drmrbrewer Dec 02 '21 at 23:14
0

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"
        }

    }
]
}
MatteoPHRE
  • 358
  • 4
  • 10
  • Exactly the same happens with this `launch.json` too... it seems as if the configuration is just being completely ignored, so that it is using the default node.js launch configuration. – drmrbrewer Nov 30 '21 at 20:35