简体   繁体   中英

NPM package install into public directory (best practice)

Currently I am working on a website project. Its file structure looks like below:

source_code
- application
- node_modules
- system
- www
-- js
-- css
-- img
-- third-party
-- index.php
- package.json

This time I would like to ensure third-party libraries with NPM.

What is the best practice if some package should be available in public way and I want to avoid the manual copy-paste flow.
(Example packages: jquery, lazysize, bootstrap)

It depends on how you're setting things up to be used. If you're using webpack , for example, you would probably handle your JavaScript dependencies in package.json , and let webpack collect all these into a dist/bundle.js file which is the actual JS file included in your index.html .

If you don't have a packaging step like this set up, you can do one of these methods:

  • Link out to a CDN in your index.html ( <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> )
  • Download this file in www/js/lib/ and include it locally within the site ( <script src="/js/lib/jquery-3.2.1.slim.min.js"></script> ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM