Using CDN vs Installing library by NPM

Though, I have been using NPM, but I do not understand how the files in the node_modules are added to my index.html and work from there.

For Example, if I have to use jQuery, its so simple. I will get the file through cdn and add in my index.html file

CASE I: CDN

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 

<html>

<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
$('body').css('background','red');
});
</script>
</body> 
</html> 

我可能误解了你的问题......但是你不能只是将这一行添加到你的index.html文件中吗?

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

CDN

Use CDN if you are developing a website which will be accessible by the internet users.

CDN Benefits:

  • Will be cashed on the most browsers because its used by a lot of other websites

  • Reduce the bandwidth

  • check for more benefits here

    NPM

    npm is a great tool to manage dependancies in your app with using a module bundler.

    Example:

    assume using a webpack module bundler and jQuery in installed

    import $ from 'jQuery'
    ...
    var content = $('#id').html();
    

    but the browser does not understand the import statement so you have to transpile the code with webpack commands, the bundler will check all the used dependancies in bind them in a single file without any dependancies problems.

    useful links : Getting start with webpack


    I think you want to host jQuery yourself and use it within a web app running in the browser.

    If so, you need to host this file - make it downloadable via the same web server you are using to host index.html .

    If you are using Express, you might do something like this on the server side:

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

    And then reference the file in index.html :

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

    See Express' manual for serving static files.

    If you're not using Express, you need to consult your web server's stack manual. No way to guess unfortunately - I gave an Express.js example because this is probably the single most popular package like that for node.js.

    链接地址: http://www.djcxy.com/p/96018.html

    上一篇: 视频正在播放

    下一篇: 使用CDN与NPM安装库