Can not find async after installation - node.js

Today, I get strange thing that after i install async in global, nodejs reports it can not find the module.following is the workflow
install async
npm install -g async
make sure async exists
npm list -g async
get this output:
/usr/local/lib
├── async#0.2.9
└─┬ npm#1.3.21
└─┬ request#2.30.0
└─┬ form-data#0.1.2
└── async#0.2.9
3.try to use it.
I create a simple js file which only contains one statement:
var async=require('async');
then execute the file via node, I get exception:
Error: Cannot find module 'async'
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. (/lxzhu/nodejs/asynctest/test.js:1:73)
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)

It is because you are installing async globally.
npm install async will put create a directory called node_modules, and the require lookup algorithm will find it there.

A global installation of an NPM doesn't always mean that the module can be shared for multiple projects. This is a pretty popular misconception. You can read this blog post on nodejs.org for more information, but generally speaking, global modules are used for command line tools and other system utilities, not for modules to be used in your code.
So, ideally, you would need the modules locally for each of your projects.

async installed globally. For that we have to create and install modules of async.
npm install async --save
This command line add files in node_modules folder.

This worked for me:
npm uninstall async
npm install -g async
npm link async

One way of using globally installed modules in multiple projects is to use the npm link command
npm link will create a symlink of the globally installed package into your apps node_modules directory

if dont Find Any Module like
Cannot find module 'sql' , Cannot find module 'nodemailer'
then use npm install and module name which is can not found.
npm install async

Finally, i get answer from http://nodejs.org/api/modules.html.
After install globally, i need to put its subdirectory to NODE_PATH to make it appears in node's search path.
Also, as the document said, It is suggested to store module locally and NODE_PATH is for version compatibility and we should not use it any more.

Related

Node module-alias Error: Cannot find module '#src/utils/constants'

I'm running mocha tests and I recently upgraded to the newest version of the ask-cli. I ran the tests again and I'm now receiving this module error. I've npm installed src and utils to no avail. From what I've read it's possible module-alias does not support #src paths.
I'm calling this command:
$ ask api simulate-skill -l en-US -t "start my day" -s amzn1.ask.skill.XXXXXXXX-4156-4ca0-b14e-XXXXXXXXXXXX
Update: this seems to be an issue with the ask-cli which uses
const CONSTANTS = require('#src/utils/constants');
The #src should find the source of the node package but this #src is not used in any other packages I could find. All other packages seem to use ../../ so that's likely while module-alias does not find anything bc it doesn't know how to route this new syntax
Does anyone know of this new #src syntax and if there is a npm module for helping route it?
Which worked for ask-cli 1.1.6 but I upgraded to 1.7.2 for new functionality.
Error: Cannot find module '#src/utils/constants'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._resolveFilename (/Users/calebgates/WebstormProjects/AutomatedUtteranceTesting/node_modules/module-alias/index.js:49:29)
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> (/Users/calebgates/WebstormProjects/AutomatedUtteranceTesting/node_modules/ask-cli/lib/commands/init/index.js:1:81)
at Module._compile (module.js:652:30)
at Module.replacementCompile (/Users/calebgates/WebstormProjects/AutomatedUtteranceTesting/node_modules/nyc/node_modules/append-transform/index.js:58:13)
at module.exports (/Users/calebgates/WebstormProjects/AutomatedUtteranceTesting/node_modules/nyc/node_modules/default-require-extensions/js.js:8:9)
at Object.<anonymous> (/Users/calebgates/WebstormProjects/AutomatedUtteranceTesting/node_modules/nyc/node_modules/append-transform/index.js:62:4)
module.js:549
I solved this by including the ask-sdk in my local project's dependencies.
This is acknowledged bug when ask-cli is installed locally. There are two work-arounds pointed out in the bug report.
Either install ask-cli globally:
$ npm install -g ask-cli
Or install the module-alias package:
$ npm install module-alias
and configure it to look for #src in the ask-cli/lib folder by adding the following to your package.json file:
"_moduleAliases": {
"#src": "./node_modules/ask-cli/lib",
"#root": "./node_modules/ask-cli",
"#test": "./node_modules/ask-cli/test"
}

node: Cannot find module (node installed with brew)

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/

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 ...

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!

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