NodeJS 所需的模块在其他模块中不可用

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

NodeJS required module not available in other modules

node.jsmodulerequire

提问by Bas van Ommen

I'm a bit new to NodeJS. Maybe it's just the way it works but to be sure:

我对 NodeJS 有点陌生。也许这只是它的工作方式,但可以肯定的是:

My 'index.js':

我的“index.js”:

var fs = require('fs');
// do something with fs here
var app = require('./app.js');

The 'app.js'

'app.js'

fs.readFile('/somedir/somefile.txt', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});

Then I get an error:

然后我得到一个错误:

ReferenceError: fs is not defined

As I've read, the 'solution' to this is to 're-require' the fs-module in app.js. Now what I do understand is that the fs-module is cached (any module, but using the example) so Node will still be really quick. What I don't really get is: "If the fs-module is cached, so actually it's kinda available anyway, why do I still have to 're-require' the module?

正如我所读到的,对此的“解决方案”是“重新要求”app.js 中的 fs-module。现在我明白的是 fs-module 被缓存(任何模块,但使用示例)所以 Node 仍然会非常快。我真正没有得到的是:“如果 fs-module 被缓存,那么实际上它无论如何都可以使用,为什么我仍然必须'重新要求'该模块?

I'll be honest; it's just to understand why.

我会说实话;这只是为了理解为什么。

回答by Thank you

Each file has to include references to modules

每个文件都必须包含对模块的引用

index.js

索引.js

var fs    = require("fs"),
    other = require("./otherfile"); 

// you can now use `fs`

otherfile.js

其他文件.js

var fs = require("fs");

// you can now use `fs` here

One of the best parts about this is you're not locked into naming the variable a certain way in any given file. Every file is pretty much isolated from all the other files in your lib, and that's a verygood thing.

关于此的最好部分之一是您不会被锁定在任何给定文件中以某种方式命名变量。每个文件都与您的 lib 中的所有其他文件完全隔离,这是一件非常好的事情。

Also know that you can include just parts a module if you'd like

也知道如果你愿意,你可以只包含一个模块的部分

var read = require("fs").readFile;

read("myfile.txt", function(err, data) {
  if (error) {
    return throw error;
  }
  console.log(data);
};


Explanation:

解释:

Node.js does not encourage the use of globals; and as such, you should not try to implement things that depend on global variables.

Node.js 不鼓励使用全局变量;因此,您不应该尝试实现依赖于全局变量的东西。

When you call in the fsmodule again, it's not really "re-requiring" it so much as you're just declaring a variable that points to the cached module.

当您fs再次调用模块时,它并不是真正“重新要求”它,因为您只是声明了一个指向缓存模块的变量。



Additional example:

附加示例:

In this answerI go into detail about how to structure a simple app that avoids the use of globals.

这个答案中,我详细介绍了如何构建一个避免使用全局变量的简单应用程序。

回答by Felipe

Sometimes we can forget it, but it's fundamental to declare it:

有时我们可以忘记它,但声明它是基础:

var fs = require('fs');