节点。无法找到模块'request'

时间:2022-12-29 16:55:46

I installed request module, and getting the error:

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

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

i've read all the posts about this error, and understand that this is because module requests is not globally found, but i've already tried the 2 suggestions

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

npm install request -g

npm安装请求- g

should this install it in /usr/loca/bin ? because i don't see it there.

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

and

sudo npm link

sudo npm链接

/usr/local/lib/node_modules/request -> /Users/soulsonic/dev/sandbox/node_test/request

/usr/local/lib/node_modules /请求- > /用户/ soulsonic / dev /盒/ node_test /请求

i restarted terminal after each command, but keep getting the cannot find module error.

我在每个命令之后重新启动终端,但是继续得到找不到的模块错误。

update

更新

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) .. after switching to a new directory it just worked.

在我的初始目录中一定有某种冲突,因为“npm安装请求”并没有在node_modules(还有其他10个)下添加“请求”。切换到新目录后,它就可以工作了。

if i run it with -g switch, i do see it bing installed to /usr/local/lib/node_modules/request.

如果我使用-g开关运行它,我确实看到它被安装到/usr/local/lib/node_modules/请求。

it seems that i just need to update my profile so that above path is automatically added.

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

6 个解决方案

#1


93  

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)

使该目录成为项目的根目录(这将创建一个默认包)。json文件)

npm init --yes

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

安装所需的npm模块,并将其保存为项目依赖项(它将出现在package.json中)。

npm install request --save

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

创建一个测试。项目目录中的js文件,代码来自包示例

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

node test.js

#2


21  

You should simply install request locally within your project.

您应该在项目中本地安装请求。

Just cd to the folder containing your js file and run

只需要cd到包含js文件的文件夹就可以运行了。

npm install request

#3


5  

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

我有同样的问题,对我来说npm安装请求——保存解决了这个问题。希望它可以帮助。

#4


-1  

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"

node app.js或node test。js,这样做会遇到这个问题。您还可以使用这个命令来打印正在发生的问题。“节点- p app.js”

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

上面启动nodeJs的命令已被弃用。而不是使用

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

打开web浏览器并检查localhost:3000。

You should see Express install (Welcome to Express)

您应该看到Express install(欢迎来到Express)

#5


-1  

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

我遇到了与全局安装相同的问题,然后我尝试在本地安装它,它就可以工作了。

#6


-1  

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

如果找不到某个模块,可以尝试使用静态URI,例如:

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

这个例子,运行在64位的Ubuntu Gnome 16.04上,节点-v: v4.2.6, npm: 3.5.2引用:Ben Nadel的博客。

#1


93  

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)

使该目录成为项目的根目录(这将创建一个默认包)。json文件)

npm init --yes

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

安装所需的npm模块,并将其保存为项目依赖项(它将出现在package.json中)。

npm install request --save

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

创建一个测试。项目目录中的js文件,代码来自包示例

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

node test.js

#2


21  

You should simply install request locally within your project.

您应该在项目中本地安装请求。

Just cd to the folder containing your js file and run

只需要cd到包含js文件的文件夹就可以运行了。

npm install request

#3


5  

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

我有同样的问题,对我来说npm安装请求——保存解决了这个问题。希望它可以帮助。

#4


-1  

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"

node app.js或node test。js,这样做会遇到这个问题。您还可以使用这个命令来打印正在发生的问题。“节点- p app.js”

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

上面启动nodeJs的命令已被弃用。而不是使用

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

打开web浏览器并检查localhost:3000。

You should see Express install (Welcome to Express)

您应该看到Express install(欢迎来到Express)

#5


-1  

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

我遇到了与全局安装相同的问题,然后我尝试在本地安装它,它就可以工作了。

#6


-1  

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

如果找不到某个模块,可以尝试使用静态URI,例如:

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

这个例子,运行在64位的Ubuntu Gnome 16.04上,节点-v: v4.2.6, npm: 3.5.2引用:Ben Nadel的博客。