0

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!

6
  • Is the value of env being set to development? Or is there something else in process.env.NODE_ENV Commented Jul 12, 2018 at 1:27
  • Yep, I think that Azure is setting NODE_ENV to production. Commented Jul 12, 2018 at 1:28
  • I duplicated the development set in config.json and renamed to "production" so will see if that does it. Deploying now... Commented Jul 12, 2018 at 1:31
  • 2
    You shouldn't need to path.join() to get to something like a config.json. just require('../config/config.json'). If you do need to use path.join, then make sure you get that ready first; const path = require('path'); Commented Jul 12, 2018 at 1:35
  • @jfriend00 require() handles the differences between platforms for you, you can separate path components in require() using '/' on any platform. But, you do need to require('path') before you can use it. nodejs.org/api/path.html Commented Jul 12, 2018 at 2:16

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

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.