How to relocate global packages? - node.js

In the process of getting familiar with npm, ive installed a bunch of stuff globally, including angular cli - into my users directory on windows (C:\users...)
How can I either relocate this directory or uninstall and reinstall to a different folder?

You could do it one by one by
npm uninstall angular-cli
npm -g install angular-cli
without -g will interact with current directory package.json while with -g do it globally (your %USER_PROFILE%)

I don't think you can change directories like that. Global installation chooses the path to {prefix}/lib/node_modules by default as the program refers to this path when looking for -g modules. You can't change them.
For local installation files automatically go to ./node_modules/.bin/ and program checks from there.
Please see https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/

Related

npm install command - please explain

I am starting to learn React Native and I am very new to npm package manager. I read that npm can install packages localy or globaly but I am trying to understand what does that mean.
I am reading this page https://docs.npmjs.com/getting-started/installing-npm-packages-locally, can someone explain to me what does this mean please.
If you want to depend on the package from your own module using something like Node.js' require, then you want to install locally, which is npm install's default behavior. On the other hand, if you want to use it as a command line tool, something like the grunt CLI, then you want to install it globally.
Since I am very new to npm, React Native, Node (never used it), I am confused by the very first sentence in this quote. What does it mean "my own module?
If I want to use CRNA, I guess, I would have to install it globally?
If I am to install a package, say CRNA locally or globally, where do I see it installed on my MacBook Pro?
The difference between local and global install is that local install puts it into the node_modules directory of your project (this is what is referred to as "your own module") while global puts it into a system directory (the exact location depends on your OS, on OSX it should be /usr/local/lib/node_modules).
Basically:
Local install ties the installed module to your project: other projects on your computer do not get it but if your project is copied to another computer the module will be installed there too
global install ties it to your computer: you can use it on all of
your projects on your computer but if your project is copied to
another computer the installed module will not be there
And yes, CRNA should be installed globally as it is a general tool not a project's library dependence.
When you install package globally npm install -g <package name> modules drops in {prefix}/lib/node_modules.
Locally - npm install <package name> - drops package in the current working directory.
If you are going to require module in your project you have to install it locally.
If you want to run in from command line you need to install it globally.
If you need more extenden explanation take a look
Since I am very new to npm, React Native, Node (never used it), I am confused by the very first sentence in this quote. What does it mean "my own module?
If you have package.json file, then everything in the same folder is treated as "module". You add dependencies to it by doing npm install --save foo (--save option adds it under dependencies in your package.json).
If I want to use CRNA, I guess, I would have to install it globally?
Not sure what "CRNA" is. But general rule is that mostly everything (libraries...) are installed locally. Which means that they are added to your package.json and installed in same folder under node_modules.
Only case when you want to install something globally (can be added to package.json but is NOT installed in the same folder under node_modules but probably in your home directory), by doing npm install --global bar (--global installs it globally). Is when tool (not library) is project independent, as you can access it from everywhere. Something like create-react-app.
TLDR:
Local are dependencies (libraries) installed in same folder and (usually) added in your package.json as dependencie.
Global are tools installed in your user home folder and (usually) NOT added in your package.json as dependencie.
Let first start with how nodejs finds package.
Suppose you have some folder structure like-
root
-pixel
-project1
-project2
So, if your are working on project1 and required some npm package, nodejs tries to find a folder named node_modules in current directory. If fails, it goes parent(pixel folder) and tries to find node_modules and goes recursively upto root(which is global).
So, if there any package installed globally, you don't need to install it in your current working directory.
So, why don't we install all packages globally? Isn't it saves our harddisk memory?
Yes, true. But as npm packages are updating and changing its version everytime, its necessary to use specific package in your current working package to avoid collusion.
Then how global packages is useful?
Its good idea to install some cli packages to run directly from command line i.e webpack to easy our task.

What does "npm install -g" do?

I am trying to install Less from NPM by running npm install -g less in the command line. I checked the docs for the install command:
In global mode (ie, with -g or --global appended to the command), it
installs the current package context (ie, the current working
directory) as a global package.
What does it mean by "global package"?
You are not required to install Less globally.
Installing it locally means the module will be available only for a specific project (the directory you were in when you ran npm install), as it installs to the local node_modules folder.
A global install will instead put the module into your global package folder (OS dependent), and allows you to run the included executable commands from anywhere. Note that by default you can only require local packages in your code.
See the node.js docs for more info on global vs local packages.
Generally speaking, you should install most modules locally, unless they provide a CLI command that you want to use anywhere.
In the end, I suggest you install less globally, as less provides an executable command that you will likely need in different projects. This is also what the Less docs recommend.
From: https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/
There are two ways to install npm packages:
globally —- This drops modules in {prefix}/lib/node_modules, and puts executable files in {prefix}/bin, where {prefix} is usually something like /usr/local. It also installs man pages in {prefix}/share/man, if they’re supplied.
locally —- This installs your package in the current working directory. Node modules go in ./node_modules, executables go in ./node_modules/.bin/, and man pages aren’t installed at all.
It simply means that the package you are installing while be available/integrated throughout your Nodejs platform.

Does npm touch anything besides the node_modules folder when installing a package locally?

I'm only curious about installing packages locally in a project - not globally.
Is there any difference between npm uninstall some-package and just deleting the some-package folder from the node_modules directory?
After reading that: https://npmjs.org/doc/files/npm-folders.html I would say no. There is no difference, when using local modules between npm uninstall and deleting the directory.
However, I think the /tmp directory is used when there is additional process during the installation (Compilation, etc...). Therefore, it is possible that the uninstall command remove those files if needed (But I can't see that in the present documentation)
My two cents

Where does npm store node modules?

I just setup official node.js on windows which includes npm in custom directory d:\myserver\nodejs
I tested npm with
npm install less
it works but I can't see any less directory in node_modules\npm\node_modules subdirectory.
Where could I find it (I guess the name of less module is actually less).
under : node_modules
just check where you where pointer "in what directory were you" at the time of the installation
d:\myserver\nodejs -> node_modules
Just for further clarification, google brought me here while looking up where global modules are stored (installed via npm install -g ...).
From the documentation
Local install (default): puts stuff in ./node_modules of the current package root.
Global install (with -g): puts stuff in /usr/local or wherever node is installed.
Install it locally if you’re going to require() it.
Install it globally if you’re going to run it on the command line.
If you need both, then install it in both places, or use npm link.

npm package.json install globally?

Is there any way to set an NPM dependency to be installed globally in the package.json file?
Whenever I run npm update all the dependencies are installed locally.
Thanks.
I believe that the -g option causes things to be installed globally.
Is your reason for installing globally in order to make new scripts available on the command line? If so, I might have a workaround for you.
Just install your packages as usual (without the -g):
npm install -S my_module_name
Including the -S flag or --save will help keep your package.json file up to date.
As usual, your project's npm install step will install locally (as you have described). However, it will also produces a local folder containing symlinks to each of the project's npm-supplied command-line executables (located inside the node_modules/.bin/ folder).
Add that folder to your system path to enable command-line access to npm modules without requiring installation via -g, or root access to a machine:
export PATH=/path/to/your/project/source/node_modules/.bin/:$PATH

Resources