- 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
npm-folders
Folder Structures Used by npm
Description
npm puts various things on your computer. That's its job.
This document will tell you what it puts where.
tl;dr
- Local install (default): puts stuff in
./node_modules
of the current package root. - Global install (with
-g
): puts stuff in /usr/local or wherever node is installed. - Install it locally if you're going to
require()
it. - Install it globally if you're going to run it on the command line.
- If you need both, then install it in both places, or use
npm link
.
prefix Configuration
The prefix
config defaults to the location where node is installed. On most systems, this is /usr/local
. On Windows, it's %AppData%\npm
. On Unix systems, it's one level up, since node is typically installed at {prefix}/bin/node
rather than {prefix}/node.exe
.
When the global
flag is set, npm installs things into this prefix. When it is not set, it uses the root of the current package, or the current working directory if not in a package already.
Node Modules
Packages are dropped into the node_modules
folder under the prefix
. When installing locally, this means that you can require("packagename")
to load its main module, or require("packagename/lib/path/to/sub/module")
to load other modules.
Global installs on Unix systems go to {prefix}/lib/node_modules
. Global installs on Windows go to {prefix}/node_modules
(that is, no lib
folder.)
Scoped packages are installed the same way, except they are grouped together in a sub-folder of the relevant When in global mode, executables are linked into When in local mode, executables are linked into When in global mode, man pages are linked into When in local mode, man pages are not installed. Man pages are not installed on Windows systems. See Temporary files are stored by default in the folder specified by the Temp files are given a unique folder under this root for each run of the program, and are deleted upon successful exit. When installing locally, npm first tries to find an appropriate Starting at the $PWD, npm will walk up the folder tree checking for a folder that contains either a If no package root is found, then the current folder is used. When you run Any bin files are symlinked to If the For global installation, packages are installed roughly the same way, but using the folders described above. Cycles are handled using the property of node's module system that it walks up the directories looking for Consider the case above, where This shortcut is only used if the exact same version would be installed in multiple nested Another optimization can be made by installing dependencies at the highest level possible, below the localized "target" folder. Consider this dependency graph: In this case, we might expect a folder structure like this: Since foo depends directly on Even though the latest copy of blerg is 1.3.7, foo has a specific dependency on version 1.2.5. So, that gets installed at [A]. Since the parent installation of blerg satisfies bar's dependency on Bar [B] also has dependencies on baz and asdf, so those are installed in bar's Underneath bar, the Underneath For a graphical breakdown of what is installed where, use Upon publishing, npm will look in the This allows a package maintainer to install all of their dependencies (and dev dependencies) locally, but only re-publish those items that cannot be found elsewhere. See node_modules
folder with the name of that scope prefix by the @ symbol, e.g. npm install @myorg/package
would place the package in {prefix}/node_modules/@myorg/package
. See Executables
{prefix}/bin
on Unix, or directly into {prefix}
on Windows../node_modules/.bin
so that they can be made available to scripts run through npm. (For example, so that a test runner will be in the path when you run npm test
.)Man Pages
{prefix}/share/man
.Cache
Temp Files
tmp
config, which defaults to the TMPDIR, TMP, or TEMP environment variables, or /tmp
on Unix and c:\windows\temp
on Windows.More Information
prefix
folder. This is so that npm install foo@1.2.3
will install to the sensible root of your package, even if you happen to have cd
ed into some other folder.package.json
file, or a node_modules
folder. If such a thing is found, then that is treated as the effective "current directory" for the purpose of running npm commands. (This behavior is inspired by and similar to git's .git-folder seeking logic when running git commands in a working dir.)npm install foo@1.2.3
, then the package is loaded into the cache, and then unpacked into ./node_modules/foo
. Then, any of foo's dependencies are similarly unpacked into ./node_modules/foo/node_modules/...
../node_modules/.bin/
, so that they may be found by npm scripts when necessary.Global Installation
global
configuration is set to true, then npm will install packages "globally".Cycles, Conflicts, and Folder Parsimony
node_modules
folders. So, at every stage, if a package is already installed in an ancestor node_modules
folder, then it is not installed at the current location.foo -> bar -> baz
. Imagine if, in addition to that, baz depended on bar, so you'd have: foo -> bar -> baz -> bar -> baz ...
. However, since the folder structure is: foo/node_modules/bar/node_modules/baz
, there's no need to put another copy of bar into .../baz/node_modules
, since when it calls require("bar"), it will get the copy that is installed in foo/node_modules/bar
.node_modules
folders. It is still possible to have a/node_modules/b/node_modules/a
if the two "a" packages are different versions. However, without repeating the exact same package multiple times, an infinite regress will always be prevented.Example
foo
+-- blerg@1.2.5
+-- bar@1.2.3
| +-- blerg@1.x (latest=1.3.7)
| +-- baz@2.x
| | `-- quux@3.x
| | `-- bar@1.2.3 (cycle)
| `-- asdf@*
`-- baz@1.2.3
`-- quux@3.x
`-- bar
foo
+-- node_modules
+-- blerg (1.2.5) <---[A]
+-- bar (1.2.3) <---[B]
| `-- node_modules
| +-- baz (2.0.2) <---[C]
| | `-- node_modules
| | `-- quux (3.2.0)
| `-- asdf (2.3.4)
`-- baz (1.2.3) <---[D]
`-- node_modules
`-- quux (3.2.0) <---[E]
bar@1.2.3
and baz@1.2.3
, those are installed in foo's node_modules
folder.blerg@1.x
, it does not install another copy under [B].node_modules
folder. Because it depends on baz@2.x
, it cannot re-use the baz@1.2.3
installed in the parent node_modules
folder [D], and must install its own copy [C].baz -> quux -> bar
dependency creates a cycle. However, because bar is already in quux's ancestry [B], it does not unpack another copy of bar into that folder.foo -> baz
[D], quux's [E] folder tree is empty, because its dependency on bar is satisfied by the parent folder copy installed at [B].npm ls
.Publishing
node_modules
folder. If any of the items there are not in the bundledDependencies
array, then they will not be included in the package tarball.See Also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论