2019-11-05 14:55:08 -05:00
|
|
|
---
|
|
|
|
section: cli-commands
|
|
|
|
title: npm-link
|
|
|
|
description: Symlink a package folder
|
|
|
|
---
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
# npm-link
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
## Symlink a package folder
|
2015-10-09 23:13:57 -07:00
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
### Synopsis
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
```bash
|
|
|
|
npm link (in package dir)
|
|
|
|
npm link [<@scope>/]<pkg>[@<version>]
|
|
|
|
|
|
|
|
alias: npm ln
|
|
|
|
```
|
|
|
|
|
|
|
|
### Description
|
2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
Package linking is a two-step process.
|
|
|
|
|
2016-03-29 23:30:51 -07:00
|
|
|
First, `npm link` in a package folder will create a symlink in the global folder
|
|
|
|
`{prefix}/lib/node_modules/<package>` that links to the package where the `npm
|
2019-11-05 14:55:08 -05:00
|
|
|
link` command was executed. (see [`npm-config`](npm-config) for the value of `prefix`). It
|
2016-09-22 07:59:37 -07:00
|
|
|
will also link any bins in the package to `{prefix}/bin/{name}`.
|
2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
Next, in some other location, `npm link package-name` will create a
|
2016-03-29 23:30:51 -07:00
|
|
|
symbolic link from globally-installed `package-name` to `node_modules/`
|
|
|
|
of the current folder.
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2013-08-16 08:19:31 -07:00
|
|
|
Note that `package-name` is taken from `package.json`,
|
2013-05-14 14:37:59 -07:00
|
|
|
not from directory name.
|
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
The package name can be optionally prefixed with a scope. See [`npm-scope`](npm-scope).
|
2015-01-08 14:37:26 -08:00
|
|
|
The scope must be preceded by an @-symbol and followed by a slash.
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2011-11-21 09:48:45 -08:00
|
|
|
When creating tarballs for `npm publish`, the linked packages are
|
|
|
|
"snapshotted" to their current state by resolving the symbolic links.
|
|
|
|
|
2014-09-24 14:41:07 -07:00
|
|
|
This is handy for installing your own stuff, so that you can work on it and
|
|
|
|
test it iteratively without having to continually rebuild.
|
2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
```bash
|
2011-11-21 09:48:45 -08:00
|
|
|
cd ~/projects/node-redis # go into the package directory
|
|
|
|
npm link # creates global link
|
|
|
|
cd ~/projects/node-bloggy # go into some other package directory.
|
|
|
|
npm link redis # link-install the package
|
2019-11-05 14:55:08 -05:00
|
|
|
```
|
2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
Now, any changes to ~/projects/node-redis will be reflected in
|
2015-03-13 02:07:27 -07:00
|
|
|
~/projects/node-bloggy/node_modules/node-redis/. Note that the link should
|
2016-03-29 23:30:51 -07:00
|
|
|
be to the package name, not the directory name for that package.
|
2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
You may also shortcut the two steps in one. For example, to do the
|
|
|
|
above use-case in a shorter way:
|
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
```bash
|
|
|
|
cd ~/projects/node-bloggy # go into the dir of your main project
|
|
|
|
npm link ../node-redis # link the dir of your dependency
|
|
|
|
```
|
2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
The second line is the equivalent of doing:
|
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
```bash
|
|
|
|
(cd ../node-redis; npm link)
|
|
|
|
npm link redis
|
|
|
|
```
|
2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
That is, it first creates a global link, and then links the global
|
|
|
|
installation target into your project's `node_modules` folder.
|
|
|
|
|
2018-07-18 13:55:52 -07:00
|
|
|
Note that in this case, you are referring to the directory name, `node-redis`,
|
|
|
|
rather than the package name `redis`.
|
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
If your linked package is scoped (see [`npm-scope`](npm-scope)) your link command must
|
2014-09-24 14:41:07 -07:00
|
|
|
include that scope, e.g.
|
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
```bash
|
|
|
|
npm link @myorg/privatepackage
|
|
|
|
```
|
2014-09-24 14:41:07 -07:00
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
### See Also
|
2011-11-21 09:48:45 -08:00
|
|
|
|
2019-11-05 14:55:08 -05:00
|
|
|
* [npm developers](/using-npm/developers)
|
|
|
|
* [package.json](/configuring-npm/package-json)
|
|
|
|
* [npm- nstall](/cli-commands/npm-install)
|
|
|
|
* [npm folders](/configuring-npm/folders)
|
|
|
|
* [npm config](/cli-commands/npm-config)
|
|
|
|
* [npmrc](/configuring-npm/npmrc)
|