加载“香草"Javascript 库到 Node.js [英] Load "Vanilla" Javascript Libraries into Node.js

查看:31
本文介绍了加载“香草"Javascript 库到 Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些第三方 Javascript 库具有一些我想在 Node.js 服务器中使用的功能.(特别是我想使用我找到的 QuadTree javascript 库.)但这些库只是简单的 .js 文件,而不是Node.js 库".

There are some third party Javascript libraries that have some functionality I would like to use in a Node.js server. (Specifically I want to use a QuadTree javascript library that I found.) But these libraries are just straightforward .js files and not "Node.js libraries".

因此,这些库不遵循 Node.js 对其模块所期望的 exports.var_name 语法.据我了解,这意味着当您执行 module = require('module_name');module = require('./path/to/file.js'); 你最终会得到一个没有可公开访问的函数等的模块.

As such, these libraries don't follow the exports.var_name syntax that Node.js expects for its modules. As far as I understand that means when you do module = require('module_name'); or module = require('./path/to/file.js'); you'll end up with a module with no publicly accessible functions, etc.

我的问题是如何将任意 javascript 文件加载到 Node.js 中,以便我可以利用其功能而无需重写它,以便它执行exports?"

My question then is "How do I load an arbitrary javascript file into Node.js such that I can utilize its functionality without having to rewrite it so that it does do exports?"

我对 Node.js 非常陌生,所以如果我对其工作原理的理解有一些明显的漏洞,请告诉我.

I'm very new to Node.js so please let me know if there is some glaring hole in my understanding of how it works.

编辑:进一步研究,我现在看到 Node.js 使用的模块加载模式实际上是最近开发的用于加载 Javascript 库的标准的一部分,称为 CommonJS.它在 Node.js 的模块文档页面 上说得对,但是直到现在我都错过了.

EDIT: Researching into things more and I now see that the module loading pattern that Node.js uses is actually part of a recently developed standard for loading Javascript libraries called CommonJS. It says this right on the module doc page for Node.js, but I missed that until now.

我的问题的答案最终可能是等到你的库的作者开始编写一个 CommonJS 接口或者你该死的自己来做."

It may end up being that the answer to my question is "wait until your library's authors get around to writing a CommonJS interface or do it your damn self."

推荐答案

有一个比使用 eval 更好的方法:vm 模块.

There is a much better method than using eval: the vm module.

例如,这是我的 execfile 模块,它在 context 或全局上下文中评估 path 处的脚本:

For example, here is my execfile module, which evaluates the script at path in either context or the global context:

var vm = require("vm");
var fs = require("fs");
module.exports = function(path, context) {
  context = context || {};
  var data = fs.readFileSync(path);
  vm.runInNewContext(data, context, path);
  return context;
}

它可以这样使用:

> var execfile = require("execfile");
> // `someGlobal` will be a global variable while the script runs
> var context = execfile("example.js", { someGlobal: 42 });
> // And `getSomeGlobal` defined in the script is available on `context`:
> context.getSomeGlobal()
42
> context.someGlobal = 16
> context.getSomeGlobal()
16

其中 example.js 包含:

function getSomeGlobal() {
    return someGlobal;
}

这种方法的一大优点是您可以完全控制执行脚本中的全局变量:您可以传入自定义全局变量(通过 context),以及由该脚本将被添加到 context.调试也更容易,因为语法错误等会以正确的文件名报告.

The big advantage of this method is that you've got complete control over the global variables in the executed script: you can pass in custom globals (via context), and all the globals created by the script will be added to context. Debugging is also easier because syntax errors and the like will be reported with the correct file name.

这篇关于加载“香草"Javascript 库到 Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆