Using Node.js modules in HTML

别来无恙 提交于 2019-12-01 04:25:41

The problem is that you're using CJS modules, but still try to play old way with inline scripts. That won't work, it's either this or that.

To take full advantage of CJS style, organize your client-side code exactly same way as you would for server-side, so:

Create client.js:

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

Create bundle with Browserify (or other CJS bundler of your choice):

browserify client.js > client.bundle.js

Include generated bundle in HTML:

<script src="client.bundle.js"></script>

After page is loaded you should see "this is module1! and this is module2!" in browser console

You can also try simq with which I can help you.

Your problems with Smoothie Require, were caused by a bug (https://github.com/letorbi/smoothie/issues/3). My latest commit fixed this bug, so your example should work without any changes now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!