Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include scripts located inside the node_modules folder?

I have a question concerning best practice for including node_modules into a HTML website.

Imagine I have Bootstrap inside my node_modules folder. Now for the production version of the website, how would I include the Bootstrap script and CSS files located inside the node_modules folder? Does it make sense to leave Bootstrap inside that folder and do something like the following?

<script src="./node_modules/bootstrap/dist/bootstrap.min.js"></script> 

Or would I have to add rules to my gulp file which then copy those files into my dist folder? Or would it be best to let gulp somehow completely remove the local bootstrap from my HTML file and replace it with the CDN version?

like image 802
Chris Avatar asked Dec 13 '14 22:12

Chris


People also ask

What is the fs module in Node JS?

The fs module provides a lot of very useful functionality to access and interact with the file system. There is no need to install it. Being part of the Node.js core, it can be used by simply requiring it: Once you do so, you have access to all its methods, which include:

Why node_modules is not in current directory?

The directory 'node_modules' may not be in current directory, so you should resolve the path dynamically. Show activity on this post. This is what I have setup on my express server: Good Luck... Show activity on this post.

How to use multiple files from node_modules in HTML?

To use multiple files from node_modules in html, the best way I've found is to put them to an array and then loop on them to make them visible for web clients, for example to use filepond modules from node_modules: Show activity on this post. If you are linking to many files, create a whitelist, and then use sendFile ():

How do I grant public access to my node_modules?

Create a symbolic link to node_modules. The easiest way to grant public access to node_modules is to create a symbolic link pointing to your node_modules from within your public directory. The symlink will make it as if the files exist wherever the link is created. For example, if the node server has code for serving static files


1 Answers

Usually, you don't want to expose any of your internal paths for how your server is structured to the outside world. What you can is make a /scripts static route in your server that fetches its files from whatever directory they happen to reside in. So, if your files are in "./node_modules/bootstrap/dist/". Then, the script tag in your pages just looks like this:

<script src="/scripts/bootstrap.min.js"></script> 

If you were using express with nodejs, a static route is as simple as this:

app.use('/scripts', express.static(__dirname + '/node_modules/bootstrap/dist/')); 

Then, any browser requests from /scripts/xxx.js will automatically be fetched from your dist directory at __dirname + /node_modules/bootstrap/dist/xxx.js.

Note: Newer versions of NPM put more things at the top level, not nested so deep so if you are using a newer version of NPM, then the path names will be different than indicated in the OP's question and in the current answer. But, the concept is still the same. You find out where the files are physically located on your server drive and you make an app.use() with express.static() to make a pseudo-path to those files so you aren't exposing the actual server file system organization to the client.


If you don't want to make a static route like this, then you're probably better off just copying the public scripts to a path that your web server does treat as /scripts or whatever top level designation you want to use. Usually, you can make this copying part of your build/deployment process.


If you want to make just one particular file public in a directory and not everything found in that directory with it, then you can manually create individual routes for each file rather than use express.static() such as:

<script src="/bootstrap.min.js"></script> 

And the code to create a route for that

app.get('/bootstrap.min.js', function(req, res) {     res.sendFile(__dirname + '/node_modules/bootstrap/dist/bootstrap.min.js'); }); 

Or, if you want to still delineate routes for scripts with /scripts, you could do this:

<script src="/scripts/bootstrap.min.js"></script> 

And the code to create a route for that

app.get('/scripts/bootstrap.min.js', function(req, res) {     res.sendFile(__dirname + '/node_modules/bootstrap/dist/bootstrap.min.js'); }); 
like image 98
jfriend00 Avatar answered Oct 11 '22 06:10

jfriend00