Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variables are not being defined while debugging #102

Closed
brennanMKE opened this issue Nov 18, 2015 · 9 comments
Closed

Environment variables are not being defined while debugging #102

brennanMKE opened this issue Nov 18, 2015 · 9 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug verified Verification succeeded
Milestone

Comments

@brennanMKE
Copy link

I use VS Code to start a debugging session and in the configuration I define a value for PORT yet it is always undefined. I cannot seem to access these environment variables. Is there another way to access these environment variables?

var port = process.env.PORT || 8001;

Below is the launch.json that I am using for a sample project to try to access environment variables. I'd like to be able to use launch configurations to create multiple configurations with different environment variables.

{
    "version": "0.1.0",
    // List of configurations. Add new configurations or edit existing ones.
    "configurations": [
        {
            // Name of configuration; appears in the launch configuration drop down menu.
            "name": "Launch server.js",
            // Type of configuration.
            "type": "node",
            // Workspace relative or absolute path to the program.
            "program": "server.js",
            // Automatically stop program after launch.
            "stopOnEntry": false,
            // Command line arguments passed to the program.
            "args": [],
            // Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
            "cwd": ".",
            // Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
            "runtimeExecutable": null,
            // Optional arguments passed to the runtime executable.
            "runtimeArgs": ["--nolazy"],
            // Environment variables passed to the program.
            "env": {
                "NODE_ENV": "development",
                "PORT" : 8005
            },
            // Use JavaScript source maps (if they exist).
            "sourceMaps": false,
            // If JavaScript source maps are enabled, the generated code is expected in this directory.
            "outDir": null
        },
        {
            "name": "Attach",
            "type": "node",
            // TCP/IP address. Default is "localhost".
            "address": "localhost",
            // Port to attach to.
            "port": 5858,
            "sourceMaps": false
        }
    ]
}
@weinand weinand self-assigned this Nov 18, 2015
@weinand
Copy link
Contributor

weinand commented Nov 19, 2015

The "env" attribute is a dictionary of strings. Please pass the PORT number as a string and not as a number.
We will try to improve the schema for "env" so that your case will be flagged as an error.

@weinand weinand assigned isidorn and unassigned weinand Nov 19, 2015
@weinand weinand added this to the Nov 2015 - Recovery milestone Nov 19, 2015
@weinand weinand added the bug Issue identified by VS Code Team member as probable bug label Nov 19, 2015
@brennanMKE
Copy link
Author

Thanks. I saw lower in the launch config that there was another port value which is a number and was copying that instance. Is it possible to define these as numbers?

@egamma egamma modified the milestones: Nov 2015 - end, Dec 2015 Nov 23, 2015
@isidorn isidorn assigned weinand and unassigned isidorn Dec 15, 2015
@weinand weinand added the verified Verification succeeded label Dec 16, 2015
@montera82
Copy link

Any update per this issue, i am getting undefined for my environment variables while in debug mode

@weinand
Copy link
Contributor

weinand commented Aug 25, 2016

@montera82 the issue discussed here was a 'user error' and might only be related to your problem if you are using non-string typed values in the "env" section of your launch configuration. If this is the case please fix it as discussed above.

If you have a different problem, please create a new issue with the detailed steps and information about your OS and node version.

@montera82
Copy link

hi @weinand ,
i need to know how to source my environment variables when on debug mode.

All my environment variables are stored in a file in my root folder named .env ( which has an entry like so
export DATABASE_HOST=127.0.0.1
)

Now when running the project from command line, i can do

Source .env

Then run

npm start

and the project will be able to resolve the variables.

How do i achieve the same when running same project on debug mode.

Thanks

@weinand
Copy link
Contributor

weinand commented Aug 25, 2016

@montera82 you probably have already a launch.json that has a environment variable:

"env": {
    "NODE_ENV": "development"
},

Just add your environment variables to that like this:

"env": {
    "NODE_ENV": "development",
    "DATABASE_HOST": "127.0.0.1"
    // more env vars
},

Make sure that all env var values are strings.

You can find debugging help here: https://code.visualstudio.com/docs/editor/debugging

@montera82
Copy link

Works well, thanks

someone will need to add this on the getting started article, i could not find there.

@weinand
Copy link
Contributor

weinand commented Aug 25, 2016

/cc @gregvanl

@Dewb
Copy link

Dewb commented Apr 6, 2017

Is there any other route for defining runtime env-vars other than the tasks.json file? For example, something that would be suitable for secrets that shouldn't be in source control?

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 18, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug verified Verification succeeded
Projects
None yet
Development

No branches or pull requests

6 participants