问题描述
有一些第三方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库。它在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
:模块。
There is a much better method than using eval
: the vm
module.
例如,这是我的 execfile
模块,它在路径
中评估脚本 context
或全局上下文:
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
),并且脚本创建的所有全局变量都将添加到上下文中
。调试也更容易,因为语法错误等将以正确的文件名报告。
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!