@@ -15,41 +15,44 @@ alias: npm ln
1515
1616### Description
1717
18+ This is handy for installing your own stuff, so that you can work on it and
19+ test iteratively without having to continually rebuild.
20+
1821Package linking is a two-step process.
1922
20- First, ` npm link ` in a package folder will create a symlink in the global folder
21- ` {prefix}/lib/node_modules/<package> ` that links to the package where the `npm
22- link` command was executed. It will also link any bins in the package to ` {prefix}/bin/{name}`.
23- Note that ` npm link ` uses the global prefix (see ` npm prefix -g ` for its value).
23+ First, ` npm link ` in a package folder will create a symlink in the global
24+ folder ` {prefix}/lib/node_modules/<package> ` that links to the package
25+ where the ` npm link ` command was executed. It will also link any bins in
26+ the package to ` {prefix}/bin/{name} ` . Note that ` npm link ` uses the global
27+ prefix (see ` npm prefix -g ` for its value).
2428
2529Next, in some other location, ` npm link package-name ` will create a
26- symbolic link from globally-installed ` package-name ` to ` node_modules/ `
27- of the current folder.
30+ symbolic link from globally-installed ` package-name ` to ` node_modules/ ` of
31+ the current folder.
2832
29- Note that ` package-name ` is taken from ` package.json ` ,
30- not from directory name.
33+ Note that ` package-name ` is taken from ` package.json ` , _ not _ from the
34+ directory name.
3135
32- The package name can be optionally prefixed with a scope. See [ ` scope ` ] ( /using-npm/scope ) .
33- The scope must be preceded by an @-symbol and followed by a slash.
36+ The package name can be optionally prefixed with a scope. See
37+ [ ` scope ` ] ( /using-npm/scope ) . The scope must be preceded by an @-symbol and
38+ followed by a slash.
3439
3540When creating tarballs for ` npm publish ` , the linked packages are
36- "snapshotted" to their current state by resolving the symbolic links.
37-
38- This is handy for installing your own stuff, so that you can work on it and
39- test it iteratively without having to continually rebuild.
41+ "snapshotted" to their current state by resolving the symbolic links, if
42+ they are included in ` bundleDependencies ` .
4043
4144For example:
4245
4346``` bash
44- cd ~ /projects/node-redis # go into the package directory
45- npm link # creates global link
46- cd ~ /projects/node-bloggy # go into some other package directory.
47- npm link redis # link-install the package
47+ cd ~ /projects/node-redis # go into the package directory
48+ npm link # creates global link
49+ cd ~ /projects/node-bloggy # go into some other package directory.
50+ npm link redis # link-install the package
4851```
4952
50- Now, any changes to ~ /projects/node-redis will be reflected in
51- ~ /projects/node-bloggy/node_modules/node-redis/. Note that the link should
52- be to the package name, not the directory name for that package.
53+ Now, any changes to ` ~/projects/node-redis ` will be reflected in
54+ ` ~/projects/node-bloggy/node_modules/node-redis/ ` . Note that the link
55+ should be to the package name, not the directory name for that package.
5356
5457You may also shortcut the two steps in one. For example, to do the
5558above use-case in a shorter way:
@@ -69,15 +72,33 @@ npm link redis
6972That is, it first creates a global link, and then links the global
7073installation target into your project's ` node_modules ` folder.
7174
72- Note that in this case, you are referring to the directory name, ` node-redis ` ,
73- rather than the package name ` redis ` .
75+ Note that in this case, you are referring to the directory name,
76+ ` node-redis ` , rather than the package name ` redis ` .
7477
75- If your linked package is scoped (see [ ` scope ` ] ( /using-npm/scope ) ) your link command must include that scope, e.g.
78+ If your linked package is scoped (see [ ` scope ` ] ( /using-npm/scope ) ) your
79+ link command must include that scope, e.g.
7680
7781``` bash
7882npm link @myorg/privatepackage
7983```
8084
85+ ### Caveat
86+
87+ Note that package dependencies linked in this way are _ not_ saved to
88+ ` package.json ` by default, on the assumption that the intention is to have
89+ a link stand in for a regular non-link dependency. Otherwise, for example,
90+ if you depend on ` redis@^3.0.1 ` , and ran ` npm link redis ` , it would replace
91+ the ` ^3.0.1 ` dependency with ` file:../path/to/node-redis ` , which you
92+ probably don't want! Additionally, other users or developers on your
93+ project would run into issues if they do not have their folders set up
94+ exactly the same as yours.
95+
96+ If you are adding a _ new_ dependency as a link, you should add it to the
97+ relevant metadata by running ` npm install <dep> --package-lock-only ` .
98+
99+ If you _ want_ to save the ` file: ` reference in your ` package.json ` and
100+ ` package-lock.json ` files, you can use ` npm link <dep> --save ` to do so.
101+
81102### See Also
82103
83104* [ npm developers] ( /using-npm/developers )
0 commit comments