pixel
pixel

Reputation: 10587

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?

Upvotes: 4

Views: 22966

Answers (4)

Fazal Rasel
Fazal Rasel

Reputation: 4526

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.

Upvotes: 1

Mentos1386
Mentos1386

Reputation: 308

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.

Upvotes: 1

dabadab
dabadab

Reputation: 418

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.

Upvotes: 3

Mikhail Katrin
Mikhail Katrin

Reputation: 2384

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

Upvotes: 1

Related Questions