是包。Json应该手动编辑?像npm这样的程序不能只查看文件,看到“require”语句,然后使用它将必要的条目放入包中吗?json文件吗?有这样的节目吗?


当前回答

包。NPM使用Json文件来了解你的node.js项目。

使用npm init生成包。Json文件!

它与npm捆绑在一起。在这里阅读它的文档:https://docs.npmjs.com/cli/init

此外,您可以使用一个官方工具以编程方式生成该文件:https://github.com/npm/init-package-json

其他回答

包。NPM使用Json文件来了解你的node.js项目。

使用npm init生成包。Json文件!

它与npm捆绑在一起。在这里阅读它的文档:https://docs.npmjs.com/cli/init

此外,您可以使用一个官方工具以编程方式生成该文件:https://github.com/npm/init-package-json

1. 选择

如果你是git和GitHub用户:

生成包比NPM init更简单。

else

或者你不喜欢包装。Json模板,生成包或NPM init生成:

你可以通过脚手架应用生成自己的模板,如generate, sails或yeoman。


2. 相关性

这个答案与2018年3月有关。在未来,来自这个答案的数据可能会过时。

这个答案的作者个人在2018年3月使用了generate-package。


3.限制

你需要使用git和GitHub来使用生成包。


4. 示范

例如,我创建了一个空白文件夹sasha-npm-init-vs-generate-package。

4.1. generate-package

命令:

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:

{
  "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

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"
}

我认为,这个generate-package更简单,这个npm init。


5. 定制

创建你自己的包。Json模板,见生成和yeoman的例子。

使用NPM init -f命令生成包。在每个命令之后保存,这样每个模块都会在package.json中自动更新 例如:NPM install express—save

NPM热

创建包。Json文件,然后使用

Ls node_modules/ | xargs NPM install——save

填写node_modules文件夹中的模块。

Edit: @paldepind指出第二个命令是多余的,因为npm init现在会自动添加你在node_modules/文件夹中的内容。我不知道是不是一直都是这样,但至少现在,它不需要第二个命令就能工作。

基于Pylinux的回答,下面是针对Windows操作系统的解决方案,

dir node_modules > abc.txt
FOR /F %k in (abc.txt) DO npm install --save

希望能有所帮助。