Why do I keep getting "Cannot find module 'socket.io'"? - javascript

I have been trying to use socket.io in my NodeJS script, but I'm constantly getting the error "Cannot find module 'socket.io'".
Full error:
$ sudo node /var/www/apache/server/serverScript.js
module.js:549
throw err;
^
Error: Cannot find module 'socket.io'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/www/apache/server/serverScript.js:59:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
I've tried installing socket.io globally, I've updated everything npm related, and tried all other common suggestions I found online, but the error hasn't changed.
If I had to guess, I think it's related to the package.json file. I started off with NodeJS just recently, and never used a package.json file. I saw somebody mention it as a fix, so I added one through npm init. This placed it in /home/pi. Since this didn't help, I moved the file to the js file location: /var/www/apache/server. Unfortunately, no luck.
Can anyone tell me what's causing this issue?

It's hard to give a good answer without any of your source code. However, you can try do to this.
Open your cmd or terminal.
cd into your project folder.
Run the command: npm i --save socket.io
Within your source code, type const io = require('socket.io'); to import socket.io.

Have you place this code at the bottom of your render document (like index.html for example) ?
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
You can just install socket IO inside your project not globally.

If you are developing in NodeJS, you need to add the package library to your package.json. This can be done by doing:
npm install --save socket.io
Note: If your npm install fails to install socket.io, try to:
Install node-gyp globally: npm install -g node-gyp
Create a .npmrc file with this in the content: #types:registry=https://registry.npmjs.org/
which is going to add this line to your package.json:
"dependencies": {
"debug": "~3.1.0",
...
"socket.io-adapter": "~1.1.0",
"socket.io-client": "2.1.1",
"socket.io-parser": "~3.2.0"
},
Note: the dots are other packages library that may exists in there.
Then, you can use it in your code:
const io = require('socket.io');

Related

npm ReferenceError: primordials is not defined (node.js)

I know this question is posted many times but I tried the solutions and I can not resolve it. I am trying to work wit npm however I get this error:
evalmachine.<anonymous>:35
} = primordials;
^
ReferenceError: primordials is not defined
at evalmachine.<anonymous>:35:5
at Object.<anonymous> (C:\Users\User\AppData\Roaming\npm\node_modules\npm\node_modules\graceful-fs\fs.js:11:8)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\User\AppData\Roaming\npm\node_modules\npm\node_modules\graceful-fs\graceful-fs.js:3:27)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
I can not even execute npm -v to see the version.
I tried to downgrade node to version 10.
I tried to uninstall-reinstall node so many times (From remove application settings).
I tried to create a file called "npm-shrinkwrap.json" insde the npm folder where package.json exists.
But there is no use.
And I can't see an "AppData" folder in the "Users/User" path but I can access it by "cd" from the command line. I don't understand how it is not visible.
Thank you.
It is resolved!
I know I said I uninstalled npm, but just uninstalling it from the settings were not enough. I tried to uninstall it deleting the node releted directories on my computer following this post: https://stackoverflow.com/a/11178106/13806114. And then I installed node again and it worked!
Thank you.
It's looking for the "primordials" method in graceful-fs, trying to reinstall this dependency.
About npm, try reinstalling nodejs again, probably your npm is corrupted
About AppData is not visible or is a hidden directory in Windows, you can access it by path in Windows Explorer

Cannot find module "serve-static" | JavaScript | node js

I want to create a simple server.js file in node.js which will act as a
local server.
I installed the connect module by the following command
npm install -g connect
And I can see the connect module inside node_modules
Then I tried to install serve-static module as follows
npm install -g serve-static
It says packages added but I can't see the module inside node_modules.
And when I try to run the following server.js it throws an exception saying
Cannot find module serve-static
server.js
var connect = require('connect'),
serveStatic = require('serve-static');
var app = connect();
app.use(serveStatic("../angularjs"));
app.listen(5000);
Error
Error: Cannot find module 'serve-static'
at Function.Module._resolveFilename (module.js:555:15)
at Function.Module._load (module.js:482:25)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Program Files\nodejs\server.js:2:16)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
1- Install them locally
npm install connect -S
npm install serve-static -S
2- OR Link them into your folder
npm link connect
npm link serve-static
3- OR Make sure that if the environment variable NODE_PATH is set correctly

Express.js Cannot find module 'mime-types'

I'm trying to run a simple express server with body-parser:
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
I realize this is no longer bundled with express and have double checked both packages are installed with:
npm install -s express body-parser
The error I get back is:
module.js:472
throw err;
^
Error: Cannot find module 'mime-types'
at Function.Module._resolveFilename (module.js:470:15)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/home/osboxes/.nvm/versions/node/v7.2.0/lib/node_modules/body-parser/node_modules/type-is/index.js:16:12)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
I've also tried:
npm install -s mime-types
which had no effect.
Note: I verified that line 4 is where the error occurs by logging before and after.
Ensure body-parser and express are listed as dependencies in your package.json, then delete your whole node_modules directory and do a clean install of your dependencies using npm i.
It looks like mime-types is a dependency of the type-is which body-parser has listed as a dependency, make sure that all of those are installed properly and if not install them. You shouldn't need to a --save flag when installing mime-types because you don't need to make it a dependency of your package since you're already listing body-parser as a dependency, if anything that could cause another problem because you may have a differing version requirement from that of body-parser/type-is.
Also, I don't believe the save flag for npm is -s it is either --save or -S.
npm install takes 3 exclusive, optional flags which save or update the package version in your main package.json:
-S, --save: Package will appear in your dependencies.
-D, --save-dev: Package will appear in your devDependencies.
-O, --save-optional: Package will appear in your optionalDependencies.

npm ERR! extraneous: pug#2.0.0-alpha6

I need some help. I am just learning to build a website with node.js.
I have installed node v4.5.0, npm 2.15.9, coffeescript 1.10.0
I am following instructions from a book "CoffeeScript and Node.js".
I have this file package.json:
{
"name": "todo",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "3.0.0beta6",
"jade": "*",
"socket.io": "*",
"coffee-script": "*",
"connect-assets": "*"
}
}
I ran the command: npm install, after which some warnings were shown, about jade being deprecated, and that pug had to be installed.
I have changed jade to pug in the package.json file, and ran npm install again. And again there were some warnings, about newer versions of pug! Made some changes again, at last this ERR appeared!
I would appreciate some help, how to go on.
Note: the example code is from quite an old book (2012), and never updated
How should I proceed? I have a directory tree with both jade and pug. Will that work? Or should I update node and/or npm?
EDIT: I got most of it working now, but another issue came up. Should it be related to the previous ones?
The issue: I installed another helper module: npm install supervisor -g. When I run supervisor app.coffee, at first everything looks right, but after making changes in one of the files, the console display goes wild. I have to stop it, and this is what came out:
Starting child process with 'coffee.cmd app.coffee'
Listening on port 3000
events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE :::3000
at Object.exports._errnoException (util.js:907:11)
at exports._exceptionWithHostPort (util.js:930:20)
at Server._listen2 (net.js:1250:14)
at listen (net.js:1286:10)
at Server.listen (net.js:1382:5)
at Function.app.listen (C:\Examples\todo_node\todo\node_modules\express\lib\application.js:533:24)
at Object.<anonymous> (C:\Examples\todo_node\todo\app.coffee:13:5)
at Object.<anonymous> (C:\Examples\todo_node\todo\app.coffee:1:1)
at Module._compile (module.js:409:26)
at Object.exports.run (C:\Users\ad\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\coffee-script.js:134:23)
at compileScript (C:\Users\ad\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:224:29)
at compilePath (C:\Users\ad\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:174:14)
at Object.exports.run (C:\Users\ad\AppData\Roaming\npm\node_modules\coffee-script\lib\coffee-script\command.js:98:20)
at Object.<anonymous> (C:\Users\ad\AppData\Roaming\npm\node_modules\coffee-script\bin\coffee:7:41)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3
Program coffee.cmd app.coffee exited with code 1
Starting child process with 'coffee.cmd app.coffee'
Received SIGINT, killing child process...
Parent process exiting, terminating child...
Second EDIT; I have read that Error: listen EADDRINUSE :::3000 is indicating that the 3000 address is in use. I have no idea what might be using it, but rather I'd like to know how this can be avoided. I am running this app on Windows 10, that just had an update today.
And, moreover, I have no problems running the app with the command coffee app.coffee. Drawback is that this has to be closed and started again each time a file is changed.
I hope someone can help me further,
Thank you,
Ad
Jade is the old name of the Pug templating engine.
Use Pug, latest version is 2.0.0-b6 :
Pug page on NPM
You can try it on the page, in your browser.
If NPM fails, try GitHub repository
If you aren't comfortable with versions, just use :
npm install pug --save
To automatically add Pug into your package.json (with the correct, latest version).
Edit : which warnings/errors do you have ? Can you post it please ?
Edit 2 : your Express version is outdated too.

GithubElectron: A JavaScript error occured in the browser process

I installed "electron-packager" using npm and executed the command
electron-packager . Johnston --platform=darwin --arch=x64 --version=0.36.0
And then I when I run the "Johnston.app", this error showed up (I didn't even see the main window)
Uncaught Exception: Error: Cannot find module 'electron'
at Function.Module._resolveFilename (module.js:332:15)
at Function.Module._load (module.js:282:25)
at Module.require (module.js:361:17)
at require (module.js:380:17)
at Object. (/Users/David/Github/Johnston/Johnston-darwin-x64/Johnston.app/Contents/Resources/app/main.js:3:18)
at Module._compile (module.js:426:26)
at Object.Module._extensions..js (module.js:444:10)
at Module.load (module.js:351:32)
at Function.Module._load (module.js:306:12)
at Object. (/Users/David/Github/Johnston/Johnston-darwin-x64/Johnston.app/Contents/Resources/atom.asar/browser/lib/init.js:104:10)
Is there any solutions? Thanks!
This likely has something to do with you having a different version of Electron installed on your machine that you are using to build with. In the folder for your app, run electron --version to get the version of electron that you have installed. Then, change the --version=0.36.0 to use whatever version you have installed.
If that doesn't fix it, then you should try rm -rf node_modules && npm install on your project. This will remove you node modules and then reinstall them all. If that doesn't work, please comment here and I will try to help again.

Categories