Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer NPM installed libraries from web pages and templates?

Tags:

I have installed, say, jQuery:

npm install jquery

Now my site has

ROOT/node_modules/jquery/*

subdirectory.

What to do next? Just write

<script src="/node_modules/jquery/src/jquery.js"></script>

Or I can do something to take jquery.js out this path and/or encode reference in some portable way?

For example, suppose I am using Jade template. Shoud I write just

script(src="/node_modules/jquery/src/jquery.js")
like image 942
Dims Avatar asked Jun 14 '17 08:06

Dims


1 Answers

To answer your question shortly: yes, you can copy jquery.js and move it to whatever folder you want. Then, you would have to change the src attribute in the script element, like this:

<script src="/new/path/jquery.js"></script>

However, in production cases, you would most likely want to join all the js files, and then only load a few files, like the following:

<script src="/path/libs.js"></script>

where libs.js contains all of our dependencies in correct order.

like image 175
uksz Avatar answered Sep 21 '22 03:09

uksz