pedalpete
pedalpete

Reputation: 21536

getting node env variables in typescript

I'm new to typescript and can't seem to access my process.env variables in my typescript pages. It seems it's a scope issue, but that makes no sense.

I get my environment variables from a yaml file and the attach them to the running process.

module.exports = function() {
   const YAML = require('yamljs');
    const envVars = YAML.load('env.yml')[process.env.NODE_ENV];
    Object.keys(envVars).forEach(v => {
        console.log('vars', v);
        process.env[v] = envVars[v];
    });
};

I then run my typescript in npm with

cross-env NODE_ENV=test node -e \"require('./setup-env')()\" && jasmine-ts **/*.spec.ts

I can see the console for each var in the loadYmlEnv but when I try to console log the vars from my typescript files, they are all undefined. I can spit out the entire process.env and the env vars I need are not there.... strange

Upvotes: 1

Views: 2103

Answers (1)

Lpc_dark
Lpc_dark

Reputation: 2940

cross-env NODE_ENV=test node -e \"require('./setup-env')()\" && jasmine-ts **/*.spec.ts

This creates a process with NODE_ENV set to test

Then you create a different process that requires 'setup-env' and then that process exits

Then you run jasmine-ts that only have NODE_ENV=test set up

Upvotes: 2

Related Questions