Js: can not find module‘ request’

我安装了 请求模块,并得到了错误:

module.js:340
throw err;
^
Error: Cannot find module 'request'

我已经阅读了所有关于这个错误的文章,并且理解这是因为模块请求没有全局发现,但是我已经尝试了2个建议

安装请求 -g

这个应该安装在/usr/loca/bin 中吗? 因为我没有看到它。

还有

Sudo npm 链路

/usr/local/lib/node _ module/request->/Users/soulsonic/dev/sandbox/node _ test/request

我重新启动终端后,每个命令,但不断得到无法找到模块错误。

更新

there must have been some sort of conflict in my initial directory, because "npm install request" was not adding "request" under node_modules (there 10 others in there) .. 在切换到一个新的目录后,它就工作了。

如果我使用-g switch 运行它,我确实看到它被安装到/usr/local/lib/node _ module/request。

似乎我只需要更新我的配置文件,以便上面的路径是自动添加。

230117 次浏览

You should simply install request locally within your project.

Just cd to the folder containing your js file and run

npm install request

Go to directory of your project

mkdir TestProject
cd TestProject

Make this directory a root of your project (this will create a default package.json file)

npm init --yes

Install required npm module and save it as a project dependency (it will appear in package.json)

npm install request --save

Create a test.js file in project directory with code from package example

var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body); // Print the google web page.
}
});

Your project directory should look like this

TestProject/
- node_modules/
- package.json
- test.js

Now just run node inside your project directory

node test.js

I was running into the same problem, here is how I got it working..

open terminal:

mkdir testExpress
cd testExpress
npm install request

or

sudo npm install -g request // If you would like to globally install.

now don't use

node app.js or node test.js, you will run into this problem doing so. You can also print the problem that is being cause by using this command.. "node -p app.js"

The above command to start nodeJs has been deprecated. Instead use

npm start

You should see this..

testExpress@0.0.0 start /Users/{username}/testExpress
node ./bin/www

Open your web browser and check for localhost:3000

You should see Express install (Welcome to Express)

I have met the same problem as I install it globally, then I try to install it locally, and it work.

if some module you cant find, try with Static URI, for example:

var Mustache = require("/media/fabio/Datos/Express/2_required_a_module/node_modules/mustache/mustache.js");

This example, run on Ubuntu Gnome 16.04 of 64 bits, node -v: v4.2.6, npm: 3.5.2 Refer to: Blog of Ben Nadel

I had same problem, for me npm install request --save solved the problem. Hope it helps.

ReferenceError: Can't find variable: require.

You have installed "npm", you can run as normal the script to a "localhost" "127.0.0.1".

When you use the http.clientRequest() with "options" in a "npm" you need to install "RequireJS" inside of the module.

A module is any file or directory in the node_modules directory that can be loaded by the Node. Install "RequiereJS" for to make work the http.clientRequest(options).

I tried installing the module locally with version and it worked!!

npm install request@^2.*

Thanks.