@@ -15,41 +15,44 @@ alias: npm ln
15
15
16
16
### Description
17
17
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
+
18
21
Package linking is a two-step process.
19
22
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).
24
28
25
29
Next, 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.
28
32
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.
31
35
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.
34
39
35
40
When 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 ` .
40
43
41
44
For example:
42
45
43
46
``` 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
48
51
```
49
52
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.
53
56
54
57
You may also shortcut the two steps in one. For example, to do the
55
58
above use-case in a shorter way:
@@ -69,15 +72,33 @@ npm link redis
69
72
That is, it first creates a global link, and then links the global
70
73
installation target into your project's ` node_modules ` folder.
71
74
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 ` .
74
77
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.
76
80
77
81
``` bash
78
82
npm link @myorg/privatepackage
79
83
```
80
84
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
+
81
102
### See Also
82
103
83
104
* [ npm developers] ( /using-npm/developers )
0 commit comments