Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript module.exports在哪里导出您的函数?当我们仍然使用require将代码导入到您的模块中时,它有什么用途_Javascript_Node.js - Fatal编程技术网

Javascript module.exports在哪里导出您的函数?当我们仍然使用require将代码导入到您的模块中时,它有什么用途

Javascript module.exports在哪里导出您的函数?当我们仍然使用require将代码导入到您的模块中时,它有什么用途,javascript,node.js,Javascript,Node.js,我在node.js中编写了以下代码 notes.js console.log('notes app is running'); app.js const notes = require('./notes.js'); console.log(notes); const notes = require('./notes.js'); var res = notes.addNote(); console.log(res); console.log(notes); 当我导

我在node.js中编写了以下代码

notes.js

console.log('notes app is running');
app.js

const notes = require('./notes.js');

console.log(notes);
const notes = require('./notes.js');
     var res = notes.addNote();
    console.log(res);
    console.log(notes);
当我导入代码并运行app.js时,输出显示为
notes app is running

现在我更新了notes.js的代码

console.log('notes app is running');
addNote = () =>{
 console.log('addNote');
 return  'New note';
} ;
现在我想在我的代码中使用下面的箭头函数,所以updtaed我的

app.js

const notes = require('./notes.js');

console.log(notes);
const notes = require('./notes.js');
     var res = notes.addNote();
    console.log(res);
    console.log(notes);
现在它给了我错误

notes.addNote is not a function
1) 我知道我应该使用module.exports.addNote

2) 但是我想知道为什么我们可以看到一个我们在notes.js中编写的日志,而不使用module.exports station。为什么我们不能像对类的实例那样使用requirestation和storetotal代码并从该变量调用函数呢

3) 更确切地说,module.export在哪里导出代码(我指的是哪个directrey)

4) 如果有什么不对劲,请纠正我(1和4不需要答案,所以我不回答了。)

2) 但是我想知道为什么我们可以看到一个我们在notes.js中编写的日志,而不使用module.exports station

使用Node.js的模块风格(这是CommonJS的一种风格),模块在第一次
require
d时被加载和执行。您的
console.log
位于模块的代码中,因此当您
require
it(第一次)时,该代码将运行

为什么我们不能像对类的实例那样使用requirestation和storetotal代码并从该变量调用函数呢

如果你想这样做,你可以:

exports = {
    // code here as object properties
    addNote: () => {
        console.log('addNote');
        return  'New note';
    }
};

3) 更确切地说,module.export在哪里导出代码(我指的是哪个directrey)

到内存中的。

(#1和#4不需要答案,所以我将它们省略。)

2) 但是我想知道为什么我们可以看到一个我们在notes.js中编写的日志,而不使用module.exports station

使用Node.js的模块风格(这是CommonJS的一种风格),模块在第一次
require
d时被加载和执行。您的
console.log
位于模块的代码中,因此当您
require
it(第一次)时,该代码将运行

为什么我们不能像对类的实例那样使用requirestation和storetotal代码并从该变量调用函数呢

如果你想这样做,你可以:

exports = {
    // code here as object properties
    addNote: () => {
        console.log('addNote');
        return  'New note';
    }
};

3) 更确切地说,module.export在哪里导出代码(我指的是哪个directrey)


到内存中。

在内部,节点缓存所有模块。为此,它从入口点文件(例如,you app.js文件)开始,递归搜索所有require语句(或导入)

当节点解析模块时,文件顶层的任何代码都将被执行,比如您的console.log行

console.log('notes app is running');
但是,请注意,此时文件中的任何内容都没有暴露给代码库的任何其他部分。相反,节点接受通过module.exports导出的任何值,并将其添加到内部缓存中。该缓存在require语句(转换为绝对路径)中显示的文件路径上设置关键帧,因此,例如,以下require语句:

const module1 = require('../module1.js');
const module2 = require('../module2.js');
将产生如下缓存项:

<path_to>/../module1.js = ... contents of module1.exports
<path_to>/../module2.js = ... contents of module2.exports
/../module1.js=。。。模块1.1的内容导出
/../module2.js=。。。module2.exports的内容
当您再次需要其中一个模块时,您将获得模块的缓存版本,它不会重新解析文件。例如,这意味着无论您需要notes.js文件多少次,它都只会打印您的console.log(“notes应用正在运行”);声明一次

由于节点隔离加载模块的方式,您只能访问通过module.exports导出的元素。这意味着无法访问您在文件中定义但未导出的任何函数

因此,要直接回答您的问题:

  • 我知道我应该使用module.exports.addNote
    • 对。但是,您也可以将新对象分配给module.exports,例如module.exports={addNote}
  • 但是我想知道为什么我们可以看到一个我们在notes.js中编写的日志,而不使用module.exports station。为什么我们不能像对类的实例那样使用requirestation和storetotal代码并从该变量调用函数呢
    • 因为节点在生成缓存时解析所有必需的文件
  • 更确切地说,module.export在哪里导出代码(我指的是哪个directrey)
    • 它们不存储在目录中,而是根据module.exports的文件名和内容缓存在内存中
  • 如果有什么不对劲,请纠正我
    • 我猜这个不需要答案

在内部,节点缓存所有模块。为此,它从入口点文件(例如,you app.js文件)开始,递归搜索所有require语句(或导入)

当节点解析模块时,文件顶层的任何代码都将被执行,比如您的console.log行

console.log('notes app is running');
但是,请注意,此时文件中的任何内容都没有暴露给代码库的任何其他部分。相反,节点接受通过module.exports导出的任何值,并将其添加到内部缓存中。该缓存在require语句(转换为绝对路径)中显示的文件路径上设置关键帧,因此,例如,以下require语句:

const module1 = require('../module1.js');
const module2 = require('../module2.js');
将产生如下缓存项:

<path_to>/../module1.js = ... contents of module1.exports
<path_to>/../module2.js = ... contents of module2.exports
/../module1.js=。。。模块1.1的内容导出
/../module2.js=。。。module2.exports的内容
当您再次需要其中一个模块时,您将获得模块的缓存版本,它不会重新解析文件。例如,这意味着无论您需要notes.js文件多少次,它都只会打印您的console.log(“notes应用正在运行”);声明一次

由于节点隔离加载模块的方式,y