Search code examples
node.jsnpmdotfiles

Where to store a node_package specific settings on the client machine?


I' have a node_package bookiza (installed globally) that POSTS/PATCHes values to a receiving substrate using credentials taken off a .rc file.

I'm currently saving the .rc file inside the module itself, at usr/lib/node_modules/bookiza, but I can do so anywhere I like. The problem in storing it inside the package is that the settings are overwritten whenever the user npm i -g installs again, to update the package.

function updateBookizaConfig(res) {

  var bookizaConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '..', '.bookizarc')).toString());

  bookizaConfig.token = res.body.key;
  bookizaConfig.username = res.body.username;
  bookizaConfig.email = res.body.email;

  fs.writeFileSync(path.join(__dirname, '..', '.bookizarc'), JSON.stringify(bookizaConfig, null, 2));

  // Move or copy the config file outside of package to retain credentials upon package update.
  // cp('-R', path.join(__dirname, '..', '.bookizarc'), path.join(__dirname, '..', '..')); 

  console.log(chalk.bold.cyan('Registration successful'));

}

This works, but note that the .dotfile file is saved inside usr/lib/node_modules/ directory, as a sibling to other global packages installed on the machine. Now I could put the settings file anywhere else on the machine too, but what is the good practice/standard way of doing this?

Will it be better for me to put the settings file inside a usr/lib/node_modules/dots folder where in future other package writers could also probably put their .rc files?


Solution

  • Users in the comments have already hit on this solution, but here for the record anyway:

    1. npm recommends you save user-specific config data in the user's home directory rather than in npm's modules directory, both because it is inconvenient to persist those settings, and because it is a problem in multi-user environments.

    2. There are a number of modules that will find the user's home directory in a cross-platform way for you to put your files in; we like https://www.npmjs.com/package/osenv