I am learning ES6 modules. But I am confused with what's the difference between a module and library.
And also how module and library is different than a node.js package.
I am learning ES6 modules. But I am confused with what's the difference between a module and library.
And also how module and library is different than a node.js package.
Share Improve this question edited Aug 1, 2015 at 12:51 Yaroslav Admin 14.5k6 gold badges64 silver badges84 bronze badges asked Jul 10, 2015 at 11:29 Narayan PrustyNarayan Prusty 4274 silver badges9 bronze badges 2- 8 What a great question. The problem with any kind of technobabble is that everyone uses similar terms for different things. But I would say that "libraries" are just any old collection of useful JavaScript functions to make it easier to write common tasks. While "modules" are libraries that are encapsulated in some sort of standard way, for example CommonJS Modules, Asynchronous Module Definition (AMD), or ECMAScript 6 Modules (which should eventually become the universal standard). I'm not confident enough to write this as a proper answer, but I'm looking forward to what others say. – kieranpotts Commented Jul 10, 2015 at 11:41
- 1 A very good read on difference between module, Library and framework in general. – RBT Commented May 22, 2017 at 1:06
2 Answers
Reset to default 16A module is a unit of software. This refers - depending on the context - to a self-contained part of source code, to the file that the former is found in, or to the module object (data structure) said code declares (or generates when executed).
Typically there's a 1:1:1 relation between these, and this is a good practise. You seldomly find multiple modules in the same source file1. ES6 implementations will enforce this by taking single files as single modules, that can be imported by their unique name - just as it previously worked with CommonJS or AMD modules.
Next to ES6 modules, there also has been the module pattern, which uses IIFEs to encapsulate code and create singleton objects. See What is this JavaScript pattern called and why is it used?, here or the JS design patterns book for details.
And since modularity is so important, there have been many approaches at implementing module loaders, each with its own syntax and subtleties, often being part of a larger framework. See this article for further discussion.
A library is a collection of useful things that belong together and are distributed as a whole. This might comprise more than pure source code or more than a single language, but typically is not when we talk of a "javascript library". A library, consisting of a set of js functions, typically exports them as a module.
1: Except when they've been minified to a single script. Also, HTML5 might introduce ways to declare inline ES6 modules.
Libraries are the collection of modules. and modules is part of libraries. means modules contain singal functionality and libraries contains groups of modles.