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 distribution version of the website (the live version) 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?
The directory 'node_modules' may not be in current directory, so you should resolve the path dynamically.
I didn't find any clean solutions (I don't want to expose the source of all my node_modules) so I just wrote a Powershell script to copy them:
As mentioned by jfriend00 you should not expose your server structure. You could copy your project dependency files to something like
public/scripts
. You can do this very easily with dep-linker like this: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:If you were using express with nodejs, a static route is as simple as this:
Then, any browser requests from
/scripts/xxx.js
will automatically be fetched from yourdist
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()
withexpress.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:And the code to create a route for that
Or, if you want to still delineate routes for scripts with
/scripts
, you could do this:And the code to create a route for that
I would use the path npm module and then do something like this:
If you want a quick and easy solution (and you have gulp installed).
In my
gulpfile.js
I run a simple copy paste task that puts any files I might need into./public/modules/
directory.The downside to this is that it isn't automated. However, if all you need is a few scripts and styles copied over (and kept in a list), this should do the job.