简体   繁体   English

Module.exports 导出 nodejs 中的变量

[英]Module.exports to export variables in nodejs

i have two javascript files.我有两个 javascript 文件。 a1.js and utils.js The files are as follows: a1.js 和 utils.js 文件如下:

a1.js a1.js

const namet = require('./utils.js')
console.log(name);

utils.js实用程序.js

console.log('utils.js');
const name ='Mike';
module.exports = name;

When I run this file I get an error message当我运行此文件时,我收到一条错误消息

ReferenceError: name is not defined

The only way I avoid the error message is if I change the require line in a1.js to say:避免错误消息的唯一方法是如果我将 a1.js 中的 require 行更改为:

const name = require('./utils.js')

What am I doing wrong?我究竟做错了什么? I thought that the name of the require const should not matter.我认为 require const 的名称应该无关紧要。 I am using node version 10.1.0我正在使用节点版本 10.1.0

Rename name on line 2 to namet将第 2 行的name重命名为namet

const namet = require('./utils.js')
console.log(namet);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM