Error: Cannot find module 'socket.io' - node.js

[~]# node node.js
Error: Cannot find module 'socket.io'
[~]# node -v
v0.10.10
socket.io installed:
npm install socket.io
npm WARN package.json policyfile#0.0.4 No repository field.
npm WARN package.json policyfile#0.0.4 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field

Looks like you have installed socket.io in a different location to your current path. Either install globally like below:
npm install -g socket.io
Or reference the location you've installed to:
var io = require('../lib/socket.io');

Thanks ajtrichards!
Just to add to the answer - in case you simple use
sudo npm install socket.io
The installation path will be
/home/.../.npm/socket.io
If you use
sudo npm install -g socket.io
The installation path will be
/usr/local/lib/node_modules/socket.io
In first case, I tried adding the socket.io path in global path variable but it did not work.

you might have installed but not added to the dependencies in package.json
Use below command to install socket.io module
npm install socket.io --save
Hope this will resolve your problem..

I had the same issue with version 0.12.0 on Windows. I tried npm install -g socket.io but that didn't change anything. Also tried npm cache clean also no change, but after npm update npm -g, things got well.

I had this problem with NodeJs, Typescript and Socket.io 4. the error was:
TS2792: Cannot find module 'socket.io'.
So my fix was to update the tsconfig.json adding a new property (moduleResolution).
tsconfig.json:
{
"compilerOptions": {
....
"moduleResolution": "node",
}
}

This almost happens than you try to get socket.io in you html files like :
index.html
where you have:
< script type="text/javascript" src="/socket.io/socket.io.js"></script>
It will not find socket.io because you did not started module in you application file wich contain the server like
server.js
You must include following lines after started your server in server.js :
var io = require('socket.io').listen(server);
Hope, will save time.

I think that you have executed the command npm install socket.io
in a different location and your files are in different directory..
So either run the command in the same directory which have your files or either mention the path where you have currently installed socket.io in your PATH variable.

I had the same problem in mac... you can install the module with sudo npm install socket.io-client

Related

Cannot find module 'express' on nodejs.log

I have a react and nodejs project on the host 'O2Switch'.
My React seems to work fine, but there is a whitepage on my url https://www.desmettre-maxime-canin.com/ because of my node not respond. On localhost, it's good.
In my nodejs.log file, i have the error "Cannot find module 'express' on nodejs.log"
How to fix this please ??? The module is correctly installed on my node_modules, good in package.json etc.
I tried a million time to re-install express in a million ways...
Thank you
Hello first check again if you installed express via npm in root directory of your project.Some users had same problem and used this command and worked
npm install express --save
or install a specific express version
npm install express#4.18.1
Also try to remove node_modules and package.json file,clear npm cache ,maybe you did it many times and accidentaly you created errors.And after that try again to install npm depedencies
rm-rf node_modules package-lock.json
npm clean cache --force
npm init
Finally try to fix node_path environment variable,if your node_modules is in another directory
set NODE_PATH=your\directory\to\node_modules;%NODE_PATH%
I am not sure but installing many times express may caused this problem,you can check this by creating new project with clean install of npm packages.

Installing a package doesn’t show in my package.json file

I have a weird problem when I install a Node.js package:
npm i socket.io
It doesn't appear in my package.json file as a dependency, but it was installed and I can use it. The problem is, when someone will download it from GitHub, he/she needs to install socket.io manually instead from just npm i. How can I add it?
Use
npm i --save socket.io
The --save option will add the package to your dependencies.

Has npm install just removed all my files?

I was just following these commands:
Node.js Error: Cannot find module 'request'
npm init
npm install --save request
node app.js
And now when I go to my project directory, all my files are gone. Is there anyway to get them back???
TO clear your thoughts, following commands does not remove files.
npm init // Initialises the npm in your directory and creates package.json file.
npm install --save package_name // Install package in your directory in the node_module folder.
So, definitely, you have done something else, which has been causing directory removal or disappearance.
I would suggest you check trash.
Can you post history of commands you used? Maybe that would help me understand what happened exactly.

Node MODULE_NOT_FOUND

I just upgraded to node version 9.0.0 and am now getting this error in the command line when trying to use npm install
npm ERR! code MODULE_NOT_FOUND
npm ERR! Cannot find module 'internal/util/types'
I'm using:
OSX 10.10.5
Node version 9.0.0
NPM version 5.5.1
Extra information: I am also trying to do this with a Laravel 5.5 project. This is how I update my version of node: How do I update Node.js?
run
rm -rf /usr/local/lib/node_modules/npm
and then re-install Node.js will work in most cases
Leaving this here for anyone using the n nodejs version manager:
$ n 6.12.0 # Go back to a stable release
$ npm install -g npm#latest # Update npm to latest
$ n lts # Get 8.9.1
$ npm install #Should work now.
The MODULE_NOT_FOUND error seems to happen when changing between node versions and some files are possibly still being cached. I am not sure exactly but the above sequence of commands work for me.
When I first got this, I solved just running "npm install" again to make sure everything was installed.
I got similar error also on Windows 8 after I have just upgraded node js. First: how I ran into the issue then the solution that worked for me.
How I ran to the issue:
When I did npm --version and node --version I discovered that I wass running npm v3.x and node 5.x. So I went to nodejs.org site from where I downloaded node-v8.11.3-x64.msi. After installing the msi package I confirmed that my nodejs version was now v8.11.3 via node --version command.
Then, when I ran "npm install http-server" (w/o the quotes) that's when I got the issue:
npm ERR!
node v8.11.3
npm ERR! npm v3.5.3
npm ERR! code MODULE_NOT_FOUND
My resolution:
I did some research including on the internet and found out that the npm version pointed to in my path was the one in my roaming profile C:\Users[myname.hostname]\AppData\Roaming\npm. In other words, the npm being used is not the one in the updated package I have just installed which is located in C:\Program Files\nodejs.
The resolution was to delete npm and npm-cache in the roaming folder. Note, I used cygwin as I was not able to delete these folders via Windows cmd prompt. With cygwin, I navigated to
cd "C:\Users[myname.hostname]\AppData\Roaming"
Then I removed the aforementioned folders like so
rm -rf npm-cache
rm -rf npm
After that, I opened a new Windows cmd prompt and was able to now successfully install http-server like so:
npm install http-server
Hope this works for you.
For me it was package installation issue, so I just write,
npm i or npm install in the root of the application.
to open the terminal in the root of the application, if you're using VS-code right click on the package.json and click on Open in integrated terminal.
I founded this problem too, so I found that I have imported wrong module instead of express module I had imported router module after I had replaced this two my code work as well
If all the above solutions doesn’t work check for any blank spaces in your folder/file where you copied the path
Make sure you are inside the project folder.
Rename the folder "node_modules" to any other name (for example: node_modules_old).
Run command: "npm i" (the command will build new the folder node_modules).
Try running your program again.
If the problem is resolved and your program is running correct, delete the old folder node_modules.
If you are using libraries make sure to install everything with npm or yarn before starting. And in cases of you files if you are going to use them make sure to do the export.module thing everytime.
If you are working with Local modules then don't have node_modules. All things go well in a easy way.
But if you want to work with both local and node_modules then use
.mjs (extension) - For modules
.cjs (extension) - For common scripts which you want to run with node
in which you can use require statements like
var http = require('http');
var fs = require('fs');
but if using .js extension then use
import http from "http"
import fs from "fs"
And also your package.json for type
Haa well, I have spent two days on this and have done everything I can to fix this issue even tried resetting the system but none of them reloved the issue.
And accidentally found out what was causing this issue, it is because of & in my parent folder name. File hierarchy R&D>remix>blog, When I was trying to run the blog server it was throwing module not found, require stack error.
code: ←[32m'MODULE_NOT_FOUND'←[39m,
requireStack: []
Solution: I have changed the parent folder name to RnD and it fixed the issue. If the file name contains any special characters(even parent folders) try updating it. In my case, it is &
The MODULE_NOT_FOUND error happened to me and even running npm install the error persisted.
Try to do this
For me, what worked was deleting the node_modules folder
rm -r -f node_modules/
After that, run the command to install the package.json dependencies
npm install
What happened to me was that when I ran npm install for the first time I had a very low internet connection and therefore I believe that the packages from package.json were not downloaded correctly and due to that the MODULE_NOT_FOUND error occurred. The funny thing is that just running the npm install command has no effect because it understands that the package is already there but it isn't. Similar as a corrupted data. In my case the npm update was without effect too.
If when you are using React And getting this error message. You can use this ,
NPM
npm install #reduxjs/toolkit
Yarn
yarn add #reduxjs/toolkit

npm publish node.js module and yet can not find module after npm install

thanks your time advance.
first, I publish npm package success.(verified that already in the npmjs.com profile, the package name I can call 'firstnpmpublish'(uniqueness))
second, write require('firstnpmpublish') in my test.js file and type npm install firstnpmpublish
third , type node test.js in the terminal on my mac.
but it print:
throw err;
^
Error: Cannot find module 'firstnpmpublish'
I have tried 'npm install firstnpmpublish' and 'npm install firstnpmpublish -g' and also 'npm install' as I create package.json which dependencise include firstnpmpublish. but it still doesn't work.
however, instead of require('firstnpmpublish') and using require('express'),it work smoothly.
where i was wrong? thanks
Your package.json file doesn't specify the path to your main file, so the require can't be resolved. Add the following to your package.json and then republish:
"main": "Print.js"
I'd recommend renaming that file to index.js though, to better fit with the Node conventions.
I had a very similar issue. Removing the entire node_modules folder and re-installing worked for me:
rm -rf node_modules
npm install

Resources