zngb
zngb

Reputation: 633

Node js require()[env] undefined

Brand new to node trying to understand a project. The project loads on dev on a windows machine just fine, but throws this error on Azure App Services..

TypeError: Cannot read property 'database' of undefined

when trying: config.database for example in index.js  

models/index.js

const env = process.env.NODE_ENV || "development";
const config = require(path.join(__dirname, '..', 'config', 'config.json'))[env];
>>> var whatever = config.database; <<<

config/config.json

{
  "development": {
    "logging": false, 
    "dialect": "mssql",
    "username": "whatever",
    "password": "whatever",
    "database": "whatever",
    "host": "whatever",
    "dialectOptions": {
      "encrypt": true
    }
  }
}

No idea where to start with this since it works fine on local. The file location seems correct, path is valid, path.join() works fine...

Thank you!

Upvotes: 0

Views: 1704

Answers (1)

jfriend00
jfriend00

Reputation: 708146

There is really only one main possibility here and you need to do a little bit of your own debugging to verify it.

Because this statement does not generate an error:

const config = require(path.join(__dirname, '..', 'config', 'config.json'))[env];

That means that require(path.join(__dirname, '..', 'config', 'config.json')) is returning a Javascript object. So, there must indeed be a file at the path you construct and it must be giving you an object when you require() it.

But, the error itself: TypeError: Cannot read property 'database' of undefined when you try to reference config.database means that config is undefined. The only way that happens is that you're using a value for env that is not in your config object.

That's like trying to do this:

const obj = {development: {someKey: "someValue"}};  // what require() gives you
const env = "test";                                 // your value for env
const config = obj[env];                            // reading non-existent [env] property
console.log(config);                                // undefined

So, add console.log(env) to your code to see what env actually is and then verify that you have an object in your config data structure for that specific value (or set the desired value in the environment).

If you had verified the value of env yourself with basic debugging steps before posting here, you probably would have solved your own problem (hoping you can learn simple techniques to solve more of your own problems).

If you didn't follow all of my original logic, you could also just debug it like this:

const env = process.env.NODE_ENV || "development";
console.log("env: ", env);

const configPath = path.join(__dirname, '..', 'config', 'config.json');
console.log("configPath: ", configPath);

const configObj = require(configPath);
console.log("configObj: ", configObj);

const config = configObj[env];
console.log("config: ", config);

Upvotes: 2

Related Questions