user1796747
user1796747

Reputation: 142

expose nodejs library in nodejs public folder

Is there any way to reference a Nodejs library included in node_modules folder from a ejs view?

I'm using expressjs and my client libraries are served form /public folder as depicted below, so I'm not being able to reach the node_modules folder from a ejs view

app.use(express.static(__dirname + '/public'));

Upvotes: 3

Views: 1000

Answers (1)

Linus Thiel
Linus Thiel

Reputation: 39223

Update:

Using requirejs, it seems you can serve modules directly from node_modules. See the documentation on requirejs and node.

Example:

var requirejs = require('requirejs');

requirejs.config({
    nodeRequire: require
});

requirejs(['some_module'], function(some_module) {
    // Here, some_module will be loaded from your node_modules
});

Old answer:

You can use app.locals in express to expose properties or functions to your views.

See the example from the docs (linked above):

app.locals.title = 'My App';
app.locals.strftime = require('strftime');

Upvotes: 2

Related Questions