在 browserify 创建的包文件中使用现有模块对象
Use existing module object in bundle files created by browserify
我有两个文件:a.js
和 b.js
。
a.js
:
console.log(require("./b.js")());
module.exports = function () {
console.log("Hello Mars!");
};
b.js
:
module.exports = function () {
console.log("Hello World");
};
通过捆绑这些文件,我获得了 c.js
:
browserify a.js -o c.js
我希望 c.js
导出一个函数(调用时打印 Hello Mars
)。通过在节点进程中 require
ing c.js
我得到一个空对象。
$ node
> c = require("./c.js")
Hello World
undefined
{}
> c()
TypeError: c is not a function
at repl:1:1
at REPLServer.defaultEval (repl.js:252:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:417:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:826:14)
因为 node
已经有一个 module
对象,我如何告诉 Browserify 将这个 module
对象用于主文件,所以我将得到 Hello Mars
在 Node 中使用 require
时 module.exports
中的函数?
我查看了文档,但没有找到任何选项...也许我漏掉了什么。
我只想捆绑一个模块(具有依赖项)并 require
它在节点进程中。
您正在寻找以下选项:
--standalone -s
Generate a UMD bundle for the supplied export name.
This bundle works with other module systems and sets the name
given as a window global if no module system is found.
您可能还想传递 --node
选项,以关闭节点特定功能的转换。
我有两个文件:a.js
和 b.js
。
a.js
:
console.log(require("./b.js")());
module.exports = function () {
console.log("Hello Mars!");
};
b.js
:
module.exports = function () {
console.log("Hello World");
};
通过捆绑这些文件,我获得了 c.js
:
browserify a.js -o c.js
我希望 c.js
导出一个函数(调用时打印 Hello Mars
)。通过在节点进程中 require
ing c.js
我得到一个空对象。
$ node
> c = require("./c.js")
Hello World
undefined
{}
> c()
TypeError: c is not a function
at repl:1:1
at REPLServer.defaultEval (repl.js:252:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:417:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:210:10)
at REPLServer.Interface._line (readline.js:549:8)
at REPLServer.Interface._ttyWrite (readline.js:826:14)
因为 node
已经有一个 module
对象,我如何告诉 Browserify 将这个 module
对象用于主文件,所以我将得到 Hello Mars
在 Node 中使用 require
时 module.exports
中的函数?
我查看了文档,但没有找到任何选项...也许我漏掉了什么。
我只想捆绑一个模块(具有依赖项)并 require
它在节点进程中。
您正在寻找以下选项:
--standalone -s
Generate a UMD bundle for the supplied export name. This bundle works with other module systems and sets the name given as a window global if no module system is found.
您可能还想传递 --node
选项,以关闭节点特定功能的转换。