Reputation: 14048
I'm writing a nodejs cli utility (NPM module intended to be installed globally) that will need to store some user-supplied values. What is the best way to store these values on a system?
For example, should I create my own config file under say: /etc/{my-utility-name}/conf.json
and have this directory+file initialized in my install
script
(https://npmjs.org/doc/misc/npm-scripts.html)
Upvotes: 9
Views: 5065
Reputation: 2887
I found that the yeoman project has a package for this called configstore. I tried it out and the API is incredibly simple and easy to use and abstracts out any need to worry about OS compatibility.
Getting and setting configs is as easy as:
const Configstore = require('configstore')
const config = Configstore(packageName, defaults)
config.set(key, value)
config.get(key)
Yeoman is a large well tested project so the fact that they built and use this in their project should indicate that this is a safe stable package to use in your own project.
Upvotes: 3
Reputation: 22738
I wrote the cli-config API to manage NodeJS app configuration including:
Visit https://github.com/tohagan/cli-config for more details.
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}});
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'
});
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
Upvotes: 1
Reputation: 570
Looking at the questions and comments i think there are two problems to solve:
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. ;)
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!!
Upvotes: 6
Reputation: 27849
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.
Upvotes: 1