Unable to access socket.io module from node console - node.js

Please check this image here, as I can't display it here directly. I have installed socket.io module globally and even i have tried it installing locally inside PushNotification directory. I am still getting the error "Error: Cannot find module 'socket.io'".

don't use socket.io in global npm (-g)
install it locally in your project
npm -install socket.io --save

Node has no idea where to find socket.io when you install it globally. Global module installs are really only for command line level tools, that have to be linked to your PATH, so that you can call them from outside the node env. The most obvious of which is npm e.g. npm install.
When you installed socket.io globally, you ended up putting it in /usr/local/lib, which is not in the list of locations that node looks for module requires. Node looks in a few locations when it encounters a require statement:
Node core module
if path, look in project path
else look in node_modules path
None of these locations match /usr/local/lib, so node throws a "not found" error.
Read more about require here.
To answer your other question, if you install socket.io in your node_modules folder, and then in the node command line require('socket.io'), it will end up looking in your node_modules folder and finding socket.io, so you can easily use it from inside the node command line.

Related

How does Node.js know that I have a certain package installed?

I've mostly been using node in a text editor. But I was playing around with Node in the terminal today, and I typed: let Web3 = require('web3'); which is a package. I was not expecting this to work, but it did. How does node know that I have this package...did I install it globally before?
And what "environment" am I in when I run node on the terminal? Is it my current working directory? (I'm asking because when I run npm install [package] I know that that package is installed locally in my project directory, but not elsewhere)
You can install packages globally(system wide). These are available system wide. So you may have installed web3 globally.
To install globally you need to use -g flag like this
npm install -g web3
And what "environment" am I in when I run node on the terminal? Is it my current working directory?
Yes node starts where you run node. Module resolution depends on the start location as well.
Module resolution algorithm in detail:
https://nodejs.org/dist/latest-v15.x/docs/api/modules.html#modules_all_together
Directories with a package.json files treated by npm as a project folder. So, that means three things:
Your environment is not a working directory, but the one with a package.json file in it.
Calling npm install [package] will install this package in a node_modules folder on the same level as package.json file.
Node has a module resolution system which will look for required package in closest node_modules folder and some other places.
If you are interested in how it works in more details here is a nice article

Node.js: module not found after installing it with npm -g

I have installed the tar-module using
npm install -g tar
When I type
npm list -g --depth=0
I can see the entry tar#6.1.0 in the module-tree, however when I try to require it in a js-file const tar = require("tar"), I get the error message
Uncaught Exception:
Error: Cannot find module 'tar'
What am I missing?
The issue here is that you're trying to use something installed globally in a local project. You should be able to use your libraries if you install them inside the project with npm i tar.
The reason we install something globally is for use during development on many projects. This way, we don't have to install a tool on every project. With something you want to use inside a projects code however, you should install it on a project level. This way everything that the project needs to work lives inside of the project itself. You should see all dependencies listed inside of your package.json file
Not gonna advocate you do this, but if you really want to include the globally installed library, you can do something like this:
require('./../../.npm-global/lib/node_modules/tar'); // Relative path to library
Where you go up the file directory to your $HOME directory into the default global install location for node and bring it in. This is poor practice, please don't do it, but heres the info none-the-less.

NPM on OSX, error getting global installed packages

I have nodejs installed on a MBP which runs OSX 10.9, I have installed as a package downloaded from the nodejs website. Then I have installed the MEAN stack following instructions on mean.io.
The commands are:
sudo npm install -g mean-cli
mean init yourNewApp
That works correctly
Now the real issue is when after my app is created I enter the dir using the terminal, and write gulp, and it thows me some errors that some mandatory modules are not found.
The modules are written in the package.json file that mean generated, and they are installed as global modules on ~/.npm
I browsed the folder and there are all the required packages folders, inside the folders there is a package.tgz file which has the code of the package and a package folder which holds a package.json file describing the package itself.
Now I don't understand why the packages are compressed and why if they are installed globaly can not be accessed from gulp on my project folder.
Thanks in advance.
If you install some global module then you better don't put it into the package.json of your app because when you run your app that's the first place where is going to search and if it is there your app is going to look at node_modules folder and if it is not there your app will crash.
My advice is try to install your modules inside your app, npm install your_module --save because your app is gonna be portable and with a simple npm install you will be able to install all your needed packages.
But if you still wanna install global packages you maybe wanna follow this rules:
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
If you have the time to read the link then you will see that there are exceptions and how to handle them.

Can't get Socket.IO to install on Ubuntu VPS server?

I am having trouble to get socket.io installed in the correct directory I guess for it to work with my website. I had it working at one point, however I had to install socketIO in the directory with my website files (so that was cluttered and couldn't be there, i deleted those files and now it no longer works).
My website files are located in: /var/www/
Node.js seems to work fine globally as when I type, node -v anywhere it gives me the version.
I have tried sudo npm install socket.io -g but that didn't fix the issue. Where am I suppose to install socket.io, or how am I suppose to in order to make it work?
Possible duplicate:
Getting errors when trying to install socket.io globally
You have to install npm packages in the project folder where your package.json and node_modules folder exist if doing locally. For global installation you must have NODE_PATH variable containing your global package folder, which should be /usr/local/lib/node_modules

NPM basics with nave and Node.js

I recently installed node.js and was told that express was the way to go for routing and getting set up with web application development.
I installed the latest version of node which apparently is incompatible with the latest express.
I looked up and found nave... Like RVM, nave allows you to switch versions of node. So I ran nave.sh install 0.4.11... That worked successfully and I was able to run.
npm install express -g
This I thought, should install express globally. So I run:
express testapp
which creates
create : testapp
create : testapp/package.json
create : testapp/app.js
create : testapp/public/stylesheets
create : testapp/public/stylesheets/style.css
create : testapp/public/images
create : testapp/public/javascripts
create : testapp/views
create : testapp/views/layout.jade
create : testapp/views/index.jade
Then I
cd testapp/
node app.js
I get
Error: Cannot find module 'express'
Is this usual behavior?
Since express is in packages.json, if I run npm install -d, it will create a node_modules directory in my application and not just symlink to the node_modules in my node path.
In a word, yes, this is the usual behavior.
When you install packages using NPM with -g option, it installs it globally, which does nice things like putting executeables on your path (i.e. the express script you used)
However, it does NOT put those packages anywhere that node can find them.
To install it so node can find the package, you must also do
cd "your express app"
npm install express
which installs locally (to the node_modules folder in the root of your application dir).
This is primarily to avoid any dependencies conflicts, and though it may seem silly, it is in fact really useful.
If you have some real reason to want to use your global install (say for example you have many applications that you want to make sure always share the same version) you can use the npm link command.
For a good rundown of NPM and global vs local see this blog post.
If you are on Windows, add location to your path.
export NODE_PATH="C:\Users\IMarek\AppData\Roaming\npm\node_modules"
Change: IMarek to your user name.

Resources