- npm 是什么?
- 如何安装 npm 并管理 npm 版本
- How to Prevent Permissions Errors
- 如何安装本地包
- Working with package.json
- 如何更新本地安装的包
- 如何卸载本地安装的包
- 如何安装全局包
- 如何更新全局安装的包
- 如何卸载全局安装的包
- 如何创建 Node.js 模块
- How to Publish & Update a Package
- 如何使用语义化版本
- How to Work with Scoped Packages
- How to Label Packages with Dist-tags
- How to Use Two-Factor Authentication
- How to Work with Security Tokens
- How to Change Profile Settings from the CLI
- Understanding Packages and Modules
- npm-access
- npm-adduser
- npm-audit
- npm-bin
- npm-bugs
- npm-build
- npm-bundle
- npm-cache
- npm-ci
- npm-completion
- npm-config
- npm-dedupe
- npm-deprecate
- npm-dist-tag
- npm-docs
- npm-doctor
- npm-edit
- npm-explore
- npm-help
- npm-help-search
- npm-hook
- npm-init
- npm-install
- npm install-ci-test -- Install a project with a clean slate and run tests
- npm install-test -- 安装依赖包并运行测试
- npm-link
- npm-logout
- npm-ls
- npm
- npm-org
- npm-outdated
- npm-owner
- npm-pack
- npm-ping
- npm-prefix
- npm-profile
- npm-prune
- npm-publish
- npm-rebuild
- npm-repo
- npm-restart
- npm-root
- npm-run-script
- npm-search
- npm-shrinkwrap
- npm-star
- npm-stars
- npm-start
- npm-stop
- npm-team
- npm-test
- npm-token
- npm-uninstall
- npm-unpublish
- npm-update
- npm-version
- npm-view
- npm-whoami
- npm-coding-style
- npm-config
- npm-developers
- npm-disputes
- npm-orgs
- npm-registry
- npm-removal
- npm-scope
- npm-scripts
- semver
- npm-folders
- npmrc
- package-lock.json
- npm-package-locks
- package.json
- npm-shrinkwrap.json
- 尝试 node 的最新稳定版本
- Try the latest stable version of npm
- 如果 npm 损坏了
- Try clearing the npm cache
- Common Errors
Understanding Packages and Modules
Node.js and npm have very specific definitions of packages and modules, which are easy to mix up. We'll discuss those definitions here, make them distinct, and explain why certain default files are named the way they are.
Quick Summary
- A package is a file or directory that is described by a
package.json
. This can happen in a bunch of different ways! For more info, see What is apackage
?A package is any of the following:
- a) A folder containing a program described by a
package.json
file. - b) A gzipped tarball containing (a).
- c) A url that resolves to (b).
- d) A
<name>@<version>
that is published on the registry with (c). - e) A
<name>@<tag>
that points to (d). - f) A
<name>
that has alatest
tag satisfying (e). - g) A
git
url that, when cloned, results in (a).
Noting all these
package
possibilities, it follows that even if you never publish your package to the public registry, you can still get a lot of benefits of using npm:- If you just want to write a node program, and/or,
- If you also want to be able to easily install it elsewhere after packing it up into a tarball.
Git urls can be of the form:
git://github.com/user/project.git#commit-ish git+ssh://user@hostname:project.git#commit-ish git+http://user@hostname/project/blah.git#commit-ish git+https://user@hostname/project/blah.git#commit-ish
The
commit-ish
can be any tag, sha, or branch which can be supplied as an argument togit checkout
. The default ismaster
.What is a
module
?A module is anything that can be loaded with
require()
in a Node.js program. The following are all examples of things that can be loaded as modules:- A folder with a
package.json
file containing amain
field. - A folder with an
index.js
file in it. - A JavaScript file.
Most npm packages are modules
Generally, npm packages that are used in Node.js program are loaded with
require
, making them modules. However, there's no requirement that an npm package be a module!Some packages, e.g.,
cli
packages, only contain an executable command-line interface and don't provide amain
field for use in Node.js programs. These packages are not modules.Almost all npm packages (at least, those that are Node programs) contain many modules within them (because every file they load with
require()
is a module).In the context of a Node program, the
module
is also the thing that was loaded from a file. For example, in the following program:var req = require('request')
we might say that "The variable
req
refers to therequest
module".File and Directory Names in the Node.js and npm Ecosystem
So, why is it the
node_modules
folder, butpackage.json
file? Why notnode_packages
ormodule.json
?The
package.json
file defines the package. (See "What is apackage
?", above.)The
node_modules
folder is the place Node.js looks for modules. (See "What is amodule
?", above.)For example, if you create a file at
node_modules/foo.js
and then had a program that didvar f = require('foo.js')
, it would load the module. However,foo.js
is not a "package" in this case because it does not have a package.json.Alternatively, if you create a package which does not have an
index.js
or a"main"
field in thepackage.json
file, then it is not a module. Even if it's installed innode_modules
, it can't be an argument torequire()
. - a) A folder containing a program described by a
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论