Search code examples
angularnpmcdn

Is it mandatory to use npm modules?


I am new to Node.js and trying to understand node features. now I am developing an Angular 2 project where I can see every plugin is imported from node_modules folder.

My question is, is it mandatory to import everything from node_modules? What if I have CDN link to be used?

For example, if I want to use datatable and I have the links of its CDN (https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js) rather than using from node_modules.


Solution

  • Of course you can. But...

    Why modules?

    Using a technique called "Tree shaking" your bundler can run static analysis on your code dependencies and create a bundle that only includes referenced modules. This can drastically reduce the bundle size.

    Tree shaking

    Tree Shaking is an optimized way of creating application bundles. The idea is to create a bundle that only includes code that is directly used by the application. Unused modules will be excluded from the final bundle. As a result we may end up with a drastically smaller application bundle.

    Shorthand: There is mainly one important thing to remember: you should use only these parts of code which you will use. Thanks to modules you are able to do it and maybe now it doesn't seem so clear, but it will with your skills growth.