[英]How to install npm dependencies which aren't in package.json?
I have problem with npm install
. 我对
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. 在
package.json
中没有任何依赖关系,但是在我的项目文件中,例如,我有const mongo=require('mongoose')
和另一个文件const morgan=require('morgan')
等。
I know that when typing npm i --save <dependency_name>
it will update my package.json
. 我知道当输入
npm i --save <dependency_name>
,它将更新我的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
? 有什么方法可以安装整个项目中使用的所有依赖项,但
package.json
中没有?
It is not intended to write your program code first and then let NPM find all dependencies that are used. 并非要先编写程序代码,然后让NPM查找所使用的所有依赖关系。 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
, or package.json
,或者 Anyways, there would be some kinds of solutions for your problem: 无论如何,针对您的问题会有一些解决方案:
The nasty solution: Run npm start
and see what error messages you get. 讨厌的解决方案:运行
npm start
,看看得到了什么错误消息。 All uninstalled dependencies will not be found, so you'll see the package names and can add them manually to your package.json
. 找不到所有已卸载的依赖项,因此您将看到软件包名称,并可以将其手动添加到
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.
为避免这种情况,您可以看看Nodemon ,它会自动重新启动程序。
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. 更好的解决方案:打开您喜欢的Web IDE,然后使用“全局搜索”功能查找所有出现的字符串
require(
或作为正则表达式: require\\((.+)\\)
,这将列出所有依赖项在程序文件中导入。如果还应该导入ECMA 6,则还要import (.+) from (.+)
搜索import (.+) from (.+)
--但是,您仍然必须手动将所有文件中的所有依赖项名称复制并粘贴到您的文件中package.json
文件。
The best but most complex solution: Write a Node.js script that scans all your files recursively , beginning in your root project directory. 最好但最复杂的解决方案:从根项目目录开始,编写一个Node.js脚本以递归方式扫描所有文件 。 Create a dependency memory variable, like
let dependencies = []
. 创建一个依赖项内存变量,例如
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. 读取所有
*.js
文件(同步或异步),并在每次require
或import
语句匹配时,检查依赖项是否已在dependencies
数组中。 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
. 最后,所有项目依赖项都将在
dependencies
数组中列出,您可以将它们复制并粘贴到package.json
。
Pseudo Node.js Code: 伪Node.js代码:
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]);
}
}
});
No, you can't do that - you will have to add all dependencies by installing them explicitly, like npm install morgan
. 不,您不能这样做-您将必须通过显式安装所有依赖项来添加所有依赖项,例如
npm install morgan
。 NPM is not aware of the dependencies you're importing in your files. NPM不知道您要在文件中导入的依赖项。 Another thing is that requiring dependencies that are not listed in
package.json
is simply wrong and should never take place. 另一件事是,要求未在
package.json
列出的依赖项完全是错误的,并且永远不会发生。
Short answer: You can't 简短答案:您不能
How npm install
works is checking all your dependencies listed in the package.json
and install them by once. npm install
工作方式是检查package.json
列出的所有依赖项,并一次安装它们。 So either you get the package.json
from the tutor or you install them one by one 因此,要么从导师那里获取
package.json
,要么一一安装
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: 假设您的项目仅使用common.js(要求),则可以通过执行以下操作来获取所有模块的列表:
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. 您可以将上面的脚本保存在文件中,然后将其作为Shell脚本执行。
To automatically install the modules just pipe it to xargs
: 要自动安装模块,只需将其通过管道传递给
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. 我将支持ES6模块留给读者作为家庭作业。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.