Javascript 节点引用错误模块

Javascript 节点引用错误模块,javascript,node.js,node-modules,Javascript,Node.js,Node Modules,我刚开始学习NodeJS,在学习模块时遇到了一个问题 我有两个文件放在同一个目录中,第一个是app.js,第二个是hello.js。 在app.js中,我写道: const hello = require('./hello'); console.log(me); 在第二个文件中,即hello.js文件中,我创建了一个对象,然后使用module.exports: let me = { name : 'Bao Chan', age : 20, job : 'Develope

我刚开始学习NodeJS,在学习模块时遇到了一个问题

我有两个文件放在同一个目录中,第一个是
app.js
,第二个是
hello.js
。 在
app.js
中,我写道:

const hello = require('./hello');
console.log(me);
在第二个文件中,即
hello.js
文件中,我创建了一个对象,然后使用
module.exports

let me = {
    name : 'Bao Chan',
    age : 20,
    job : 'Developer',
    hobbies : ['Listen music', 'Play Videogames']
}

module.exports = me;
然后我保存了2个文件并在cmd中键入了
node app.js
,但出现了一个错误:

ReferenceError: me is not defined
    at Object.<anonymous> (E:\Web Dev Assets\Projects\nodejs-tut\app.js:2:13)
    at Module._compile (internal/modules/cjs/loader.js:1151:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
ReferenceError:未定义me
反对。(E:\Web Dev Assets\Projects\nodejs tut\app.js:2:13)
at模块编译(内部/modules/cjs/loader.js:1151:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:1171:10)
在Module.load(内部/modules/cjs/loader.js:1000:32)
at Function.Module._load(内部/modules/cjs/loader.js:899:14)
在Function.executeUserEntryPoint[作为runMain](internal/modules/run_main.js:71:12)
在internal/main/run_main_module.js:17:47
我已经在这里呆了大约一个小时,但仍然没有弄明白,我不知道我是否错过了安装任何东西或我的代码有任何问题,请帮助我,非常感谢你们


非常感谢大家,我找到了解决方案。

您需要使用导入文件时使用的常量呼叫,该常量为您好

Try console.log(hello,"---")

您需要名为
hello
而不是
me
的变量中的hello文件(即使
me
是您在
hello.js
文件中实际调用的对象。请尝试
console.log(hello)
将变量名
hello
更改为
me

const me = require('./hello');
console.log(me);
试试这个:

hello.js

let me = {
    name : 'Bao Chan',
    age : 20,
    job : 'Developer',
    hobbies : ['Listen music', 'Play Videogames']
}

module.exports = {
    me: me
};
const hello = require('./hello');
console.log(hello.me);
app.js

let me = {
    name : 'Bao Chan',
    age : 20,
    job : 'Developer',
    hobbies : ['Listen music', 'Play Videogames']
}

module.exports = {
    me: me
};
const hello = require('./hello');
console.log(hello.me);

叫它
hello.me

const hello = require('./hello');
console.log(hello .me);

改为使用
const me
让我
只在模块范围内,您需要一个名为
hello
的变量中的hello文件,而不是
me
(即使我是您在
hello.js
文件中实际所称的我。请尝试
console.log(hello)
非常感谢您,我找到了解决方案。没问题:)