In VS Code-debugger, how do I use envFile in launch.json for nodejs? - node.js

I'm trying to run the debugger in VS Code on my nodejs application. I'm using an .env file to store environment variables that I later call with process.env.. When I looked up the VS Code docs for the launch.json, it mentions the envFile option to load the the .envFile. Unfortunately, this is not loading the files when I run the debugger.
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "RegressionTestSuite",
"autoAttachChildProcesses": true,
"program": "node ${workspaceFolder}/node_modules/.bin/cucumber-js",
"args": [
],
"envFile": "${workspaceFolder}/.env"
},
]
}
.env:
export SCREEN_SIZE_WIDTH='1366';
export SCREEN_SIZE_HEIGHT='768';
When I run the VS Code debugger, there are no environment variables from my .env file. How should I be calling the .env file in the launch.json?

You can try this to load the env file.
{
// 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": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}\\Chat\\server.js",
"envFile": "${workspaceFolder}\\Chat\\.env"
}
]
}

I would use the dotenv package to load your .env file, as it can be used by people who aren't using VS Code as well. If you want to include it in your VS Code config, you could do:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "RegressionTestSuite",
"autoAttachChildProcesses": true,
"program": "node -r dotenv/config ${workspaceFolder}/node_modules/.bin/cucumber-js",
"args": []
},
]
}
Your problem could also be that your .env file should not contain export and semi-colons, as it is not a JavaScript/shell file:
SCREEN_SIZE_WIDTH=1366
SCREEN_SIZE_HEIGHT=768

Related

.env files not getting loaded when running node server with nodemon

Folder structure
src
server
server.js
.env
I am running a node server with nodemon using the below launch.json, but the environment variables are not being loaded.
{
// 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": [
{
"type": "node",
"request": "launch",
"name": "Nodemon",
"runtimeExecutable": "nodemon",
"program": "${workspaceFolder}/src/server/server.js",
"restart": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"envFile": "${workspaceFolder}/src/server/.env",
}
]
}
Edit:
Used "envFile": "${workspaceFolder}/src/server/.env",
Instead of: "runtimeArgs": [
"--require=dotenv/config"
]
Now all the env variables are loaded!
Used: "envFile": "${workspaceFolder}/src/server/.env",
Instead of: "runtimeArgs": [ "--require=dotenv/config" ]
Now all the env variables are loaded!

How to pass arguments to node script from launch configuration in vscode

Why isn't my launch configuration in vscode picking up on args key?
vscode Version: 1.63.2
launch.json
"version": "0.2.0",
"configurations": [
{
"command": "node ${workspaceFolder}/test.js",
"args": ["foo"],
"name": "test",
"request": "launch",
"type": "node-terminal",
},
]
}
test.js
console.log("running script");
console.log(process.argv);
I press play from debug sidebar and get this in console:
$ node /Users/acohen/mp/mobileapp-client-tests/test.js
Debugger attached.
running script
[
'/Users/acohen/.nvm/versions/node/v12.16.2/bin/node',
'/Users/acohen/mp/mobileapp-client-tests/test.js'
]
Waiting for the debugger to disconnect...
Why isn't "foo" passed as an argument?
Here is a config example that worked for me:
{
"configurations": [
{
"name": "Launch Program",
"args": ["foo"],
"program": "${workspaceFolder}/test.js",
"request": "launch",
"type": "pwa-node"
}
]
}
Ok, what I ultimately wanted to do was run a complex npm script and debug it. I re-installed vscode, and found some good docs here:
https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-support-for-npm-and-other-tools
and here for using nvm:
https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_multi-version-support
My launch.json looks like this:
where test_ios is the npm script to debug and 14.17.3 is the version of node (installed with nvm) to use:
"version": "0.2.0",
"configurations": [
{
"name": "Launch via npm",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": ["run-script", "test_ios"],
"runtimeVersion": "14.17.3"
}
]
}

Cannot find runtime 'node' on PATH. Is 'node' installed?

I tried to run my discord.js code and the came an Pop-Up after I killed the node process in the task manager
Here is my launch.json file code:
{
// 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": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/Bot/main.js"
}
]
}
You can try editing the launch.json file code to include your NodeJS path:
{
// 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": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/Bot/main.js",
"runtimeExecutable": "---YOUR NODE JS PATH---"
}
]
}
Also, for this kind of issue simply restarting the system might work.

VS Code debug with --experimental-modules flag

I can run my node.js application with es6 modules with the --experimental-modules flag as follows:
node --experimental-modules ./bin/www
How can I do the same when debuggging the application from VS Code, which uses the launch.json configuration file?
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\bin\\www",
}
]
}
Instead of using the "program" key you can pass the needed flag in "args" before you pass it your module.
node --experimental-modules ./myModule
{
"version": "0.2.0",
"configurations": [
{
"name": "run module",
"type": "node",
"request": "launch",
"args": ["--experimental-modules", "${workspaceFolder}/myModule"]
}
]
}
I only learned this last week trying to do the same thing with a Python program.
The official guidance is to use the runtimeArgs attribute like this:
{
"name": "Launch Program",
"request": "launch",
"type": "node",
"runtimeArgs": ["--experimental-modules"],
"program": "${workspaceFolder}\\bin\\www"
}
Which will run the following terminal command:
node.exe --experimental-modules .\bin\www
See Also: How to start nodejs with custom params from vscode

.env with debugging breakpoint using vscode and dotenv npm

I'm using a nodejs server side api, setting up environment variables with dotenv npm package, and running the code from npm scripts in package.json as below:
"scripts": {
"local": "cross-env NODE_ENV=local nodemon ./bin/www"
}
What I need is to configure my .vscode/launch.json file.
Currently it looks like:
{
// 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": []
}
Kindly guide me. Thanks,
Gopal.R
dotenv npm package
Visual Studio Code - Launch configurations
You would want to set the .dotenv environmental variable up like:
NODE_ENV=local
Then to require it in your debugger, you would want to add it into your launch.json configurations like:
"runtimeArgs": [
"--require=dotenv/config"
]
Here it is in context:
{
// 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": [
{
"type": "node",
"request": "launch",
"name": "Launch | local with dotenv config",
"program": "${workspaceFolder}/bin/www/your_script.js",
"runtimeArgs": [
"--require=dotenv/config"
]
}
]
}
--require=dotenv/config is the equivalent of running require('dotenv').config() in your script or node -r dotenv/config your_script.js if you're using the command line.
Here are some alternate examples of where environmental variables can be placed in the config.
{
// 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": [
{
"type": "node",
"request": "launch",
"name": "Launch | local using env file",
"program": "${workspaceFolder}/bin/www/your_script.js",
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "launch",
"name": "Launch | local without dotenv",
"program": "${workspaceFolder}/bin/www/your_script.js",
"env" : {
"NODE_ENV" : "local"
}
}
]
}
Note: This code hasn't been tested... so feedback is welcome.
I had the same question for typescript debugging and I found answer here. Need to specify runtimeArgs and envFile parameters to make it work.
Example of launch.json for TypeScript debugging:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/src/server.ts",
"preLaunchTask": "tsc: build - tsconfig.json",
"outFiles": [
"${workspaceFolder}/built/**/*.js"
],
"runtimeArgs": [
"--require=dotenv/config"
],
"envFile": "${workspaceFolder}/.env"
}
]
}

Resources