Not able to request global modules - node.js

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?

Related

one dedicated directory for node_modules

I am quite new to npm but I have a question about npm node modules. I have the node modules now installed in several directories. One for Gulp, one for nodeJS etc. I was wondering how I could have one dedicated node modules directory and let the other packages point to that directory, instead of having node modules all over the place.
What is best practise?
Cheers,
Lino
Yes, you can have global node_modules directory accessible to multiple application.
A package is available globally if it is installed with -g option. For instance if body-parser is required globally it should be installed with the following npm command:
npm install body-parser -g
Following are the paths are usage of modules:
Local install (default): puts stuff in ./node_modules of the current
package root.
Global install (with -g): puts stuff in /usr/local or wherever
node is installed.
Install it locally if you're going to require() it.
Install it globally if you're going to run it on the command line.
If you need both, then install it in both places, or use
npm link.

npm list is not pointing to the node_modules folder

I have recently install nvm, node and npm. I cant seem to get the $npm list to list the modules in the node_modules folder. Other npm commands work. npm install -g installs in the correct folder which I can see by cd'ing to it. I cant figure out why one npm command works but the next doesnt. npm config shows cwd as a different folder. Do I have to change that? If so, how do i do that? Thank you.
https://gist.github.com/kaona/0d5ba467cff814dbb1691a083baa1dee
I also tried this but didnt work. I know its something simple im missing. Still new to this. Thanks.
Have you included the modules in your package.json file? The npm list command won't show a module if its not in the package.json, even if its installed in the node_modules directory.
The tree shown is the logical dependency tree, based on package dependencies, not the physical layout of your node_modules folder.
https://docs.npmjs.com/cli/ls
When you install a module into your app, use the --save or --save-dev options to update your package.json. This allows you to install all the dependencies of your application by running npm install. It will also mean that your modules show up when you use the npm list command.
https://docs.npmjs.com/cli/install - For more details

Homebrew Install Node Location

Kinda 2 questions related to each other here.
When doing a brew install node, should I first navigate to the root of whatever folder I'm going to hold all my future web projects/apps then run it? Or does it not matter where I run the install initially for Node? Because I notice it creates a node_modules folder in /local/lib/node_modules
I assume it doesn't matter, and when you start installing node packages using npm install [package] it'll create a separate node_modules folder under the context you're in so lets say /www/MyApplication run npm install and it'll create /www/MyApplication/node_modules....and that the one under /local/lib/node_modules just serves as the one for npm itself because it needs its own root node_modules folder which is how npm runs?
Correct, it makes no difference where you run brew install node, it will install into your Homebrew folder.
When you use npm to install a Node module, it will install into the current directory, unless you use the -g global flag. Normally you will install modules that are project dependencies into your project folder, and global modules are for global utilities.
For example, to use Grunt you would install the grunt-cli package globally for the command-line utility.
npm install -g grunt-cli
And for each project that uses Grunt you will install a version of the grunt module for use with the project.
npm install grunt

Node Version Manager (NVM) npm installing modules to common folder

I've installed NVM for node.js using the instructions from this post:
http://www.backdrifter.com/2011/02/18/using-nvm-and-npm-to-manage-node-js/
When I switch between node versions and then use npm to install a module, all the modules are placed in the same 'node_modules' folder (~/node_modules/) instead of in the 'node_modules' directory specific to that version of node?
Any idea on how to remedy this?
Based on the comments from https://github.com/creationix/nvm/pull/97:
When installing packages with npm using the global switch -g the
package ends up in the proper directory (i.e.
.nvm/$VERSION/lib/node_modules), however node is unable to require it
since it somehow isn't searching on it's prefix.
So using npm install -g xxxxx will put the modules in the correct location for NVM but if you try to require one of them node can't find the module. I am still playing around with this and will update if I find a solution.
Update
Where does NPM put node_modules? (see https://docs.npmjs.com/files/folders)
Local install (default): puts stuff in ./node_modules of the current package root.
Global install (with -g): puts stuff in /usr/local or wherever node is installed.
Install it locally if you're going to require() it.
Install it globally if you're going to run it on the command line.
If you need both, then install it in both places, or use npm link.
So what I did was run npm init (see http://npmjs.org/doc/init.html) in my projects root dir which generated package.json. Now when I run npm install xxxxx it creates a node_modules dir in my project folder (which I add to my .gitignore). This works for modules that I require in my code.
For commands such as CoffeeScript I install with npm install -g coffee-script which puts it in the correct directory (.nvm/$VERSION/lib/node_modules). While I can't require these modules (npm link should solve this problem) I can run the commands - i.e. coffee.
I just installed express globally (-g) and was having problem when require("express"). Just like Jesse Vogt said I just reinstalled express but this time without the -g just like this: "sudo npm install express" and now is working perfectly!
For latest nvm window version 1.1.7.
Package was installed and placed into the respective nodejs version.
nvm use 16.8.0
npm install truffle
nvm use 16.7.0
npm install mysql

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