Linux node.js:找不到模块“请求”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16482600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:54:31  来源:igfitidea点击:

node.js: cannot find module 'request'

linuxnode.jsterminalnpm

提问by Sonic Soul

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

我已阅读有关此错误的所有帖子,并了解这是因为模块请求未在全局范围内找到,但我已经尝试了 2 条建议

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

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

须藤 npm 链接

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

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 install request”没有在 node_modules 下添加“request”(那里还有 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/request.

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

看来我只需要更新我的个人资料,以便自动添加上述路径。

采纳答案by glukki

Go to directory of your project

转到您的项目目录

mkdir TestProject
cd TestProject

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

将此目录设为项目的根目录(这将创建一个默认package.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.jsfile in project directory with code from package example

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

回答by SLaks

You should simply install requestlocallywithin your project.

您应该简单地在您的项目中request本地安装。

Just cdto the folder containing your js file and run

只需cd到包含您的 js 文件的文件夹并运行

npm install request

回答by Rakesh Chouhan

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

nodeapp.js或 node test.js,你会遇到这个问题。您还可以使用此命令打印正在导致的问题。"node -p app.js"

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

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

npm start

You should see this..

你应该看到这个..

[email protected] 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 (Welcome to Express)

回答by Jeorge Washington

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

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

回答by Fabio Andres Pino Gutierrez

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

这个例子,运行在 Ubuntu Gnome 16.04 of 64 bits,node -v: v4.2.6, npm: 3.5.2 参考:Ben Nadel 的博客

回答by rust

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

我有同样的问题,因为我npm install request --save解决了这个问题。希望能帮助到你。

回答by Roberto Aprea

ReferenceError: Can't find variable: require.

参考错误:找不到变量:需要。

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

您已经安装了“npm”,您可以正常运行脚本到“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.

当您在“npm”中使用带有“options”的 http.clientRequest() 时,您需要在模块内安装“RequireJS”。

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

模块是 node_modules 目录中可由 Node.js 加载的任何文件或目录。安装“RequiereJS”以使 http.clientRequest(options) 工作。

回答by shamnas cv

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

我尝试使用 version 在本地安装模块,并且成功了!!

npm install request@^2.*

Thanks.

谢谢。