简体   繁体   中英

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?

{

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

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

您也可以尝试“ runtimeExecutable”:“您的/路径/到/ bin /节点”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM