Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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
like image 579
RGKrish183 Avatar asked Feb 07 '19 06:02

RGKrish183


People also ask

How do I Debug a breakpoint in Visual Studio?

To set a breakpoint in source code, click in the far left margin next to a line of code. You can also select the line and press F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert breakpoint. The breakpoint appears as a red dot in the left margin.

How do you run in Debug mode in Visual Studio Code?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

What is DotEnv NPM?

DotEnv is a lightweight npm package that automatically loads environment variables from a . env file into the process.


1 Answers

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.

like image 86
jgraup Avatar answered Nov 14 '22 15:11

jgraup