8

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

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

1 Comment

"envFile": "${workspaceFolder}/.env" worked for me.
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.