Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reference a js file from a node module in HTML?

I've used JetBrains WebStorm to create a Node.js Express App. I used npm (via File->Settings->Node.js and NPM) to install a package called validator which is used for string validation.

The package was installed under node_modules, which is fine. If I do var validator = require('validator'); in my server code, I can use the validation functions successfully.

The problem is that I would also like to use validator in client JavaScript. I can include the script like this:

<script src="/javascripts/xss-filters.min.js"></script>

But that means I have to copy xss-filters.min.js from the node_modules folder into the public javascripts folder. Then, if I ever update the package with npm, the files will be out of sync.

Is there some way to reference node_modules from my view, or to create some sort of linked file or file reference or something? I'd rather not have to maintain this manually.

like image 774
Andy West Avatar asked Apr 05 '15 07:04

Andy West


2 Answers

you should consider using browserify, which allows you to require modules in the browser by building all the dependencies. so basically you code like you would do in server side http://browserify.org

like image 168
Edgar Zakaryan Avatar answered Sep 21 '22 06:09

Edgar Zakaryan


You can done it by using another node.js module, called node-browserify

  • how to use node.js module system on the clientside

You can try to use bower, or yeoman. bower - will simplify the process to include js libs. yeoman - will help you to build projects with the the libraries that you need.

like image 42
mabc224 Avatar answered Sep 23 '22 06:09

mabc224