Jamala
Jamala

Reputation: 21

node.js sharing javascript file between client and server

I want a JavaScript file to be included in my client as well as server.

Having some issues. Please help me.

Code:

require('app.js');

or

<script src='app.js'></script>

Which one should I use?

Upvotes: 2

Views: 243

Answers (1)

George
George

Reputation: 915

You can use Browserify to bundle your node module then import that js file with:

<script src='app.js'></script>

The require() function will now be on the global namespace and you can call the following to make the module available to use:

var app = require('app.js');

Note: in Browserify you'll need to export the module you want to import using require.

Upvotes: 1

Related Questions