Gubasek Duzy
Gubasek Duzy

Reputation: 60

How to install npm dependencies which aren't in package.json?

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

Answers (4)

slebetman
slebetman

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

SparkFountain
SparkFountain

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:

  • you copied and pasted some foreign code where you don't have all files, especially no package.json, or
  • you "inherited" some code of a former employee or team that is not documented and consists of unrelated, inconsistent files.

Anyways, there would be some kinds of solutions for your problem:

  1. 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.

  2. 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.

  3. 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

Ivanhoe Cheung
Ivanhoe Cheung

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

Adam Kosmala
Adam Kosmala

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

Related Questions