How to use a node.js library in html?

2020-03-30 06:57发布

问题:

I install fancybox dependency via :

npm install fancybox

I'm attempting to use the fancybox css within a html page :

<a class="fancybox fancybox.iframe"

I cannot require the fancybox library within the html page ?

Prior to using node I would just import the library directly on the page:

   <script src="http://fancyapps.com/fancybox/source/jquery.fancybox.js"></script> 
  <link rel="stylesheet" type="text/css" href="http://fancyapps.com/fancybox/source/jquery.fancybox.css"></link>  

But how to use the node library on html page ?

Update :

To achieve this I just copied the node_modules directory to the public folder. Then exposed the library with :

var express = require('express');
var app = express();

app.use(express.static(__dirname + '/public'));

回答1:

The way to do this is to use a build tool such as webpack. You use npm to install your libraries into the node_modules/ subdirectory, require them within your javascript files and use webpack to produce a single bundle .js file that contains everything.

I'd start here: http://webpack.github.io/docs/what-is-webpack.html



标签: node.js npm