Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store Node.js deployment settings/configuration files?

I have been working on a few Node apps, and I've been looking for a good pattern of storing deployment-related settings. In the Django world (where I come from), the common practise would be to have a settings.py file containing the standard settings (timezone, etc), and then a local_settings.py for deployment specific settings, ie. what database to talk to, what memcache socket, e-mail address for the admins and so on.

I have been looking for similar patterns for Node. Just a config file would be nice, so it does not have to be jammed in with everything else in app.js, but I find it important to have a way to have server-specific configuration in a file that is not in source control. The same app could well be deployed across different servers with wildly different settings, and having to deal with merge conflicts and all that is not my idea of fun.

So is there some kind of framework/tool for this, or does everyone just hack something together themselves?

like image 674
mikl Avatar asked May 03 '11 12:05

mikl


People also ask

How do I include a node JS config file?

Create a config directory and add a config/default. json file to it. This will be the default config file and will contain all your default environment variables. We'll access it in our app by importing config and using the get method to access the variables.

Where is node js configuration file?

The configuration files are located in the default config directory. The location can be overriden with the NODE_CONFIG_DIR environment variable. The NODE_ENV environment variable contains the name of our application's deployment environment; it is development by default.

Where is .ENV file in node?

The dotenv package for handling environment variables is the most popular option in the Node. js community. You can create an. env file in the application's root directory that contains key/value pairs defining the project's required environment variables.

What is config module in node JS?

Node-config organizes hierarchical configurations for your app deployments. It lets you define a set of default parameters, and extend them for different deployment environments (development, qa, staging, production, etc.).


1 Answers

I use a package.json for my packages and a config.js for my configuration, which looks like:

var config = {};  config.twitter = {}; config.redis = {}; config.web = {};  config.default_stuff =  ['red','green','blue','apple','yellow','orange','politics']; config.twitter.user_name = process.env.TWITTER_USER || 'username'; config.twitter.password=  process.env.TWITTER_PASSWORD || 'password'; config.redis.uri = process.env.DUOSTACK_DB_REDIS; config.redis.host = 'hostname'; config.redis.port = 6379; config.web.port = process.env.WEB_PORT || 9980;  module.exports = config; 

I load the config from my project:

var config = require('./config'); 

and then I can access my things from config.db_host, config.db_port, etc... This lets me either use hardcoded parameters, or parameters stored in environmental variables if I don't want to store passwords in source control.

I also generate a package.json and insert a dependencies section:

"dependencies": {   "cradle": "0.5.5",   "jade": "0.10.4",   "redis": "0.5.11",   "socket.io": "0.6.16",   "twitter-node": "0.0.2",   "express": "2.2.0" } 

When I clone the project to my local machine, I run npm install to install the packages. More info on that here.

The project is stored in GitHub, with remotes added for my production server.

like image 82
noli Avatar answered Nov 22 '22 10:11

noli