Node.js

How do I uninstall a package installed using npm link

19 September 2026 · 10 min read

How do I uninstall a package installed using npm link

Have you ever used npm link to connect a local package to another project and then struggled to remove it? Understanding how to properly uninstall a package installed using npm link is crucial for maintaining a clean and organized development environment. The npm link command is incredibly useful for testing and developing packages locally without having to repeatedly publish them to a registry. However, when you’re done testing, knowing how to cleanly remove the link is essential to avoid conflicts and ensure your project relies on the correct package versions. This guide will walk you through the steps necessary to safely and effectively unlink and uninstall these packages, preventing potential headaches down the road. We’ll cover everything from identifying linked packages to verifying their complete removal, ensuring your projects remain stable and predictable.

The npm link command is a powerful feature in Node Package Manager (npm) that allows you to simulate installing a package from the npm registry without actually publishing it. This is exceptionally useful during package development, as it enables you to test changes in real-time within another project. When you use npm link in a package directory, it creates a symbolic link in npm’s global installation prefix (typically /usr/local/lib/node_modules or similar). This symbolic link points back to your package’s directory. Then, when you run npm link package-name in another project, npm creates another symbolic link in that project’s node_modules folder, pointing to the globally linked package. This means any changes you make to the original package are immediately reflected in the project using the link. This process avoids the need to repeatedly publish and install versions from a remote registry during development. It is a more efficient way to develop packages.

However, this convenience comes with a caveat. Unlike regular npm installations, removing a linked package isn’t as straightforward as running npm uninstall. Because symbolic links are involved, simply uninstalling the package from the project where it’s linked only removes the link, not the original global link. This can lead to confusion and unexpected behavior if you’re not aware of the underlying mechanism. According to the npm documentation, “Using npm link is a two-step process: first, you create a global link, and then you link that global package into your project” [^1^]. This also means the unlinking process requires a similar two-step approach to ensure complete removal. A common misconception is that a standard npm uninstall will suffice. This is not the case, and failing to properly unlink can lead to version conflicts and unexpected dependencies.

Consider a scenario where you’re developing a UI component library. You use npm link to test your components in a separate application project. After finishing the development and publishing the library to npm, you might forget to unlink the local version. The application project might then continue to use the local, potentially outdated, version of the component library instead of the published one. This discrepancy can cause bugs and inconsistencies that are difficult to trace if you’re unaware that the link is still active.

Step-by-Step Guide to Unlinking and Uninstalling

The process of properly uninstalling a package installed using npm link involves two key steps: unlinking the package from the project where it’s being used and then unlinking it globally. This ensures that all symbolic links are removed, and the project reverts to using the version installed from the npm registry (if any). Here’s a detailed guide to walk you through the process:

  1. Navigate to the project directory: Open your terminal and navigate to the project where you linked the package. This is the project where you ran npm link package-name.
  2. Unlink the package: Run the command npm unlink package-name. This command removes the symbolic link from the project’s node_modules directory. You should see a message confirming that the package has been unlinked.
  3. Navigate to the package directory: Now, navigate to the directory of the package you linked. This is the directory where you initially ran npm link.
  4. Unlink the package globally: Run the command npm unlink (without the package name). This command removes the global symbolic link that was created when you first linked the package. You should see a message confirming that the global link has been removed.
  5. Verify the removal: Finally, go back to your project directory and run npm ls package-name. This command lists installed packages. If the package is no longer listed (or if it’s listed as a regular dependency and not a linked one), you have successfully unlinked and uninstalled it.

Let’s illustrate this with an example. Suppose you have a package named “my-awesome-package” and you’ve linked it to your “my-app” project. First, you would navigate to the “my-app” directory and run npm unlink my-awesome-package. Next, you would navigate to the “my-awesome-package” directory and run npm unlink. Finally, back in the “my-app” directory, running npm ls my-awesome-package should confirm that the package is no longer linked.

It’s important to remember that unlinking only removes the symbolic links. It does not uninstall the package from the project if it was also installed as a regular dependency. If you want to completely remove the package, you need to run npm uninstall package-name in your project directory after unlinking it. This ensures that the package is removed from your node_modules folder and your package.json file.

Common Issues and Troubleshooting

While the unlinking process is generally straightforward, you might encounter some common issues. One frequent problem is forgetting to unlink the package globally. If you only unlink it from the project, the global link remains, and subsequent projects might inadvertently pick up the linked version. Another issue arises when you have multiple versions of the package installed or linked in different places. This can lead to conflicts and unexpected behavior. To avoid this, always ensure you’re working with a clean environment and that you’ve properly unlinked and uninstalled all instances of the package.

Another potential pitfall is related to permissions. If you encounter errors related to file permissions, especially when running npm unlink globally, it might be necessary to run the command with administrative privileges (e.g., using sudo on macOS or Linux). However, be cautious when using sudo with npm, as it can lead to other issues. A better approach is often to configure npm to use a directory where you have write access without needing elevated privileges. This can be achieved by changing the default npm directory [^2^].

What if you’re unsure whether a package is linked? You can use the command npm ls -g --depth=0 to list globally installed packages. This will show you if your package is listed as a symbolic link (indicated by an arrow pointing to the package’s directory). Alternatively, you can manually inspect the node_modules directory of your project to check if the package is a symbolic link. If you see a file that’s actually a shortcut or symbolic link pointing to another location, then the package is linked. Using tools like ls -l (on Unix-like systems) can help you visualize these symbolic links.

To make the most of npm link while minimizing the risk of issues, follow these best practices:

  • Document your links: Keep track of which packages you’ve linked and where. This helps you remember to unlink them later and avoids confusion. A simple text file or a comment in your project’s README can be helpful.
  • Use version control: Always use version control (e.g., Git) to track changes to your project. This allows you to easily revert to a previous state if something goes wrong during the linking or unlinking process.
  • Clean up regularly: Make it a habit to periodically check for and remove any unused or forgotten links. This helps maintain a clean and predictable development environment.

Consider using tools that automate the process of managing linked packages. Some IDEs and build tools have built-in support for npm link, making it easier to link and unlink packages. For example, some VS Code extensions can help you manage linked packages and automatically unlink them when you close the project. Using these tools can streamline your workflow and reduce the chances of making mistakes. According to a Stack Overflow survey, developers who use automated tools for package management report fewer dependency-related issues [^3^].

Here’s a summary of key points to remember:

  • Always unlink from both the project and the global scope.
  • Verify the unlinking by checking npm ls.
  • Be mindful of file permissions.
Infographic here: Visual representation of the unlinking process.
**To uninstall a package installed using npm link**, always start by unlinking the package from your project using `npm unlink package-name`. Then, navigate to the package's directory and run `npm unlink` to remove the global link. This two-step process ensures that all symbolic links are removed, preventing conflicts and ensuring your project relies on the correct package versions. Remember to verify the removal by checking `npm ls package-name` in your project directory.
Q: What happens if I only unlink the package from the project and not globally?
A: If you only unlink from the project, the global link remains. This means that other projects might inadvertently use the linked version of the package instead of the one from the npm registry.
Q: Do I need to uninstall the package after unlinking it?
A: If the package was also installed as a regular dependency (i.e., using `npm install`), you'll need to uninstall it using `npm uninstall package-name` to completely remove it from your project.
Q: I'm getting permission errors when running `npm unlink`. What should I do?
A: Try running the command with administrative privileges (e.g., using `sudo` on macOS or Linux). However, a better long-term solution is to configure npm to use a directory where you have write access without needing elevated privileges.
Q: How can I check if a package is linked?
A: You can use the command `npm ls -g --depth=0` to list globally installed packages and check if your package is listed as a symbolic link. Alternatively, you can manually inspect the `node_modules` directory of your project to see if the package is a symbolic link.
Mastering `npm link` and its uninstallation process empowers you to develop and test packages efficiently. It also requires you to understand how to properly clean up when you are done testing. By following these steps and best practices, you can avoid common pitfalls and ensure a smooth development workflow. Remembering to unlink both locally and globally helps prevent unexpected dependency issues and keeps your projects running smoothly. Consider this knowledge a fundamental part of your Node.js development toolkit, leading to fewer headaches and more productive coding sessions. Learn more about managing Node.js dependencies at [our guide to Node.js best practices](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c).

So, the next time you’re working with npm link, remember these guidelines. Take the time to unlink and uninstall packages properly, and you’ll save yourself from future dependency conflicts. Explore advanced npm techniques and improve your workflow. For further learning, check out the official npm documentation and community resources like Stack Overflow. Share this guide with your fellow developers and help them master the art of uninstalling a package installed using npm link. Start practicing these techniques today, and you’ll be well on your way to becoming a more efficient and confident Node.js developer.

[^1^]: npm link documentation

[^2^]: Resolving npm permission errors

[^3^]: Stack Overflow Developer Survey 2023

Question & Answer :
When installing a node package using sudo npm link in the package’s directory, how can I uninstall the package once I’m done with development?

npm link installs the package as a symbolic link in the system’s global package location (’/usr/local/lib`). This allows you to test the package while still developing it, without having to install it over and over again.

Which npm command do I need to run to remove the link again?

The package can be uninstalled using the same uninstall or rm command that can be used for removing installed packages. The only thing to keep in mind is that the link needs to be uninstalled globally - the --global flag needs to be provided.

In order to uninstall the globally linked foo package, the following command can be used (using sudo if necessary, depending on your setup and permissions)

sudo npm rm --global foo 

This will uninstall the package.

To check whether a package is installed, the npm ls command can be used:

npm ls --global foo