本文翻译自:Is there a way to automatically build the package.json file for Node.js projects
Is package.json supposed to be manually edited? 是否应该手动编辑package.json? 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? 像npm这样的程序难道只能查看文件,查看“require”语句,然后使用它将必要的条目放在package.json文件中吗? Are there any programs like that? 有没有这样的节目?
#1楼
参考:https://stackoom.com/question/fnRO/有没有办法为Node-js项目自动构建package-json文件
#2楼
I just wrote a simple script to collect the dependencies in ./node_modules. 我刚写了一个简单的脚本来收集./node_modules中的依赖项。 It fulfills my requirement at the moment. 它满足了我的要求。 This may help some others, I post it here. 这可能有助于其他人,我在这里发布。
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) {