简体   繁体   中英

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM