3

I have a standard package.json file for the list of packages that my project is using.

I also need to install some global packages. Instead of doing that manually, I would like to list them in a global.json config file, so that I can do:

npm install -g -f global.json

But the -f flag does not seem to exist for npm? Is it possible to specifiy an alternative dependency list, instead of the standard package.json

blueFast
  • 41,341
  • 63
  • 198
  • 344
  • possible duplicate of [Installing "global" npm dependencies via package.json](http://stackoverflow.com/questions/14657170/installing-global-npm-dependencies-via-package-json) – laggingreflex Jan 08 '15 at 12:34
  • @laggingreflex: The "global" part is not really what I am interested about; what I really want is to be able to specify an *alternative* `package2.json` config file (an application of that would be to use it for global packages, but that is just one example). How can this be done for `npm`? Is there any flag to specify the name of the config file, or is `package.json` hardwired (!) into the npm codebase? – blueFast Jan 08 '15 at 13:12

1 Answers1

5

The filename package.json is actually hardcoded into npm source code and you cannot change it using a configuration option or a command line flag.

While this is not a verified reason from developers, I suspect the reason for not allowing this is that it would break dependency resolution. If package A renames its package.json to myapp.json, and package B specifies A as its dependency, then npm would be unable to read and install A's dependencies (or any metainformation, for that matter) because of the non-standard package.json's name.

If you search npm's repo, you will find 100+ mentions both in source and tests that directly reference the string package.json.

PS. If your application requires a npm module to be installed globally, you should either

  1. Clearly state this in README and detect its presence at runtime (and print a nice error message to the user if it is missing)
  2. Invest time and effort into making it work with local installation (subjectively preferred)
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
Robert Rossmann
  • 11,931
  • 4
  • 42
  • 73
  • Thx. Do you know the rationale behind the decision to make package.json a mandatory filename? Most tools have both a sensible default, and the possibility to override that default with a command line flag ... – blueFast Jan 08 '15 at 17:26
  • True, but not all programming languages (and their package managers) allow you the luxury of having per-module dependencies, compared to per-application dependencies, i.e. in PHP, you cannot load a module twice, each time with different version because it would cause a fatal error. Whereas in Node, each module may have a dependency and those dependencies may have the same dependency and it all works. I have updated the answer with more info, check it out. – Robert Rossmann Jan 08 '15 at 17:38