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)?

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

5 Comments

I talk about all modules that were installed by me after node.js was installed (=modules that do not come natively with node.js).
@IgorP. do I sense that you actually install modules globally (using npm install -g ...)?
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?
@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).
Ok, makes sense. Thanks for that clarification =)
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.