我有以下Node.js项目(这是我的问题的最小工作示例):

module1.js:

module.exports = function() {

return "this is module1!";

};

module2.js:

var module1 = require('./module1');

module.exports = function() {

return module1()+" and this is module2!";

};

server.js:

var module2 = require('./module2');

console.log(module2()); // prints: "this is module1! and this is module2!"

现在我想创建一个也将使用module2.js的client.html文件.这是我尝试过的(并且失败了):

天真的版本:

// should alert: "this is module1! and this is module2!"

这显然不起作用 – 它产生两个错误:

> ReferenceError:未定义require.

> ReferenceError:未定义module2.

browserify module2.js > module2.browserified.js

我将client.html更改为:

var module2 = require('module2');

alert(module2());

这不起作用 – 它会产生一个错误:

> ReferenceError:未定义module2.

使用@Torben的Smoothie.js:

var module2 = require('module2');

alert(module2());

这不起作用 – 它产生三个错误:

> module2.js第1行的语法错误.

> SmoothieError:无法加载module2(0)

> TypeError:module2不是函数

我查看了require.js,但它看起来太复杂了,无法与Node.js结合 – 我没有找到一个简单的例子,只需要一个现有的Node.js模块并将其加载到一个网页中(如示例中所示).

我查看了head.js和lab.js,但未发现Node.js的要求.

那么,为了在HTML页面中使用我现有的Node.js模块module2.js,我该怎么办?

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐