npm link and unlink package


This post goes over how to link and unlink npm packages.

Prerequisites

Assuming you have 2 packages:

  1. pkg-a
  2. pkg-b

Go to your package directory that contains package.json:

cd path/to/pkg-a

Create a global symlink:

npm link

Verify that the global link is created:

npm ls --global --depth=0

You should see something like:

/Users/remarkablemark/.nvm/versions/node/v18.18.1/lib
├── [email protected] -> ./../../../../../pkg-a
└── [email protected]

Go to another package directory:

cd path/to/pkg-b

Link install package pkg-a:

npm link pkg-a

Verify the package is installed:

npm ls pkg-a

You should see something like:

pkg-b@ /path/to/pkg-b
└── [email protected] -> ./../pkg-a

Given you’re still in package pkg-b directory:

cd path/to/pkg-b

Unlink package pkg-a:

npm unlink pkg-a

Go to package pkg-a directory:

cd path/to/pkg-a

Unlink the package globally:

npm unlink --global

Verify the global package is unlinked:

npm ls -g pkg-a

You should see the following:

/Users/remarkablemark/.nvm/versions/node/v18.18.1/lib
└── (empty)


Please support this site and join our Discord!