Best way to persist user preferences with npm/nodejs command line utility Best way to persist user preferences with npm/nodejs command line utility bash bash

Best way to persist user preferences with npm/nodejs command line utility


Looking at the questions and comments i think there are two problems to solve:

  1. Where to save it: If there are user specific settings, then by common pattern in Linux its best saved in the hidden directory in user's home directory. Hence you can best keep it in directory $HOME/.{yourAppName}/someFile

    E.x: Pidgin saves it in: /home/kushal/.purple/...("." prefix to folder name makes it hidden)
       You can save it in: /home/$HOME/.myawesomeNPMModule/...

    NOTE: If you are also targeting the windows platform, then you need a platform check and decide which path to use.

    In windows Vista and above it goes in$WIN_INSTALLATION_DRIVE\Users\$USER_NAME\AppData\Local\YourAppName...

    In windows XP it goes in $WIN_INSTALLATION_DRIVE\Documents and Settings\$USER_NAME\Local Settings\Application Data\Local\YourAppName\...

    Yes cross platform makes life difficult, or atleast adds few IF statements. ;)

  2. How to save it: You can save the credentials in JSON file easily. Now as you need the data to be secured and also not read by illegal access, the best way is to encrypt it. NodeJs Crypto module is very easy to use and you can use it in few lines to encry/decrypt your settings file. Your application will only be aware of encryption passwords and hence your application will only be able to decrypt it.

    I would recommend AES-192 for encrypting the files.

Hope it helps!!


If you have a limited number of settings, a JSON file will do. If you have a more elaborate set or configs, consider using something like a SQLite file, for easier retrieval.

At any rate, save that file in a directory that does not require elevated permissions. Also, if it's unique to the user, maybe consider saving the file under the user's home directory.


I wrote the cli-config API to manage NodeJS app configuration including:

  • Package defaults
  • Local user config
  • Command line options
  • App overrides

Visit https://github.com/tohagan/cli-config for more details.

Example 1:

Combines configuration options from package file defaults.config then ~/.<appname>.config then command line options then force the debug option to be true. Uses a shallow merge so only the top level properties are merged.

var config = require('../cli-config')             .getConfig({dirname: __dirname, override: {debug: true}});

Example 2:

Deep merge nested configuration settings from package defaults.config then ./config.json then command line options. If ./config.json does not exist, clone a copy from defaults.config so the user can use it to override defaults.config in the future.

var config = require('../cli-config').getConfig({    dirname: __dirname,              clone: true,                     configFile: './config.json',     merge: 'deep'                });

Example 3:

The command line parser returns an object that can be used to override the system settings or user settings options. You can configure this parser using the cli option. Refer to minimist for more details about command line parsing options.

var config = require('../cli-config').getConfig({    dirname: __dirname,    cli: {         boolean: {            'd': 'debug',            'v': 'verbose'        }     } });

Sets the config.debug and config.verbose options to true.

$ myapp -d -v