Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'));
like image 235
user1796747 Avatar asked Aug 03 '26 04:08

user1796747


1 Answers

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');
like image 154
Linus Thiel Avatar answered Aug 04 '26 19:08

Linus Thiel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!