有没有办法为 Node.js 项目自动构建 package.json 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9961502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to automatically build the package.json file for Node.js projects
提问by neuromancer
Is package.json supposed to be manually edited? Couldn't a program like npm just look through the files, see the "require" statements, and then use that to put the necessary entries in the package.json file? Are there any programs like that?
package.json 应该手动编辑吗?像 npm 这样的程序不能只是查看文件,查看“require”语句,然后使用它来将必要的条目放入 package.json 文件中吗?有没有这样的程序?
回答by Ore4444
The package.json file is used by npmto learn about your node.js project.
npm使用 package.json 文件来了解您的 node.js 项目。
Use npm initto generate package.json files for you!
使用npm init生成的package.json文件为您服务!
It comes bundled with npm. Read its documentation here: https://docs.npmjs.com/cli/init
它与 npm 捆绑在一起。在此处阅读其文档:https: //docs.npmjs.com/cli/init
Also, there's an official tool you can use to generate this file programmatically: https://github.com/npm/init-package-json
此外,您可以使用官方工具以编程方式生成此文件:https: //github.com/npm/init-package-json
回答by nzondlo
First off, run
首先,运行
npm init
...will ask you a few questions (read this first) about your project/package and then generate a package.json file for you.
...会问你几个关于你的项目/包的问题(先阅读这个),然后为你生成一个 package.json 文件。
Then, once you have a package.json file, use
然后,一旦你有了 package.json 文件,使用
npm install <pkg> --save
or
或者
npm install <pkg> --save-dev
...to install a dependency and automatically append it to your package.json's dependencieslist.
...安装依赖项并自动将其附加到您package.json的dependencies列表中。
(Note: You may need to manually tweak the version ranges for your dependencies.)
(注意:您可能需要手动调整依赖项的版本范围。)
回答by douyw
I just wrote a simple script to collect the dependencies in ./node_modules. It fulfills my requirement at the moment. This may help some others, I post it here.
我只是写了一个简单的脚本来收集 ./node_modules 中的依赖项。目前满足我的要求。这可能对其他人有帮助,我把它贴在这里。
var fs = require("fs");
function main() {
fs.readdir("./node_modules", function (err, dirs) {
if (err) {
console.log(err);
return;
}
dirs.forEach(function(dir){
if (dir.indexOf(".") !== 0) {
var packageJsonFile = "./node_modules/" + dir + "/package.json";
if (fs.existsSync(packageJsonFile)) {
fs.readFile(packageJsonFile, function (err, data) {
if (err) {
console.log(err);
}
else {
var json = JSON.parse(data);
console.log('"'+json.name+'": "' + json.version + '",');
}
});
}
}
});
});
}
main();
In my case, the above script outputs:
就我而言,上面的脚本输出:
"colors": "0.6.0-1",
"commander": "1.0.5",
"htmlparser": "1.7.6",
"optimist": "0.3.5",
"progress": "0.1.0",
"request": "2.11.4",
"soupselect": "0.2.0", // Remember: remove the comma character in the last line.
Now, you can copy&paste them. Have fun!
现在,您可以复制和粘贴它们。玩得开心!
回答by Pylinux
npm init
npm init
to create the package.json file and then you use
创建 package.json 文件,然后使用
ls node_modules/ | xargs npm install --save
ls node_modules/ | xargs npm install --save
to fill in the modules you have in the node_modules folder.
填写您在 node_modules 文件夹中的模块。
Edit: @paldepind pointed out that the second command is redundant because npm initnow automatically adds what you have in your node_modules/ folder. I don't know if this has always been the case, but now at least, it works without the second command.
编辑:@paldepind 指出第二个命令是多余的,因为npm init现在会自动添加您在 node_modules/ 文件夹中拥有的内容。我不知道情况是否一直如此,但至少现在,它可以在没有第二个命令的情况下工作。
回答by ahmed hamdy
Command line:
命令行:
npm init
will create package.json file
将创建 package.json 文件
To install , update and uninstall packages under dependencies into package.json file:
将依赖项下的包安装、更新和卸载到 package.json 文件中:
Command line:
命令行:
npm install <pkg>@* --save
will automatically add the latest version for the package under dependencies into package.json file
将自动将依赖项下的包的最新版本添加到 package.json 文件中
EX:
前任:
npm install node-markdown@* --save
Command line:
命令行:
npm install <pkg> --save
also will automatically add the latest version for the package under dependencies into package.json file
也会自动将依赖项下的包的最新版本添加到 package.json 文件中
ifyou need specific version for a package use this Command line:
如果您需要特定版本的包,请使用此命令行:
npm install <pkg>@<version> --save
will automatically add specific version of package under dependencies into package.json file
将自动将依赖项下特定版本的包添加到 package.json 文件中
EX:
前任:
npm install [email protected] --save
ifyou need specific range of version for a package use this Command line:
如果您需要包的特定版本范围,请使用此命令行:
npm install <pkg>@<version range>
will automatically add the latest version for the package between range of version under dependencies into package.json file
将自动将依赖项下版本范围之间的包的最新版本添加到 package.json 文件中
EX:
前任:
npm install koa-views@">1.0.0 <1.2.0" --save
Formore details about how to write version for package npm Doc
有关如何为包npm Doc编写版本的更多详细信息
Command line:
命令行:
npm update --save
will update packages into package.json file and will automatically add updated version for all packages under dependencies into package.json file
会将包更新到 package.json 文件中,并会自动将依赖项下所有包的更新版本添加到 package.json 文件中
Command line:
命令行:
npm uninstall <pkg> --save
will automatically remove package from dependencies into package.json file and remove package from node_module folder
将自动从依赖项中删除包到 package.json 文件并从 node_module 文件夹中删除包
回答by Abhinav Singi
Running npm init -ymakes your package.jsonwith all the defaults.
You can then change package.jsonaccordingly
This saves time many a times by preventing pressing enteron every command in npm init
运行npm init -y使您package.json拥有所有默认值。
然后,您可以更改package.json相应的
防止按这样可以节省时间很多时候enter在每个命令npm init
回答by Adesh M
You can now use Yeoman - Modern Web App Scaffolding Toolon node terminal using 3 easy steps.
您现在可以通过 3 个简单的步骤在节点终端上使用Yeoman - Modern Web App Scaffolding Tool。
First, you'll need to install yo and other required tools:
首先,您需要安装 yo 和其他必需的工具:
$ npm install -g yo bower grunt-cli gulp
To scaffold a web application, install the generator-webappgenerator:
要搭建 Web 应用程序,请安装generator-webapp生成器:
$ npm install -g generator-webapp // create scaffolding
Run yo and... you are all done:
运行 yo 和...你都完成了:
$ yo webapp // create scaffolding
Yeoman can write boilerplate code for your entire web application or Controllers and Models. It can fire up a live-preview web server for edits and compile; not just that you can also run your unit tests, minimize and concatenate your code, optimize images, and more...
Yeoman 可以为您的整个 Web 应用程序或控制器和模型编写样板代码。它可以启动实时预览 Web 服务器以进行编辑和编译;不仅如此,您还可以运行单元测试、最小化和连接代码、优化图像等等...
Yeoman (yo)- scaffolding tool that offers an ecosystem of framework-specific scaffolds, called generators, that can be used to perform some of the tedious tasks mentioned earlier.
Yeoman (yo)- 脚手架工具,提供特定于框架的脚手架生态系统,称为生成器,可用于执行前面提到的一些繁琐任务。
Grunt/ gulp- used to build, preview, and test your project.
Bower- is used for dependency management, so that you no longer have to manually download your front-end libraries.
Bower- 用于依赖项管理,因此您不再需要手动下载前端库。
回答by Anmol Saraf
Based on Pylinux's answer, below is a solution for Windows OS,
基于 Pylinux 的回答,以下是 Windows 操作系统的解决方案,
dir node_modules > abc.txt
FOR /F %k in (abc.txt) DO npm install --save
Hope it helps.
希望能帮助到你。
回答by Nitin Nodiyal
use command npm init -f to generate package.json file and after that use --save after each command so that each module will automatically get updated inside your package.json for ex: npm install express --save
使用命令 npm init -f 生成 package.json 文件,然后在每个命令后使用 --save 以便每个模块将在你的 package.json 中自动更新,例如: npm install express --save
回答by Саша Черных
1. Choice
1.选择
Ifyou git and GitHub user:
如果您是 git 和 GitHub 用户:
????generate-packagemore simply, than npm init.
???generate-package比 更简单npm init。
else
别的
and/oryou don't like package.jsontemplate, that generate-package or npm initgenerate:
和/或您不喜欢package.json模板,即 generate-package 或npm initgenerate:
????you can generate your own template via scaffolding apps as generate, sailsor yeoman.
????您可以通过脚手架应用程序生成自己的模板,如generate、sails或yeoman。
2. Relevance
2. 相关性
This answer is relevant for March 2018. In the future, the data from this answer may be obsolete.
此答案与 2018 年 3 月相关。将来,此答案中的数据可能已过时。
Author of this answer personally used generate-package at March 2018.
这个答案的作者在 2018 年 3 月亲自使用了 generate-package。
3. Limitations
3. 限制
You need use git and GitHub for using generate-package.
您需要使用 git 和 GitHub 才能使用 generate-package。
4. Demonstration
4. 演示
For example, I create blank folder sasha-npm-init-vs-generate-package.
例如,我创建空白文件夹sasha-npm-init-vs-generate-package。
4.1. generate-package
4.1. 生成包
Command:
命令:
D:\SashaDemoRepositories\sasha-npm-init-vs-generate-package>gen package
[16:58:52] starting generate
[16:59:01] √ running tasks: [ 'package' ]
[16:59:04] starting package
? Project description? generate-package demo
? Author's name? Sasha Chernykh
? Author's URL? https://vk.com/hair_in_the_wind
[17:00:19] finished package √ 1m
package.json:
package.json:
{
"name": "sasha-npm-init-vs-generate-package",
"description": "generate-package demo",
"version": "0.1.0",
"homepage": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package",
"author": "Sasha Chernykh (https://vk.com/hair_in_the_wind)",
"repository": "Kristinita/sasha-npm-init-vs-generate-package",
"bugs": {
"url": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package/issues"
},
"license": "MIT",
"engines": {
"node": ">=4"
},
"scripts": {
"test": "mocha"
},
"keywords": [
"generate",
"init",
"npm",
"package",
"sasha",
"vs"
]
}
4.2. npm init
4.2. 初始化
D:\SashaDemoRepositories\sasha-npm-init-vs-generate-package>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (sasha-npm-init-vs-generate-package)
version: (1.0.0) 0.1.0
description: npm init demo
entry point: (index.js)
test command: mocha
git repository: https://github.com/Kristinita/sasha-npm-init-vs-generate-package
keywords: generate, package, npm, package, sasha, vs
author: Sasha Chernykh
license: (ISC) MIT
About to write to D:\SashaDemoRepositories\sasha-npm-init-vs-generate-package\package.json:
{
"name": "sasha-npm-init-vs-generate-package",
"version": "0.1.0",
"description": "npm init demo",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Kristinita/sasha-npm-init-vs-generate-package.git"
},
"keywords": [
"generate",
"package",
"npm",
"package",
"sasha",
"vs"
],
"author": "Sasha Chernykh",
"license": "MIT",
"bugs": {
"url": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package/issues"
},
"homepage": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package#readme"
}
Is this ok? (yes) y
{
"name": "sasha-npm-init-vs-generate-package",
"version": "0.1.0",
"description": "npm init demo",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Kristinita/sasha-npm-init-vs-generate-package.git"
},
"keywords": [
"generate",
"package",
"npm",
"package",
"sasha",
"vs"
],
"author": "Sasha Chernykh",
"license": "MIT",
"bugs": {
"url": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package/issues"
},
"homepage": "https://github.com/Kristinita/sasha-npm-init-vs-generate-package#readme"
}
I think, that generate-packagemore simply, that npm init.
我认为,generate-package更简单的是,那个npm init.
5. Customizing
5. 定制
That create your own package.jsontemplate, see generateand yeomanexamples.

