basickarl
basickarl

Reputation: 40551

How to set NODE_PATH to $(pwd) when debugging in vscode

I'm currently using the following script to startup my nodejs application. I thereafter attach the vscode debugger to it, this works:

"dev": "NODE_PATH=\"$(pwd)\" NODE_ENV=development nodemon -r babel-register -r babel-polyfill --nolazy --debug-brk=36598 ./src/index.js"

I'd like to however use the inbuilt "launch" option in vscode.

In the launch.json under configurations under a configuration there is a field called env. I'd like to add the NODE_PATH there setting it's value to the current directory.

{
    "name": "Launch",
    "type": "node",
    "request": "launch",
    "program": "${workspaceRoot}/src/index.js",
    "stopOnEntry": false,
    "args": [],
    "cwd": "${workspaceRoot}",
    "preLaunchTask": null,
    "runtimeExecutable": null,
    "runtimeArgs": [
        "-r",
        "babel-register",
        "-r",
        "babel-polyfill",
        "--nolazy"
    ],
    "env": {
        "NODE_ENV": "development",
        "NODE_PATH": "$(pwd)" // <--- here
    },
    "console": "internalConsole",
    "sourceMaps": false,
    "outDir": null
}

The issue here is that the variable is actually set to "$(pwd)" instead of running it as a command and saving the output.

When I have the following in my code:

console.log(process.env.NODE_PATH);

It outputs the following (instead of the current directory path):

$(pwd) // <--- wrong, I was expecting '/home/karl/dev/my_project'

I tried setting "NODE_PATH=\"$(pwd)\"", in the runtimeArgs and args but that doesn't work either.

Any ideas?

Upvotes: 2

Views: 7623

Answers (3)

triptych
triptych

Reputation: 170

You might also try "runtimeExecutable": "your/path/to/bin/node"

Upvotes: 2

Nikhil Seepana
Nikhil Seepana

Reputation: 21

{

"version": "0.2.0",
"configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Launch Program",
        "program": "/Path_to_start_file/main.js",
        "env":{
            "NODE_PATH": "/Path_to_node_modules/node_modules"
        }
    }
] }

This Solved the problem for me.

Upvotes: 2

Juraj Paulo
Juraj Paulo

Reputation: 353

You can use ${cwd} - the task runner's current working directory on startup, as specified on VS Code Variable substitution

so in your case

"NODE_PATH": "${cwd}"

Upvotes: 1

Related Questions