1

I am about to prepare a webserver for "production-mode" (MEAN environment, talking about server-side), thus also creating the package.json file for my project. As it employs heaps of files and modules now, I am trying to figure out the easiest and most reliable way of defining which modules to consider for my package.json file:

1) Is there a way to tell Node.js to automatically create a package.json file containing all modules that I added manually since installation of Node.js (= crucial for my current Node.js project)?

2) If not, how can I list only those manually installed modules (in contrast to listing all modules using something like npm -g ls --json)?

Igor P.
  • 1,407
  • 2
  • 20
  • 34

2 Answers2

2

I don't know exactly what you mean by "manually", but to get a list of "root-level" modules that are installed in your project directory you can use this:

$ npm ls --depth 0 --json

Or, in case you installed your modules globally (don't do that...):

$ npm ls -g --depth 0 --json 

Tip: when starting a new project, have npm create a new package.json right away:

$ npm init

When you install required modules, you can have them added to it automatically:

$ npm install module --save
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • I talk about all modules that were installed by me after node.js was installed (=modules that do not come natively with node.js). – Igor P. Jul 21 '15 at 13:42
  • @IgorP. do I sense that you actually install modules globally (using `npm install -g ...`)? – robertklep Jul 21 '15 at 13:47
  • Usually I install modules like this: sudo npm install MODULENAME Sorry for asking but what is the difference to: sudo npm install -g MODULENAME? What does globally mean in this context? – Igor P. Jul 21 '15 at 13:59
  • 1
    @IgorP. installing a module globally means it will be installed systemwide instead of just local to your project. There's potential for problems when various projects depend on a particular module that is installed globally, particularly if projects depend on different versions of the same module. It's better to keep modules required by a project "close" to the project (in the local `./node_modules` directory, where they are installed when you don't use the `-g` flag). – robertklep Jul 21 '15 at 14:03
  • Ok, makes sense. Thanks for that clarification =) – Igor P. Jul 21 '15 at 14:07
0

Generally, at the time of installing each node module, you would save it to the package.json with npm install modulename --save or npm install modulename --save-dev.

If you need to add all of the installed modules to the package.json retroactively, you could write a short script, such as the one suggested here: https://stackoverflow.com/a/13381344/5070356

That will grab the name/version from each dependency and add it to your package.json.

Community
  • 1
  • 1
snozza
  • 2,123
  • 14
  • 17