使用Node JS和Web Javascript共享模块

问题描述 投票:0回答:2

在Node JS和标准Web应用程序之间共享模块。

嗨,我有一个类库,我想在NodeJS和一个简单的Web页面中使用它们。为此,我创建了模块以导出我的函数(mode.export ...),我可以在我的NodeJS代码中调用它(var func = require(“module.js”)。但是,当我想使用它时Web javascript中的模块,语法为:

    <script type="module">
        import { Function1 } from './libmain.js';
    </script>

我有一个错误:找不到导入(Function1)。

这是可行的还是客户端和服务器端不能有共性?

非常感谢 - 基督徒

javascript node.js module
2个回答
0
投票

尝试

exports.Function1 = function() {
  return "something";
};

-1
投票

你可以像节点var func = require("module.js")一样使用它,

但是你需要“编译代码”你可以使用browserify,它类似于如何在HTML中使用节点模块

Hello World for JavaScript with npm modules in the browser

© www.soinside.com 2019 - 2024. All rights reserved.