如何引用HTML中的节点模块中的js文件? [英] How do I reference a js file from a node module in HTML?

查看:292
本文介绍了如何引用HTML中的节点模块中的js文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用JetBrains WebStorm创建了一个Node.js Express应用程序.我使用npm(通过File-> Settings-> Node.js和NPM)安装了名为 validator 用于字符串验证.

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.

该软件包安装在 node_modules 下,可以.如果在服务器代码中执行var validator = require('validator');,则可以成功使用验证功能.

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.

问题是我还想在客户端JavaScript中使用验证器.我可以包含这样的脚本:

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>

但这意味着我必须将 xss-filters.min.js node_modules 文件夹复制到公共 javascripts 文件夹中.然后,如果我使用npm更新软件包,则文件将不同步.

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.

是否有某种方法可以从我的视图中引用 node_modules ,或创建某种类型的链接文件或文件引用或其他内容?我宁愿不必手动维护它.

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.

推荐答案

您应该考虑使用browserify,它允许您通过构建所有依赖项来在浏览器中使用模块.因此基本上,您的代码就像在服务器端 http://browserify.org

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

这篇关于如何引用HTML中的节点模块中的js文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆