Node.js: Include another JS, ENV, JSON file in the app

This article shows how you can include another JS (Javascript) file into your main application JS file.

We can have a situation where we store configuration settings or database settings in another file. It can be .js, .env, .json or any other file. We will be using module.exports object to achieve this.

I have created a folder named “nodejs” inside my Documents folder. And, then created a simple Node.js app over there.


mukesh:nodejs chapagain$ pwd
/Users/mukeshchapagain/Documents/nodejs

mukesh:nodejs chapagain$ ls
app.js      config

Including JS file

Here,

– the main application file is app.js
– the file to be included is in config folder: config/settings.js

We include the config/settings.js file in our main app js file i.e. app.js.

1) RETURN SIMPLE STRING

include/settings.js


module.exports = 'Hello World';

app.js


var settings = require('./config/settings');
console.log(settings);

Output


mukesh:nodejs chapagain$ node app.js
Hello World

2) RETURN PROPERTY / METHOD

include/settings.js


module.exports.myMessage = 'My simple message';

app.js


var settings = require('./config/settings');
console.log(settings);
console.log(settings.myMessage);

Output


mukesh:nodejs chapagain$ node app.js
{ myMessage: 'My simple message' }
My simple message

3) RETURN METHOD / FUNCTION

include/settings.js


module.exports.myMessage = 'My simple message';

module.exports.add = function(num1, num2) {
    return num1 + num2;
}

app.js


var settings = require('./config/settings');
console.log(settings);
console.log(settings.myMessage);
console.log(settings.add(1, 2));

Output


mukesh:nodejs chapagain$ node app.js
{ myMessage: 'My simple message', add: [Function] }
My simple message
3

4) RETURN JSON OBJECT

We can export a JSON object that can contain variable and functions within it.

include/settings.js


var addFunc = function(num1, num2) {
    return num1 + num2;
}

module.exports = {
    myMessage: 'My simple message',
    add: addFunc
}

app.js


var settings = require('./config/settings');
console.log(settings);
console.log(settings.myMessage);
console.log(settings.add(1, 2));

Output


mukesh:nodejs chapagain$ node app.js
{ myMessage: 'My simple message', add: [Function] }
My simple message
3

Including JSON file

You don’t require export statements for getting data from JSON files.

Here,

– the main application file is app.js
– the JSON file to be included is in config folder: config/settings.json

We include the config/settings.json file in our main app js file i.e. app.js.

include/settings.json


{
    "myMessage": "My simple message",
    "anotherMessage": "Another Message" 
}

app.js


var settings = require('./config/settings.json');
console.log(settings);
console.log(settings.myMessage);
console.log(settings.anotherMessage);

Output


mukesh:nodejs chapagain$ node app.js
{ myMessage: 'My simple message',
  anotherMessage: 'Another Message' }
My simple message
Another Message

Including ENV file

We will install the dotenv module to access the .env file contents.


npm install dotenv --save

Here,

– the main application file is app.js
– the environment variables are stored in .env

Note:

Remember to ignore the .env file in your git repository by creating a .gitignore file and adding the .env line over there. Otherwise, your private information stored in the .env file might get exposed.

dotenv module is used to include the .env file in our main app js file.

.env


DB_HOST=localhost
DB_USER=yourDbUsername
DB_PASS=yourDbPassword

app.js


require('dotenv').config(); // include .env file

let db = {
    host: process.env.DB_HOST,
    username: process.env.DB_USER,
    password: process.env.DB_PASS
};

console.log(db);

Output


mukesh:nodejs chapagain$ node app.js
{ host: 'localhost',
  username: 'yourDbUsername',
  password: 'yourDbPassword' }

Hope this helps. Thanks.