Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running npm globally installed packages

Can someone please explain how do node's globally installed behave. It is really confusing me. If I install a package (with executables) such as http-serverglobally I can run it with:

http-server

But if I do

node http-server

I get

module.js:339
    throw err;
    ^

Error: Cannot find module '/path/to/current/dir/http-server'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:136:18)
    at node.js:972:3

I suspect my ternpackage in emacs is trying to run it with node hence breaking. Why is this happening? Why can't node find the path to it's own modules?

like image 535
Arijoon Avatar asked Dec 27 '15 20:12

Arijoon


People also ask

How do I get npm packages installed globally?

To view the npm global packages list and their dependencies, you can run the following npm list command followed by the “-g” flag where g stands for global. As you can see in the above result, all the global packages are displayed in a tree-like structure.

How do I use Global npm packages?

To install a module from npm globally, you'll simply need to use the --global flag when running the install command to have the module install globally, rather than locally (to the current directory). Note: One caveat with global modules is that, by default, npm will install them to a system directory, not a local one.

Should I install packages globally npm?

Many node packages and tools will encourage you to install their tools globally. This is a bad practice and should be avoided. Some examples of this are Angular, Grunt, Gulp, Karma, Verdaccio, Snyk, React Native.

Should I install npm packages globally or locally?

It's best to install locally when relying on a package from your module, such as Node. js. This is how npm install works by default. The grunt CLI package, for example, must be installed globally before it can be used as a command-line tool.


1 Answers

There are two ways installing packages: globally and locally.
Locally installed package files end up in your local node_modules (in your project folder where you called npm install some-package).
Globally installed package files end up in your system so they are available in command line, if globally installed packages provides executable then you can invoke it in command line directly some-package(without node), if it does not provide executable then you can use it in repl mode (node) like var package = require('some-package') and it is also available locally (inside your project folder even if you don't have it installed locally).

like image 162
Bek Avatar answered Oct 01 '22 06:10

Bek