Reputation: 60
I have problem with npm install
. I want to install all dependencies used in my project. In package.json
there aren't any dependencies, but in my project files I have for example const mongo=require('mongoose')
and in another file const morgan=require('morgan')
etc.
I know that when typing npm i --save <dependency_name>
it will update my package.json
. But I would like to install all dependencies without typing their names explicitly anywhere.
Is there any way to install all dependencies used in the whole project, but which there aren't in package.json
?
Upvotes: 0
Views: 3302
Reputation: 114024
You can scan your project for all required modules.
Assuming your project only uses common.js (require) you can get a list of all modules by doing something like:
egrep -R --exclude-dir=node_modules '=\s*require\s*\(' | awk '{gsub(/^.+require\s*\(\s*./,""); gsub(/.\s*\).*$/,""); print $0}'
A more readable version of the above command is:
#! /bin/bash
egrep -R --exclude-dir=node_modules '=\s*require\s*\(' |
awk '{
gsub(/^.+require\s*\(\s*./,"");
gsub(/.\s*\).*$/,"");
print $0
}'
You can save the script above in a file and execute it as a shell script.
To automatically install the modules just pipe it to xargs
:
egrep -R --exclude-dir=node_modules '=\s*require\s*\(' |
awk '{
gsub(/^.+require\s*\(\s*./,"");
gsub(/.\s*\).*$/,"");
print $0
}' |
xargs npm install
I leave supporting ES6 module as homework for the reader.
Upvotes: 0
Reputation: 2270
It is not intended to write your program code first and then let NPM find all dependencies that are used. This is because of a simple reason: Before you can use an external library / package, you'll have to download it, otherwise you could hardly use it at all.
Now, I only see two reasons for your use case:
package.json
, orAnyways, there would be some kinds of solutions for your problem:
The nasty solution: Run npm start
and see what error messages you get. All uninstalled dependencies will not be found, so you'll see the package names and can add them manually to your package.json
. This will become nasty because you'll have to re-run your program each time. To avoid this, you could have a look at Nodemon which automatically re-starts your program.
The better solution: Open a web IDE of your favor and use the "Global Search" function to find all occurrences of the string require(
, or as a Regex: require\((.+)\)
. This will list you all dependency imports in your program files. If there should be also ECMA 6 imports, also search for import (.+) from (.+)
. - However, you'll still have to copy and paste all dependency names from all files manually into your package.json
file.
The best but most complex solution: Write a Node.js script that scans all your files recursively, beginning in your root project directory. Create a dependency memory variable, like let dependencies = []
. Read all *.js
files (sync or async) and every time the require
or import
statement is matched, check whether the dependency is already in your dependencies
array. If not, push it. Finally, all your project dependencies will be listed in the dependencies
array and you can copy and paste them into your package.json
.
Pseudo Node.js Code:
const lineReader = require('line-reader');
let dependencies = [];
const regex = /require\(['|"](.+)['|"]\)/g;
lineReader.eachLine('/path/to/file', function(line) {
const match = regex.exec(line);
if(match) {
if(dependencies.indexOf(match[1]) === -1) {
dependencies.push(match[1]);
}
}
});
Upvotes: 1
Reputation: 80
Short answer: You can't
How npm install
works is checking all your dependencies listed in the package.json
and install them by once. So either you get the package.json
from the tutor or you install them one by one
Upvotes: 0
Reputation: 935
No, you can't do that - you will have to add all dependencies by installing them explicitly, like npm install morgan
. NPM is not aware of the dependencies you're importing in your files. Another thing is that requiring dependencies that are not listed in package.json
is simply wrong and should never take place.
Upvotes: 0