Npm global modules destination - node.js

Where is npm -g storing the global modules by default? I am running archlinux.
So if I want to modify a global module where will be the location of it?

I have installed a package globally without sudo and gave me the error with the path. :) So the default place where npm is installing packages globally is /usr/lib/node_modules Thanks guys for your help.

Related

Not able to request global modules

I have installed two modules globally which are discord.js and request. When I do npm list -g, I seem them in there. When I go to my project folder and fire up a command prompt, I type node app.js and it says it cannot find discord.js, why is this? I had just reinstalled Windows and this was working perfectly before the reinstall.
When you run npm list -g you are getting the list of globally installed modules instead of those in the currently installed project.
Globally installed modules reside in something like /usr/local/lib/node_modules, whereas locally installed modules reside in /your_project/node_modules.
From the npm documentation for npm ls -g (aliased in your case to npm list -g):
global
Default: false
Type: Boolean
List packages in the global install prefix instead of in the current project.
The Node.js require does not look in the global node_modules folder, only your local node_modules folder.
You can either add your global node_modules folder to your NODE_PATH environment variable or install using npm install without the -g flag. When you run npm install --save <module name> you install the module in the node_modules folder for the current project, which allows your project to run properly.
Apparently I had to do npm install --save <module name>
Even though it's the answer to my problem, can anybody answer why it did this?

Node express command not found

i have used npm install -g express.
then used npm install -g express-generator
but when after install , i type express-V still told me :-bash: express: command not found.
please help me how to figure out this problem
You Say "Bash" that mean that you are running under linux or something similar. If a command could not be found from the system, it is not in in the Path environment variable. Check your NPM installation regarding environment variables. You need to include the global modules folder of npm in your Path.
Thanks for your answer.i reset the path environment and then it has been figured out.

Using npm to install packages gives command not found?

Using npm to install packages, I can never use their cli. For instance, install grunt-cli, avn, nvm, etc. and running
npm install -g avn avn-nvm avn-n
avn setup
Results in command "avn" not found. I don't know if this is a pathing issue, but my PATH is:
/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/texbin:/usr/local/sbin:/usr/local/opt/ruby/bin:/usr/local/lib/python2.7/site-packages:/usr/local/share/npm/bin
I am using OSX.
Your path does not include where the node_modules you're installing globally exist. You need to include the bin path for the modules:
export PATH=$PATH:/usr/lib/node_modules/.bin

How to know if a module is installed in node

I know that I can install a module in node.js using npm install module-name. but try that multiiple times and it will install again. I am tired of forgetting that I have installed modules globally and I am installing them again. How do I know if a module installation already exists globally in node?
you have to try
npm ls
will give you the list of installed modules
to list global packages
npm ls -g
to list global packages with more detail
npm ls -gl
same way to list local packages with detail
npm ls -l
also you can type
npm help ls
for more details regarding this
in your js code, just do this:
try {
console.log(require.resolve("some-npm-module-name"));
} catch(e) {
console.error('moduel is not installed');
process.exit(e.code);
}
If you want to know whether you have a specific module installed, you can run npm explore <module>. If you get an error, then you don't have that module and you should download it.
This is a bit of a hack, but it works.

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