cannot find module socket.io - node.js

I'm pretty new to nodejs and socket.io. At the time I don't know anything about nodejs. I met a horrible error.
My whole steps to install nodejs (on Windows 7):
- Access nodejs.org and click to their green download button.
- Install nodejs in F:/nodejs
- Install socket.io with command npm install socket.io
- Install node supervisor with command npm install supervisor -g
That's all. Then I try to use socket.io with this line:
var io = require('socket.io').listen(app);
And it output an 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)
at require (module.js:380:17)
at Object.<anonymous> (F:\nodejs\chat.js:8:10)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
I've tried all suggested method like cleaning cache, reinstalling nodejs or changing socket.io module path. Unfortunately, all of those solutions didn't solve this problem.
This terrible error killed my whole day. I greatly appreciate any help.

Try to execute npm install socket.io in the folder where your node.js file is (where you require the module). npm creates a folder node_modules wherever you execute npm install socket.io and you probably didn't do this in the correct folder.
npm install supervisor -g did work because -g is the global flag and you can basically do that anywhere.

The problem is that the package installed by npm install socket.io is not only the module itself, but also brings a sample and more stuff.
When you install, npm creates a node_modules folder.
Inside that folder, there is a folder named socketio .
Inside that one, there is another folder named node_modules .
Inside this one, there is a folder named socket.io .
Move or copy this one folder (socket.io) to inside the first node_modules folder right where your script is. Et voilla, it works!

Related

Unable to start React app via Termux (`Package exports for ... do not define a valid '.' target`)

I am unable to successfully run npm start on an application I've created via create-react-app on Termux; I have not done any modifications from the base application beyond running rm -rf node-module followed by npm install in an unsuccessful bid to get it to work, I also tried npm install autoprefixer#9.8.0 to no avail. This is the error dump:
Error: Package exports for '/data/data/com.termux/files/home/…/node_modules/postcss-safe-parser/node_modules/postcss' do not define a valid '.' target
at resolveExportsTarget (internal/modules/cjs/loader.js:455:13)
at resolveExports (internal/modules/cjs/loader.js:388:16)
at Function.Module._findPath (internal/modules/cjs/loader.js:487:20)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:782:27)
at Function.Module._load (internal/modules/cjs/loader.js:688:27)
at Module.require (internal/modules/cjs/loader.js:850:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/data/data/com.termux/files/home/…/node_modules/postcss-safe-parser/lib/safe-parse.js:1:17)
at Module._compile (internal/modules/cjs/loader.js:958:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:994:10) {
code: 'MODULE_NOT_FOUND'
}
…
I'm completely new to React, so any solutions would be appreciated. Thank you.
Well, it turns out the solution is to update the packages (presumably at least Node, although simply entering node install broke the installation) via pkg update; no more error.

Error running npm command on Windows: graceful-fs

I have a ReactJS project and I was trying to use some of the PrimeReact components. At some point, I faced an error (can't remember exactly which one) and I tried to run an older version of NPM in a copy of my main directory.
Now, I get this error every time, even when I run npm command.
I've already tried to delete my node_modules folder, delete my project and clone it back from GitHub and also to reinstall NodeJS several times. I don't know what to do!
I have Windows 10 and I'm using NodeJS v12.16.0.
Thanks to anyone that offers some help
Update: I have never installed gulp. I suppose that maybe some dependency is using it but, because I can't run any npm command, I can't remove them D:
The error:
C:\Users\Carlos>npm
evalmachine.<anonymous>:35
} = primordials;
^
ReferenceError: primordials is not defined
at evalmachine.<anonymous>:35:5
at Object.<anonymous> (C:\Users\Carlos\AppData\Roaming\npm\node_modules\npm\node_modules\npm-registry-client\node_modules\graceful-fs\fs.js:12:1)
at Module._compile (internal/modules/cjs/loader.js:1157:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
at Module.load (internal/modules/cjs/loader.js:1001:32)
at Function.Module._load (internal/modules/cjs/loader.js:900:14)
at Module.require (internal/modules/cjs/loader.js:1043:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (C:\Users\Carlos\AppData\Roaming\npm\node_modules\npm\node_modules\npm-registry-client\node_modules\graceful-fs\graceful-fs.js:3:27)
at Module._compile (internal/modules/cjs/loader.js:1157:30)
It might be an error related with gulp and node v12 conflict. If you have the gulp installed globally in your environment and you can uninstall without problems, you can try it. To help with commands, check if you have the gulp installed, typing this in your command line interface:
gulp -v
Uninstall your version:
npm uninstall -g gulp

cannot find module 'config-chain'

This is my first post, so forgive me for formatting issues. I was following this mean.io tutorial: https://www.youtube.com/watch?v=0aMNWYpG5sk. It basically gets you to the point where you can locally access the default web app. I was able to do so successfully.The video then suggests to run>
mean package myTheme
I received this error...
C:\Users\****\myApp>mean package myTheme
module.js:339
throw err;
^
Error: Cannot find module 'config-chain'
at Function.Module._resolveFilename (module.js:337:15)
at Function.Module._load (module.js:287:25)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.<anonymous> (C:\Users\****\AppData\Roaming\npm\node_modules\mean-cli\node_modules\npm\lib\config\core.js:2:10)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
I'm running npm 2.14.7 and node v4.2.3(video uses 4.2.2) on Win 10. I had previously tried this on node 5.3 and received the same error. I also tried cleaning cache & updating npm, bower, and gulp.
I found C:\Users****\myApp\node_modules\config-chain. Do I need to change something here?
Any help would be greatly appreciated. If I need to provide more information, just ask. Thanks!
I guess something was wrong with mean-cli.
I reran:
npm install -g mean-cli
npm install (inside project folder)
My issue was resolved. Hope this helps someone else.
I just removed and re-installed and everything worked. Go figure. I also had to use sudo because I'm on a mac.
sudo npm install -g ember-cli#2.5
The problem is when you install config-chain it installs in your project directory node_modules but cordova add browser is looking in global modules. This is what I did, went outside the directory and ran npm install -g config-chain That resolved this module and asked for more do the same thing for all modules it complains and you should be good.

Do I need to install express in every project directory?

I have centOS: When I try to run an node.js app that requires express, I get the following error:
module.js:340
throw err;
^
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/www/tipsterPro/index.js:2:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
I verified that express is in the node_modules directory:
root#myServer [/usr/local/lib/node_modules]# ls
./ ../ express/ express-generator/ forever/ n/ npm/ pm2/ supervisor/
I found a couple of postings stating to install express in the top directory of the node.js project. Is there a way to not have to install express in every project I have? There should be a way to point it to the install directory.
Additional info:
I installed node and express globally.
I also installed express-generator using: npm install -g express-generator.
BTW: node modules are installed in this location (not sure if correct): /usr/local/lib/node_modules
I ended up installing locally, after some reading about pros and cons of express local versus global installation it might be better if I install express locally. Once I installed locally it ran successfully.
The node module lookup algorithm looks like this (from Node.js in Action):
So if you have the module installed in node_modules at the root directory of your project, it will be found in every subdirectory and file. As you can see at the last point of the diagram, you can specify a search directory by setting the NODE_MODULES environment variable. When you globally install express it may be located somewhere like /usr/local/bin/express. You could set the NODE_MODULES directory when launching your app using
NODE_MODULES=/usr/local/bin/ node app.js
As you have guessed, there is indeed a solution to your issue.
You need to install express globally. That is instead of running npm install express you run:
sudo npm install -g express
The sudo is needed to get write access to the global location.
Flag -g stands for global installation. You can use it on any module.
This is not panacea though, some projects might require very specific versions, thus having a single install is not an option. For such projects you still have to install express locally for the project. But in your case, I believe having a global install is sufficient.
Node searches upward in the directory hierarchy for packages.
An absurd answer is to do this
# cd /
# npm install express
That will set up /node_modules ...

Installing project's dependencies globally and running the project without `node_modules/` — how?

I decided to try learning DerbyJS and this is my first acquaintance with NodeJS either.
I create a new Node/Derby project with derby new foo. This also creates a node_modules/ folder which contains a copy of all packages the project depends on.
The node_modules/ subdir of a blank Derby project is 144 MB large and contains 12967 files (sic!). As a person familiar with Ruby's RubyGems, RVM and Bundler, i find this insane. I can't express how wrong it is (actually i've got some solid argumentation against that craziness but StackOverflow is not a place for debate).
I thought that npm's -g flag would help me. I could install all packages globally, i told myself. So i did:
derby new -n foo
cd foo
sudo npm install -g
Now my project weighs 152 KB and contains 24 files. Now that's reasonable.
But i fail to run it. When i do npm start, i get "Cannot find module 'express'":
lolmaus#sandy:~/hello_derby2$ npm start
> hello_derby2#0.0.0 start /home/lolmaus/hello_derby2
> node server.js
Master pid 29884
module.js:340
throw err;
^
Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/lolmaus/hello_derby2/lib/server/index.js:1:77)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
So the question is: how do install project's dependencies into a central local repository and run the project without beating the f##k out of my Dropbox account?
Check out this link to npm's faq. Basically, you want to use the npm-link command. Go through your package.json and, for each dependency, do a sudo npm install -g <packagename>. Then link that package to your local project( see npm help link ).
The reason this is not the default behavior is that managing dependencies for multiple projects is a headache. Space is assumed to be cheap (and it is); having copies of dependencies is considered a low price to pay for fewer package version conflicts.
If you want to keep your project in Dropbox, I'd create a bare git repo in Dropbox and use it as the upstream repo for your project.
Run your project out of a non-Dropbox folder, and add the node_modules folder to .gitignore.

Resources