Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Node.js modules in HTML

I have the following Node.js project (which is a Minimal Working Example of my problem):

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!"

Now I want to create a client.html file that will also use module2.js. Here is what I tried (and failed):

naive version:

<script src='module2.js'></script>
<script>alert(module2());</script> // should alert: "this is module1! and this is module2!"

This obviously doesn't work - it produces two errors:

  • ReferenceError: require is not defined.
  • ReferenceError: module2 is not defined.

Using Node-Browserify: After running:

browserify module2.js > module2.browserified.js

I changed client.html to:

<script src='require.js'></script>
<script>
    var module2 = require('module2');
    alert(module2());
</script>

This doesn't work - it produces one error:

  • ReferenceError: module2 is not defined.

Using Smoothie.js by @Torben :

<script src='require.js'></script>
<script>
    var module2 = require('module2');
    alert(module2());
</script>

This doesn't work - it produces three errors:

  • syntax error on module2.js line 1.
  • SmoothieError: unable to load module2 (0 )
  • TypeError: module2 is not a function

I looked at require.js but it looks too complicated to combine with Node.js - I didn't find a simple example that just takes an existing Node.js module and loads it into a web page (like in the example).

I looked at head.js and lab.js but found no mention of Node.js's require.

So, what should I do in order to use my existing Node.js module, module2.js, from an HTML page?

like image 699
Erel Segal-Halevi Avatar asked Jan 27 '14 10:01

Erel Segal-Halevi


People also ask

Can I use Nodejs with HTML?

Run nodemon to start the server. Whenever the server is running, and accessing the route http://localhost:3000/ , it will output the plain text hello world! . We can use the same server to render HTML elements as the server response instead of sending plain text. Here is a list of some HTML elements.

How do I import a module in HTML?

In order to use the import declaration in a source file, the file must be interpreted by the runtime as a module. In HTML, this is done by adding type="module" to the <script> tag.

Can you run node modules in the browser?

js modules from JavaScript running in the browser has many advantages because it allows you to use Node. js modules for client-side JavaScript applications without having to use a server with Node. js just to implement functionality that is already available as a Node.


3 Answers

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

like image 119
Mariusz Nowak Avatar answered Oct 24 '22 06:10

Mariusz Nowak


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

like image 44
David Kudera Avatar answered Oct 24 '22 06:10

David Kudera


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.

like image 20
Torben Avatar answered Oct 24 '22 08:10

Torben