Node.js 错误:找不到模块“请求”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28269973/
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-09-02 18:22:24  来源:igfitidea点击:

Node.js Error: Cannot find module 'request'

node.jsnpm

提问by Bachalo

Ok so this is a common error and I HAVE read this node.js: cannot find module 'request'

好的,这是一个常见错误,我已经阅读了这个 node.js: cannot find module 'request'

The request module IS installed in my node_modules. My complete node app is

请求模块安装在我的 node_modules 中。我的完整节点应用程序是

var r = require("request");


var s = r('http://www.foo.com/');


s.on('data',function(chunk){

    console.log(">>>Data>>> "+chunk);
});


s.on('end', function(){
    console.log(">>>Done!");
})

I run my app by simply calling

我通过简单地调用来运行我的应用程序

node app

But I keep getting the same error

但我不断收到同样的错误

What gives?

是什么赋予了?

My directory structure is

我的目录结构是

app.js
node_modules
    request
        node_modules
            bl
            combined-stream
            form-data
            hawk
            mime-types
            tough-cookie

The complete error trace is

完整的错误跟踪是

module.js:340
    throw err;
          ^
Error: Cannot find module 'request'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (/Users/foo/Documents/.../app.js:1:71)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)

回答by Samer Buna

Something looks wrong in your directory structure. I would nuke the node_modules directory and redo the npm command.

您的目录结构看起来有些错误。我会核对 node_modules 目录并重做 npm 命令。

It's always a good idea to maintain a package.json file and see the dependencies written out

维护一个 package.json 文件并查看写出的依赖项总是一个好主意

cd getFoo
npm init # answer the qestions
npm install --save request
node app.js

回答by drjorgepolanco

Go to the root folder of your app

转到您的应用程序的根文件夹

cd my-app

Delete folder node_modules

删除文件夹 node_modules

rm -rf node_modules

Reinstall package.json

重新安装 package.json

npm install

Start the server

启动服务器

npm start

回答by Ilknur Mustafov

I had the same problem, and i installed the request package like that: "npm install request --save" and that solved the problem.

我遇到了同样的问题,我安装了这样的请求包:“npm install request --save”并解决了问题。