How to know if a module is installed in node - node.js

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.

Related

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

What does the -g option for npm install and npm list do?

The Node.js npm (Node Package Manager) has a -g command line argument, which I often see referenced. For example, the documentation for the Microsoft Azure x-plat (cross-platform) CLI tool says to install it by using npm install -g azure-cli.
Question: What does the -g option do?
What options do I have to install node modules?
After writing this I quickly found and old but still applicable post by Isaac (yes, the npm #isaacs). But I still think the below post is informational.
You can install npm modules globally or locally - you already know that, but why?
Globally: npm install -g some-module-a: This module is intended to be used as an executable (i.e. CLI, file watcher, code minifier, logger, etc.).
Locally: npm install some-module-b: To be imported and used in your app via import, var someModule = require('some-module)
global modules are one of the best ideas of npm. We can easily create executables using node/javascript. If your node app is meant to be run as an executable, then you will want others to install it globally. If it's a utility, helper, application, etc. then you usually don't want it installed globally. So, unless the module explicitly states that you should install it with -g, then don't.
One more time: if you are wanting to use some module called some-module in your node app - var someModule = require('some-module'), then npm install some-module from the root of your node app to pull it into your local node_modules directory. If you've installed some-module globally and not locally, it will usually not load and will show you an error about not finding the module (even though it can be made to load the global module - hint: just don't!)
So what exactly happens when you install globally?
npm install -g [some module] installs the specified node module in a directory higher up in your file system (i.e. usually /usr/local/lib/node_modules in unix systems). The biggest use case for global modules is for CLIs written using node (think npm, bower, gulp, grunt, et. al.).
Let's look at what happens when you install bower globally:
*follow these steps in your command line/terminal
step: npm install -g bower
explanation: the module - all of it's files and dependencies - are saved in your global directory (e.g. /usr/local/lib/node_modules/bower).
Something else happened here. Somehow you can now run bower in your command line. Awesome!
step: bower -v --> results in the installed bower version (i.e. 1.6.5)
explanation: It's now a fully executable node app using bower as the keyword. Inside bower's package.json file you'll find a bin property:
"bin": {
"bower": "bin/bower"
}
So how did that all work?
npm will create a symlink from where most executables live, /usr/local/bin/bower over to /usr/local/lib/node_modules/bower/bin/bower, where the module lives. That symlink makes it so when the executable runs, it can reference other files in the original module, including it's local node_modules. Pretty cool, huh?
*Note on executables: If you create a file called awesomeness in /usr/local/bin/ and chmod u+x (user + executable) it. Then write some scripting in it (in this case javascript using #!/usr/bin/env node at the top). Then you can run it anywhere in your command line/terminal just by typing awesomeness.
Hope that helped. I know doing a deeper dive into it helped me early on.
Node.js packages can be installed one of two ways:
Globally
Locally
The -g option instructs npm to install the package globally. You would install a Node.js package globally, if you want to be able to call the command directly from the terminal.
From the documentation:
There are two ways to install npm packages: locally or globally. You choose which kind of installation to use based on how you want to use the package.
If you want to use it as a command line tool, something like the grunt CLI, then you can want to install it globally. On the other hand, if you want to depend on the package from your own module using something like Node's require, then you want to install locally.
To download packages globally, you simply use the command npm install -g , e.g.:

Module async not found

I'm studying node.js
and I am making the exercises from a book
https://github.com/marcwan/LearningNodeJS/blob/master/Chapter05/05_series.js
I have a problem with an example in which you invoke the module async.js
when I go to run the example I get the error
"can not find module 'async'"
in the folder where you installed node
I checked that there is a module
I also downloaded this package
https://github.com/caolan/async
and launched the test file that works properly
the first question that you do, even if it seems correctly installed the module, there is a command to verify that a module is installed and that you can recall?
the second question is, why is this wrong example?
thanks
To install a package, use npm install package_name.
When that's done, you can easily require that package and use it in your application.
const package = require('package_name');
To install a package globally (so you don't have to install it in every project you create) add -g flag
npm install package_name -g
You should be using npm, not downloading packages from github manually: npm install async.
npm will install the module into a node_modules subdirectory of the directory that you run it in. That directory must be your examples folder, or an ancestor. See: https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders

npm install is missing modules

Before I can run gulp in my project I need to run npm install. This works except on my computer, because I get the following error:
Error: Cannot find module 'socket.io'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
...
I can fix this with
$> npm install socket.io
Now when I do the install command again I get
Error: Cannot find module 'di'
...
When I install di and run the install command again I get:
Error: Cannot find module 'log4js'
I think that this might continue for a very long time. Any suggestions what is going on here and how to fix this ?
I've faced the same problem when bootstrapping a MEAN application and add each missing dependencie with npm install packageName --save was not an option so I came across to npm-install-missing whom has saved my life :)
Installation
npm install -g npm-install-missing
Usage
npm-install-missing
Running npm install will install all dependencies that are specified in the package.json. Seems like you have quite a few dependencies that are not defined that way. Use npm install packageName --save and npm will add the package to your package.json.
I am using the same version of npm/node. Sometimes, it is like npm is "lost". What I suggest is :
rm of your node modules (at least the one that is concerned)
npm cache clean
run "npm install" several times, until all dependencies are resolved and no message are displayed
It seems that gulp need 'karma' dependencies (socket.io ,di ,log4js...) so you will have to run :
npm install karma
so just runing this command solved the problem, and all should be good, the same thing happens with grunt as well for some reasons.
This worked for me. By commenting 3 lines in C:\Program Files\nodejs\node_modules\npm\node_modules\graceful-fs\polyfills.js.
Refer [https://flaviocopes.com/cb-apply-not-a-function/]
// fs.stat = statFix(fs.stat) # Line: 61
// fs.fstat = statFix(fs.fstat) # Line: 62
// fs.lstat = statFix(fs.lstat) # Line: 63
Beside other answers, if you are using Angular and cannot make new angular project and hangs, you can get into the folder and open terminal and write:
npm -i
Maybe useful for other things too!!
I think the npm module madge would help you find the missing dependencies. It goes through your actual code and makes a list of all the dependencies found within. You could then do an npm i for each of the modules found.
If the npm-install-missing does not work for you, knowing the name of the Packages that are missing will help you out here. All I had to do was first open my package.json file inside VSCode, then paste or type the names of the missing modules into it (under dependencies) according to the way other package names were written there.
Then I ran npm install after that.
This method is helpful when you are working on a file but somehow you did not get the package.json file or some of the modules are not listed therein.
Remember to stop and restart a running server after you do npm install for your new dependencies to reflect on your work.
Upgrade your npm version
Install nvm which is easier to switch node js versions
For me node 12.18.3 worked
Just do: nvm use 12.18.3 to switch version
run npm install again and node_modules will appear
To resolve missing npm modules run:
sudo npm install -g npm-install-missing

NPM appears to do nothing on Linux Mint 15

searched, and did not see this specific problem.
Trying to get a MEAN stack built on my Linux Mint machine, and bumping into a bit of an unusual issue.
Got MongoDB installed, and finally got it running correctly (none of the instructions ANYWHERE mentioned having to create the /data/db/ directory and set permissions, go figure).... it works now.
Got NodeJS installed, and it appears to work correctly.
I had been told (apparently incorrectly) that NPM installs right alongside Node, with:
sudo apt-get install nodejs
but:
$ npm
bash: /usr/bin/npm: No such file or directory
So I go ahead and install NPM separately.
$ sudo apt-get install npm
Seems to work, so far, no errors, and it looks like it is pulling down the NPM package and installing it...
$ nodejs -v
v0.10.21
$ npm -v
$
?? It simply fails to respond without any error... so I try:
$ npm install grunt -g --save-dev
$
Same completely silent failure... in fact, NOTHING I could do gets a response out of NPM.
Looked all over the web, and saw nothing similar anywhere... found out that NPM holds its cache files in ~/.npm and noticed that this folder didn't exist (kinda like the mongo issue above), so I created it, and set permissions to 7777... still nothing.
Purged and re-installed both node and npm, tried installing them both together and separately (yes, desperation)... still no love.
WTF am I doing wrong?
I would love, eventually, to have a nice development environment setup, hopefully with Cloud9 as a local IDE.... but already pulling my hair out.
=========================================================================================
OK, after a few more headaches, this is up and built now... thank you all.
Would love to mark both as answers, but it won't let me.
npm does come with node. Where is apt-get pulling it from? I install the Mac OS X packages on my Mac for development and npm does in fact come with it. I compile from source on my CentOS server and npm comes with it.
Your package provider may be providing them separately as a (in)convenience to you.
As for why your npm command does not work after installation, I can't say for sure, but I am suspicious of your use of --save-dev and -g together.
-g means to install globally, which means 2 things:
It will be installed outside of your npm package's structure into a system location like /usr/bin or /usr/local/bin
It requires root access to install. Did you use sudo to run it with root access?
Both of those requirements conflict with --save-dev which records the package as a dependency in your package.json file so that future npm install commands will install that package within the project space.
That said, I happen to know a lot about grunt. It has 2 parts, a globally installed tool and the package-specific tool. The correct way to install it is:
$ [sudo] npm install -g grunt-cli
$ npm install grunt --save-dev
This will install the grunt-cli package into a system location guaranteed to be in the $PATH, which turns around and looks for a package-specific grunt installed which is not system-wide.
When installing nodejs with npm, this one-liner worked for me.
sudo apt-get install nodejs nodejs-dev npm
I don't believe npm comes with the nodejs installation in the apt repo. Try to install npm separately using the following tutorial http://www.giantflyingsaucer.com/blog/?p=1688 'To install NPM ....'
====== Edit ====
node and npm IS separate. Follow the official wiki and everything should be fine

Resources