0

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?

Marvin Danig
  • 3,738
  • 6
  • 39
  • 71
  • each OS has different locations. If you're talking `usr/lib`, stop think of Unix only immediately, and instead find yourself a module (that someone else already wrote) that gets you the write location for application data. Then use that. Why would you intentionally break cross-platform compatibility? It's a node module; unless you're doing low level native OS or hardware specific things, your code is supposed to be universal. – Mike 'Pomax' Kamermans Feb 05 '16 at 22:27
  • I agree. The module/CLI I have up here is OS-agnostic, that *is* why node. Using another module like? This may sound a little sacrilegious but it's not so easy to find cross-platform packages that work. Example: http://stackoverflow.com/questions/35072572/app-root-path-doesnt-return-absolute-path-correctly-on-linux – Marvin Danig Feb 05 '16 at 23:00
  • 1
    npmjs.com, search "appdata". First hit? https://www.npmjs.com/package/appdirectory -- sounds like we found your module. Alternative? https://www.npmjs.com/package/user-settings-dir, a few hits down. Finding modules is actually *really* easy. We dig a little further because people say "...based on..." and end up with https://www.npmjs.com/package/os-homedir, which gets *4.5 million downloads* a month. We found the winning module. – Mike 'Pomax' Kamermans Feb 05 '16 at 23:07
  • :) Thanks Mike! Looks like I was going about this search with wrong keywords. This is exactly what I have been looking for days! – Marvin Danig Feb 05 '16 at 23:23

1 Answers1

1

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

Seldo
  • 5,600
  • 4
  • 20
  • 11