node: Cannot find module (node installed with brew) - node.js

I try to run a script that i wrote in node. In order to debug and show you my problem, i reduced the script to:
#! /usr/bin/env node
var prompt = require('prompt');
When i try to run the script i get
module.js:340
throw err;
^
Error: Cannot find module 'prompt'
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> (/Users/derzyklop/S/dotfiles/bin/bugs:3:14)
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 tried to install "prompt" with npm install prompt and also global with npm install prompt -g. But my script still doesn't work.
"prompt" seems to be installed, because npm list --depth=0 gives me
and npm list -g --depth=0 gives me
I installed node via brew, so the next thing i tried was that i removed node by brew remove npm and brew remove node and i downloaded the installer for OSX from here. But that didn't change anything.
I'm out of ideas. Why can't node find the module?
Is it possible, that npm list -g searches at other paths than the require(..) in my script?
Update
The script is located in my personal bin-folder in ~/dotfiles/bin/myscript which is added to $PATH by export PATH=~/dotfiles/bin:$PATH.
I found out, that require() searches global modules in {prefix}/lib/node_modules, where {prefix} is npm config get prefix.
So i can temporary fix it by doing this: require('/usr/local/lib/node_modules/prompt');.
Local modules are searched in ./node_modules. In my case this must be ~/dotfiles/bin/node_modules, so i did
cd ~/dotfiles/bin/
npm install prompt
and with this i can use require('prompt'); in my script.
So the question is: How do i let my node-script search for modules in {executing directory}/node_modules?
And still: Why is require() not searching in /usr/local/lib/node_modules/?

Mocha is going to be your friend here, it is the fastest way to remedy this situation. Install mocha:
$ npm install -g mocha
Also your test script is problematic. So you can make test script: test.js with the following.
var assert = require("assert")
describe('Array', function(){
describe('#indexOf()', function(){
it('should return -1 when the value is not present', function(){
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
})
})
})
Then it is simple as running: mocha test.js
I tested this and I was receiving the same errors as you from node. Even if you do node test.js you will get an error. Hopefully this mocha solution works for you.
Refrences:
1. http://visionmedia.github.io/mocha/

Related

MEAN stack, cannot find module 'request'

I'm new to mean stack world, I installed mongoDB and nodejs
node -v => v4.4.3
npm -v => 2.15.1
mongo -version => MongoDB shell version: 3.2.6
then followed MEAN installation guide to install it.
after many faults I can run gult and have a response.
now when ever I try to run any command with mean, I get following error:
module.js:327
throw err;
^
Error: Cannot find module 'request'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/usr/lib/node_modules/mean-cli/lib/controllers/anonymized-data.js:8:12)
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 Module.require (module.js:353:17)
I tried with: sudo npm install request
but still getting same issue !! can any one guide me please? thanks in advance
UPDATE
after running command given by #Alexander Mac, I got following result
it's still same status :( . and without sudo I got lots or errors #Seth :P
Alex is right about 'request' not being part of the package. That said, instead of messing out with the install manually, it might be far easier to install a previous known good version
For e.g., you can try: sudo npm i -g mean-cli#0.12.1
mean-cli package has a bug. The authors forget to include request module in package.json: look here. You can create an issue about that on their site.
To resolve the problem locally go to /usr/lib/node_modules/mean-cli and run: sudo npm i request to install missing request module.
I use nvm to manage node.
I go to /home/ougwen1235/.nvm/versions/node/v4.2.4/lib/node_modules/mean-cli and run npm install request --save.Then, the problem was solved.

What's wrong with my typescript compiler? `tsc --version` returns nothing

In Linux Mint, I've installed node and used it to install typescript. With a working typescript installation, tsc --version should return the version number. In my case, nothing is returned at all.
When I run which tsc the result is /usr/local/bin/tsc.
When I run cat /usr/local/bin/tsc the result is:
#!/usr/bin/env node
require('../lib/tsc.js')
When I run find /usr/local -name 'tsc*' the result is:
/usr/local/bin/tsc
/usr/local/lib/node_modules/typescript/bin/tsc
/usr/local/lib/node_modules/typescript/lib/tsc.js
Does that look correct? If so, then any ideas on what else to look at would be appreciated?
Note I also tried running /usr/local/lib/node_modules/typescript/bin/tsc --version and got the same result ie nothing.
In my case on Ubuntu 16.04 the problem was because of I performed sudo apt-get install node-typescript, which delivered me broken tsc. After removing it via apt-get remove and then installing as recommended with npm install -g typescript, it worked fine.
Maybe this will help someone.
I discovered that running node --version returns nothing (but shouldn't) however nodejs --version works (the result being v0.10.25). Reinstalling node didn't fix this. I don't know why this would be the case, as I think I followed the recommended installation procedure.
However, editing file /usr/local/bin/tsc and changing the line:
#!/usr/bin/env node
to
#!/usr/bin/env nodejs
seemed to work. Now tsc --version returns message TS6029: Version 1.6.2 and I'm getting .js files generated when I run make.
I was getting the following error in Ubuntu 16.04.6 :
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module 'typescript/tsc.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/usr/bin/tsc:2:1)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
Running : npm install -g typescript didn't resolved the issue.
So, I had to install the latest version of node i.e nvm install <nvm version>
Then with npm install , it fixed the issue.

Installing MEAN Stack: npm -v module.js: 338 throw err; Error: Cannot find module './cache/caching-client.js'

I think I have installed node.js before but never used homebrew to do it. Today I tried to do a walkthrough of an application but got stuck at the very beginning. I am fairly new to programming so I am utterly confused as to why I can't get this to work.
I have tried a lot of things but can't seem to find the answer.
These are the commands I run in order to install the MEAN stack:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
(succesful no errors)
brew install node
(succesfull no errors)
I check if node has installed properly by typing:
node -v
v0.12.4
I then check if npm is installed properly by typing:
npm -v
Here is the error message:
module.js:338
throw err;
^
Error: Cannot find module './cache/caching-client.js'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at /usr/local/lib/node_modules/npm/lib/npm.js:22:24
at Object.<anonymous> (/usr/local/lib/node_modules/npm/lib/npm.js:466:3)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
I have installed node in a different way other than using Homebrew and maybe that is messing things up. Please let me know your thoughts.
What I have tried to do so far is uninstall node and reinstall it using this process:
https://gist.github.com/DanHerbert/9520689
Maybe you've already fixed, but the answer might serve others.
I tried what is in the post that Mario linked to in his comment, but it didn't work. It just so happens that my npm is installed at /Users/USERNAME/.node/bin/, so I added the following line in my .bash_profile file: export PATH="$HOME/.node/bin:$PATH".
In my case it was caused by me installing node with root and later with brew (without root).
As a result brew was not able to override the old npm files upon upgrading.
Manually removing the old node installation worked for me.

cannot find module socket.io

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!

Node.JS cannot find module 'xml2js' (Windows)

OS: Windows 7 64-bit
Need to do parsing xml-file using Node.js.
Using a library for parsing xml2js.
Xml2js installed using the command "npm install xml2js".
However, if you run the code:
var fs = require ('fs'), xml2js = require ('xml2js');
var parser = new xml2js.Parser ();
fs.readFile ('<path to the xml-file>', function (err, data) {
parser.parseString (data, function (err, result) {
console.dir (result);
console.log ('Done');
});
});
an error:
module.js:340
throw err;
^
Error: Cannot find module 'xml2js'
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> (<путь до js-файла>:3:14)
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)
Tell me, please, how to solve the problem?
I was also facing the same issue. Here's what I did:
Install the module using following:
$ npm install -g *module_name*
Then go to any parent dir of your project dir (it can be project dir itself) and run the following command:
$ npm link *module_name*
Make sure that the directory ./node_modules/xml2js exists.
When you run npm install somemodule at D:/test, it will be stored to D:/test/node_modules/somemodule, then you can require this module in D:/test/*.js, and you can not require it at D:/other/place/*.js. If you hope the module can be required anywhere you should run:
npm install somemodule -g
I had a similar issue and even tried the fix mentioned by Lellansin, but it didnt help. There were some issues with npm cache and I had to clear it using the command:
npm cache clean
And it worked for me.

Resources