Locally installed versus globally installed NPM modules

34,958

Solution 1

Installing locally makes bower available to the current project (where it stores all of the node modules in node_modules). This is usually only good for using a module like so var module = require('module'); It will not be available as a command that the shell can resolve until you install it globally npm install -g module where npm will install it in a place where your path variable will resolve this command.

Edit: This documentation explains it pretty thorougly.

Solution 2

You can execute your local instance by typing the line below in cmd:

node_modules/bower/bin/bower <bower args>

Solution 3

We use both PHP and JavaScript, so we have composer and npm.

Each of the projects we work on have different packages both for runtime of the package as well as build/dev tools.

As there are version constraints in each project, installing version x of a package globally (that would be run from the command line), would cause us issues, we install all the tooling in each package. Much easier to define in the appropriate composer.json / package.json files.

But running the CLI tools is a pain if you have to constantly add an additional path to the command.

To that end, we have recommend to the team that the following paths are added to your $PATH in the appropriate .bashrc (or equivalent):

./vendor/bin:./node_modules/.bin

(EDIT: For Windows, the paths would be .\vendor\bin;.\node_modules\.bin;)

So, whilst in project X, we have access to the CLI tools for that project. Switch to project Y, and we get that projects tools.

Sure, you are going to get duplications, but each project is maintained by different teams (and some people are in multiple teams), so again, having 1 version in the global setup is an issue there.

Solution 4

Usually you install NPM modules globally if you want them included in your path to be ran from the command line. Since it is installed locally you will have to run it from the node_modules folder.

Share:
34,958
Rigil
Author by

Rigil

Updated on August 28, 2020

Comments

  • Rigil
    Rigil over 3 years

    In my package.json file, I have bower listed as a dependency. After I run npm install, bower gets installed locally. When I try to run bower after installing it locally I get an error

    "bower" is not recognized as an internal or external command

    It seems the only way to resolve this is to install bower globally. Why should I have to do this? If my project contains a local copy of bower, why won't node use it?