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-server
globally 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 tern
package 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?
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With