npm commit strips out comments

What I Wanted to Do

Create a ~/.npmrc with important comments (telling my system where this file should be linked to so I can automatically deploy configurations on multiple platforms) then follow the documentation to set some values using npm config set save=true for example. I expected that the appropriate configuration lines would be added to the config file.

What Happened Instead

The config file ~/.npmrc was completely rewritten with all comments striped out, breaking my deployment system.

Reproduction Steps

  1. Make a ~/.rmprc file with some comments.
  2. Run npm config set save=true

Comments are stripped out.


This issue was reported on the original github but tracker, but closed and no corresponding discussion has ensued since

Some information about why it i so difficult to read the original file and then simply modify the appropriate lines instead of rewriting the whole thing would be appreciated so that contributors could figure out how to start implementing a fix.

Platform Info

$ npm --versions
{ npm: '6.10.3',
  ares: '1.14.0',
  cldr: '33.1',
  http_parser: '2.8.0',
  icu: '62.1',
  modules: '64',
  napi: '3',
  nghttp2: '1.34.0',
  node: '10.13.0',
  openssl: '1.1.0i',
  tz: '2018e',
  unicode: '11.0',
  uv: '1.23.2',
  v8: '',
  zlib: '1.2.11' }
$ node -p process.platform

There were some detailed comments added to that bug before it was closed, it got some consideration. There was mention of ini doing the work and it is still a dependency of the npm/cli project. (I have not checked actual code usage.)

This supports reading a file, manipulating it in code, and writing a file.

The first three approaches I thought of to preserve comments are to include them in the in-memory model, do a merge when writing over an existing file, or find a different package which supports this and is compatible enough. Since comments are usually about a nearby configuration option, adding them to the in-memory model would probably also mean preserving ordering in some way, and be fairly weak.

There are some open issues against ini asking to preserve comments:

A different approach would be to clarify the current behaviour by adding support for a comment at top of file warning comments will be lost. Likewise the npm documentation could say that the file will be rewritten if manipulated by npm.