javascript - Nodejs加载并执行文件

标签 javascript node.js

是否可以让其返回输出?

hello.js

var y = 1;

console.log(x);
console.log(y);

ma​​in.js

var x = 42;

var magic = somehowInclude('hello.js');

当您使用 Node 运行 main.js 时,它会打印 421

是否可以在没有 requireexports 的情况下做到这一点?

最佳答案

使用 Node.js 模块。

hello.js

module.exports.magic = function (x) {
  var y = 1;

  console.log(x);
  console.log(y);
};

ma​​in.js

var x = 42;

var hello = require('./hello.js');
hello.magic(42);

阅读 module loading system 的详细说明在 Node.js 文档中。

关于javascript - Nodejs加载并执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10099521/

相关文章:

javascript - getRegistration().then 给出了未定义的

javascript - 在 JavaScript (Node.js) 中启动/停止后台工作程序

node.js - 预编译 Handlebars 模板

javascript - 如何使用 Thinktecture.IdentityServer.v2 和 Node.js 来实现 WS-Federation?

javascript - react中导入js对象时的内存

Javascript - 更改除 1 之外的 id 范围的所有类

javascript - npm:缺少 node_modules 中的传递依赖项

node.js - 将大型 MongoDB 实例中的字符串索引更改为 ObjectID 索引

javascript - 应用脚本 : filter out empty elements from array

javascript - $.isNumeric 与 isNaN