How do I uninstall a package installed using npm link? - node.js

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

you can use unlink to remove the symlink.
For Example:
cd ~/projects/node-redis
npm link
cd ~/projects/node-bloggy
npm link redis # links to your local redis
To reinstall from your package.json:
npm unlink redis
npm install
https://www.tachyonstemplates.com/npm-cheat-sheet/#unlinking-a-npm-package-from-an-application

npm link pain:
-Module name gulp-task
-Project name project-x
You want to link gulp-task:
1: Go to the gulp-task directory then do npm link this will symlink the project to your global modules
2: Go to your project project-x then do npm install make sure to remove the current node_modules directory
Now you want to remove this madness and use the real gulp-task, we have two options:
Option 1: Unlink via npm:
1: Go to your project and do npm unlink gulp-task this will remove the linked installed module
2: Go to the gulp-task directory and do npm unlink to remove symlink. Notice we didn't use the name of the module
3: celebrate
What if this didn't work, verify by locating your global installed module. My are location ls -la /usr/local/lib/node_modules/ if you are using nvm it will be a different path
Option 2: Remove the symlink like a normal linux guru
1: locate your global dependencies cd /usr/local/lib/node_modules/
2: removing symlink is simply using the rm command
rm gulp-task make sure you don't have / at the end
rm gulp-task/ is wrong πŸ”₯🚨
rm gulp-task βœ”οΈ

npm uninstall --global my-package

This worked for me:
check the list of npm global packages:
npm ls --global
uninstall your package:
npm uninstall --global my-package
go to your testbed and unlink the linked package:
npm unlink my-package
navigate to your testbed directory and reinstall packages:
npm install
restart your testbed server

If you've done something like accidentally npm link generator-webapp after you've changed it, you can fix it by cloning the right generator and linking that.
git clone https://github.com/yeoman/generator-webapp.git;
# for fixing generator-webapp, replace with your required repository
cd generator-webapp;
npm link;

"npm install" replaces all dependencies in your node_modules installed with "npm link" with versions from npmjs (specified in your package.json)

You can undo the link command with the unlink command.
Create a link
In package
cd ./some-package
npm link
In comsumer
cd ./some-project
npm link some-package
Remove a link
Removing a link should be done in the reverse order - start from the consumers.
cd ./some-project
npm unlink some-package
In package
cd ./some-package
npm unlink

Related

npm link removes child dependencies

I am trying to do local development of an NPM package and test it in a package which depends on it. I'm using NPM (7.5.3) and specifically npm link for this but running into a problem with the chain of dependencies.
The child package has dependencies, these are all added to the parent's node_modules folder when using npm install "git+https://github.com/name/child_package". But when I npm link that module:
cd child_package
npm link
cd ../parent_package
npm link child_package
With the last command run (npm link child_package), all of the dependencies for child_package which were in the node_modules of parent_package are removed. NPM reporting:
removed 60 packages, changed 1 package, and audited 231 packages in 1s
At which point all the compilation in the parent package fails due to the missing deps. It finds the child_package, which is symlinked as expected, but dependency defined in child_package of "gsap" has now been removed.
If I reinstall it using npm install "git+https://github.com/name/child_package" it will add the deps back into the node_modules folder of the parent project.
try to do the following:
cd child_package
npm install
that will install child dependencies to directory of child package
personally I hate npm link and always use npm publish (use version number like 1.0.0-preview.1 for your child package and remove '-preview.Number' when you are done)
This is a behavior introduced in npm V7 + .
The only reasonable "workaround" i have found is to go back to npm 6 (npm install -g npm#6).
Another "workaround" is to npm install --no-save ../../my-local-module but to reflect changes to the local module you will need to delete it from node_modules and reinstall again. Kind of lame....

How to install node.js modules downloaded manually [duplicate]

There are quite a few modules which are listed on node's github page but are not published with the npm-registry. These modules can't be installed using npm.
What is the correct way to install these nodejs modules after cloning them from Git?
You need to download their source from the github. Find the main file and then include it in your main file.
An example of this can be found here > How to manually install a node.js module?
Usually you need to find the source and go through the package.json file. There you can find which is the main file. So that you can include that in your application.
To include example.js in your app. Copy it in your application folder and append this on the top of your main js file.
var moduleName = require("path/to/example.js")
These modules can't be installed using npm.
Actually you can install a module by specifying instead of a name a local path. As long as the repository has a valid package.json file it should work.
Type npm -l and a pretty help will appear like so :
CLI:
...
install npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install <pkg>
npm install <pkg>#<tag>
npm install <pkg>#<version>
npm install <pkg>#<version range>
Can specify one or more: npm install ./foo.tgz bar#stable /some/folder
If no argument is supplied and ./npm-shrinkwrap.json is
present, installs dependencies specified in the shrinkwrap.
Otherwise, installs dependencies from ./package.json.
What caught my eyes was: npm install <folder>
In my case I had trouble with mrt module so I did this (in a temporary directory)
Clone the repo
git clone https://github.com/oortcloud/meteorite.git
And I install it globally with:
npm install -g ./meteorite
Tip:
One can also install in the same manner the repo to a local npm project with:
npm install ../meteorite
And also one can create a link to the repo, in case a patch in development is needed:
npm link ../meteorite
Edit:
Nowadays npm supports also github and git repositories (see https://docs.npmjs.com/cli/v6/commands/npm-install), as a shorthand you can run :
npm i github.com:some-user/some-repo
Download the code from github into the node_modules directory
var moduleName = require("<name of directory>")
that should do it.
if the module has dependancies and has a package.json, open the module and enter npm install.
Hope this helps
You can clone the module directly in to your local project.
Start terminal. cd in to your project and then:
npm install https://github.com/repo/npm_module.git --save
Step-by-step:
let's say you are working on a project use-gulp which
uses(requires) node_modules like gulp and gulp-util.
Now you want to make some modifications to gulp-util lib and test it locally with your use-gulp project...
Fork gulp-util project on github\bitbucket etc.
Switch to your project: cd use-gulp/node_modules
Clone gulp-util as gulp-util-dev : git clone https://.../gulp-util.git gulp-util-dev
Run npm install to ensure dependencies of gulp-util-dev are available.
Now you have a mirror of gulp-util as gulp-util-dev. In your use-gulp project, you can now replace: require('gulp-util')...; call with : require('gulp-util-dev') to test your changes you made to gulp-util-dev

npm installs all modules in /usr/local/lib/node_modules/

I have node.js 0.8.14 installed on Ubuntu 12.10. I created a directory in my home directory with a sub directory node_modules. I want to install some local node modules there but running
npm install myModule
in this directory installs this module in /usr/local/lib/node_modules/ (same behavior as installing the module with the -g flag
There is no node path in .bashrc.
Any idea how I can install local node modules?
After some further research I found the solution.
Running the command npm config ls revealed that the default config global=false (you see the default config with npm config ls -l) was overwritten by global=true in /home/vsdev/.npmrc and /usr/local/etc/npmrc.
Reverting this to global=false solved the issue.
That is odd.
FYI you don't need to create the node_modules directory, npm will do that for you
npm normally just installs to the current directory. Even if the package you are installing is configured to prefer global installation, npm will install it locally unless you explicitly pass the -g parameter.
can you run the following shell commands and confirm npm is really the real npm?
which npm
alias | grep npm
npm install load all in node_modules then it might be version 3 behaviour http://blog.npmjs.org/post/110924823920/npm-weekly-5 or as mentioned by #vsdev so once you make sure it version 3 behaviour and u want to go with it then its fine else follow below
1- uninstall all modules.. into the node_modules folder in your project then execute: npm uninstall *
2- Tell npm to install with legacy bundling for this one install:
npm install --legacy-bundling
A "permanent" alternative:
Set your npm config to always use legacy bundling...
npm set legacy-bundling=true
.. and run as usual:
npm install
*fetching dependencies with legacy bundling will take a lot more time because many several different versions of the same dependencies will be installed.

How can I uninstall npm modules in Node.js?

As commonly known, any npm module can be installed by running a simple command: npm install <module_name>.
I have installed a few modules that I do not use any more and I just want to get them off. I have a few questions regarding this:
Do we have any command or process to uninstall a module from the root (something like npm uninstall <module_name>)
or will simply removing the module files do?
How does it affect us if we keep the unused modules?
The command is simply npm uninstall <name>
The Node.js documents https://npmjs.org/doc/ have all the commands that you need to know with npm.
A local install will be in the node_modules/ directory of your application. This won't affect the application if a module remains there with no references to it.
If you're removing a global package, however, any applications referencing it will crash.
Here are different options:
npm uninstall <name> removes the module from node_modules but does not update package.json
npm uninstall <name> --save also removes it from dependenciesin package.json
npm uninstall <name> --save-dev also removes it from devDependencies in package.json
npm uninstall -g <name> --save also removes it globally
If it doesn't work with npm uninstall <module_name> try it globally by typing -g.
Maybe you just need to do it as an superUser/administrator with sudo npm uninstall <module_name>.
Well, to give a complete answer to this question, there are two methods (for example we call the installed module as module1):
To remove module1 without changing package.json:
npm uninstall module1
To remove module1 with changing package.json, and removing it from the dependencies in package.json:
npm uninstall --save module1
Note: to simplify the above mentioned commands, you can use -S instead of --save , and can use remove, rm, r, un, unlink instead of uninstall
I just install stylus by default under my home dir, so I just use npm uninstall stylus to detach it, or you can try npm rm <package_name> out.
To uninstall the Node.js module:
npm uninstall <module_name>
This will remove the module from folder node_modules, but not from file package.json. So when we do npm install again it will download the module.
So to remove the module from file package.json, use:
npm uninstall <module_name> --save
This also deletes the dependency from file package.json.
And if you want to uninstall any globally module you can use:
npm -g uninstall <module_name> --save
This will delete the dependency globally.
To remove packages in folder node_modules in bulk, you could also remove them from file package.json, save it, and then run npm prune in the terminal.
This will remove those packages, which exist in the file system, but are not used/declared in file package.json.
P.S.: This is particularly useful on Windows, as you may often encounter problems with being unable to delete some files due to the "exceeded path length limit".
Sometimes npm uninstall -g packageName doesn’t work.
In this case you can delete package manually.
On Mac, go to folder /usr/local/lib/node_modules and delete the folder with the package you want. That's it. Check your list of globally installed packages with this command:
npm list -g --depth=0
You can also run the following as shorthand:
npm un packageName or npm rm packageName
Note: Add -g at end of command to uninstall global packages.
Update for npm 5:
As of npm 5.0.0, installed/uninstalled modules are added/removed as a dependency by default, so the --save option is no longer needed.
Run
npm uninstall <package>
For example:
npm uninstall mongodb
It will remove the module from the node_modules folder and also the package.json file.
Alias can be used to to uninstall node_modules package
un alias for uninstall
removes single package
- npm un <PACKAGE_NAME>
removes multiple packages by adding space between packages names
- npm un <PACKAGE_NAME_1> <PACKAGE_NAME_2>
removes all node_modules packages
- rm -rf node_modules/
I found this out the hard way, even if it is seemingly obvious.
I initially tried to loop through the node_modules directory running npm uninstall module-name with a simple for loop in a script. I found out it will not work if you call the full path, e.g.,
npm uninstall module-name
was working, but
npm uninstall /full/path/to/node_modules/module-name
was not working.
For Windows users - if you want to remove all the Node.js modules installed at once:
Open a PowerShell window
Go inside the node_modules folder (cd node_modules)
Run this command - "npm uninstall (Get-ChildItem).Name"
It will uninstall all the modules.
To uninstall a module using npm, you can use:
npm uninstall moduleName
Also, if you want to uninstall and want the change to be reflected in your package.json then you can use the --save flag, like this:
npm uninstall moduleName --save
OR
npm uninstall -S
And if you want to uninstall a module from devDependencies and want the change to be reflected in package.json then you can use -D flag, like this:
npm uninstall moduleName -D
The uninstall option didn't work for me when I tried to use the same command to the one I used in installing (as I was installing with the #latest directive)
So for example, I installed a package like this:
npm install #ngtools/webpack#latest
And then I wanted to uninstall it, so I used the same command (including #latest):
npm uninstall #ngtools/webpack#latest
So the above uninstall didn't work. I have to remove the #latest, and then it worked well:
npm uninstall #ngtools/webpack
In npm v6+ npm uninstall <package_name> removes it both in folder node_modules and file package.json.
Simply,
npm un module_name
OR
npm uninstall module_name
Make sure you uninstall it in the same directory as the package.json and node_modules folder.
Additionally, if you've started using yarn, in place of npm:
yarn remove <package-name>
Is the equivalent of:
npm uninstall <package-name> --save
This will
- remove the package from package.json, as well as
- uninstall it from your project's node-modules folder
# Log in as root (might be required depending on install)
su -
# List all global packages
npm ls -g --depth=0
# List all local (project) packages
npm ls -p --depth=0
# Remove all global packages
npm ls -g --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm
# Remove all local packges
npm ls -p --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -p rm
# NOTE (optional): to use node with sudo you can add the bins to /usr/bin
# NOTE $PATHTONODEINSTALL is where node is installed (e.g. /usr/local/node)
sudo ln -s $PATHTONODEINSTALL/bin/node /usr/bin/node
sudo ln -s $PATHTONODEINSTALL/bin/npm /usr/bin/npm
The simplest solution is:
npm uninstall packageName --save-dev
See upper level packages names in the your project:
npm list --depth=0
The output will be like:
app#0.1.0 /home/jackkobec/projects/myAppName
β”œβ”€β”€ packageName#packageVersion
β”œβ”€β”€ express#4.16.4
Copy package name and execute npm uninstall command. Example for express package:
npm uninstall express --save-dev
If you want to uninstall an specific package using npm,
you can use the following command :
Sintax:
npm uninstall <package-name>
Example:
npm uninstall moment
If you want to uninstall a number of modules, then just run the npm uninstall.
Then go to file package.json and delete the unwanted module from there, and then just run the command npm install. It should fix your problem.
In case you are on Windows, run CMD as administrator and type:
npm -g uninstall <package name>
You can delete a Node.js module manually. For Windows,
Go to the node_modules directory of your repository.
Delete the Node.js module you don't want.
Don't forget to remove the reference to the module in your package.json file! Your project may still run with the reference, but you may get an error. You also don't want to leave unused references in your package.json file that can cause confusion later.

How to install a node.js module without using npm?

There are quite a few modules which are listed on node's github page but are not published with the npm-registry. These modules can't be installed using npm.
What is the correct way to install these nodejs modules after cloning them from Git?
You need to download their source from the github. Find the main file and then include it in your main file.
An example of this can be found here > How to manually install a node.js module?
Usually you need to find the source and go through the package.json file. There you can find which is the main file. So that you can include that in your application.
To include example.js in your app. Copy it in your application folder and append this on the top of your main js file.
var moduleName = require("path/to/example.js")
These modules can't be installed using npm.
Actually you can install a module by specifying instead of a name a local path. As long as the repository has a valid package.json file it should work.
Type npm -l and a pretty help will appear like so :
CLI:
...
install npm install <tarball file>
npm install <tarball url>
npm install <folder>
npm install <pkg>
npm install <pkg>#<tag>
npm install <pkg>#<version>
npm install <pkg>#<version range>
Can specify one or more: npm install ./foo.tgz bar#stable /some/folder
If no argument is supplied and ./npm-shrinkwrap.json is
present, installs dependencies specified in the shrinkwrap.
Otherwise, installs dependencies from ./package.json.
What caught my eyes was: npm install <folder>
In my case I had trouble with mrt module so I did this (in a temporary directory)
Clone the repo
git clone https://github.com/oortcloud/meteorite.git
And I install it globally with:
npm install -g ./meteorite
Tip:
One can also install in the same manner the repo to a local npm project with:
npm install ../meteorite
And also one can create a link to the repo, in case a patch in development is needed:
npm link ../meteorite
Edit:
Nowadays npm supports also github and git repositories (see https://docs.npmjs.com/cli/v6/commands/npm-install), as a shorthand you can run :
npm i github.com:some-user/some-repo
Download the code from github into the node_modules directory
var moduleName = require("<name of directory>")
that should do it.
if the module has dependancies and has a package.json, open the module and enter npm install.
Hope this helps
You can clone the module directly in to your local project.
Start terminal. cd in to your project and then:
npm install https://github.com/repo/npm_module.git --save
Step-by-step:
let's say you are working on a project use-gulp which
uses(requires) node_modules like gulp and gulp-util.
Now you want to make some modifications to gulp-util lib and test it locally with your use-gulp project...
Fork gulp-util project on github\bitbucket etc.
Switch to your project: cd use-gulp/node_modules
Clone gulp-util as gulp-util-dev : git clone https://.../gulp-util.git gulp-util-dev
Run npm install to ensure dependencies of gulp-util-dev are available.
Now you have a mirror of gulp-util as gulp-util-dev. In your use-gulp project, you can now replace: require('gulp-util')...; call with : require('gulp-util-dev') to test your changes you made to gulp-util-dev

Resources