如何使用npm安装后添加节点项目的jquery? [英] How to add jquery with node project after installing with npm?
问题描述
我已经使用npm(npm install jquery --save)安装了jquery,并将jquery添加到我的node_modules文件夹中。我在html页面上的脚本标记使用什么路径?
I have installed jquery with npm (npm install jquery --save) and it added jquery to my node_modules folder. What path do I use for the script tag on the html page?
这是我的目录结构的样子(仅包括必要的内容)
Here is what my directory structure looks like (only included what is necessary)
-Project Root
-node_modules
-jquery
-dist
-jquery.js
-src
-index.js
-index.html
-package.json
对于socket.io,它看起来像这样
For socket.io it looks like this
<script src="/socket.io/socket.io.js"></script>
我试过这个用于jquery但它不起作用
I tried this for jquery but it didn't work
<script src="/jquery/dist/jquery.js"></script>
任何帮助将不胜感激!谢谢!
Any help will be greatly appreciated! Thanks!
推荐答案
您无需安装jQuery节点模块即可在客户端运行jQuery。您应该能够从公共或静态目录加载它。例如,
You don't need to install a jQuery node module to run jQuery on the client side. You should be able to just load it from a public or static directory. For example,
您的项目结构
-Project Root
-public
-js
jquery.js
-node_modules
-index.js
-index.html
-package.json
HTML
<!doctype html>
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script src="public/js/jquery.js"></script>
</head>
<body>
... stuff ...
</body>
</html>
app.js
我假设您正在使用Express,就像Socket.IO入门示例中推荐的那样。 http://socket.io/get-started/chat/ 所以在你的app.js中您需要声明您的公共或静态位置。这将允许您的index.html访问该文件夹中的文件。
I am assuming you are using Express, like what is recommended in the Socket.IO getting started example. http://socket.io/get-started/chat/ So in your app.js you need to declare your public or static location. This will allow your index.html to access the files from within that folder.
app.use('/public', express.static('public'));
快速服务静态
http://expressjs.com/starter/static-files.html
这篇关于如何使用npm安装后添加节点项目的jquery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!