16
loading...
This website collects cookies to deliver better user experience
modules
, then these modules can share data between them and make our code more organized and maintainable. Now what's great about modules is we can use third-party modules within our application. There are various packages shared on the NPM repository which can be used e.g. React, Leaflet and etc. NPM stands for Node Package Manager
because it was originally built for Node.js and for Node.js However, NPM has established itself as the goto repository for all kinds of packages in Modern JavaScript Development
.Transpiling
and Polyfilling
, which is basically to convert all modern JavaScript syntax and features back to old ES5 syntax, so that even older browsers can understand our code without breaking. And this is usually done using a tool called Babel
. So these are the two steps of our build process, and after these two steps, we end up with that final JavaScript bundle, ready to be deployed on a server for production
. Webpack
and Parcel
. And these are called JavaScript bundlers because well, as the name says they take our raw code and transform it into a JavaScript bundle. Now Webpack
is the more popular one, but it can be really hard and confusing to set it up. So that's because there's a lot of stuff that we need to configure manually, in order to make it work properly. Parcel
, on the other hand, is a zero-configuration bundler, which simply works out of the box. And so in this bundler, we don't have to write any setup code, which is really amazing. modules
are small reusable
components that can be imported and exported between them and combined to form a bigger application. Composition
: These small building blocks put together to build complex applications.
Isolation
: Modules are developed in isolation without thinking about other codebases.
Abstraction
: Hiding implementation details from other components and only sharing functionalities.
Organization
: Modules lead to a more organized codebase.
Reusability
: Modules can be easily used with other projects for their specific functionality.
Now let's know what's the difference between a module and a script.js file
Now let's take a look at Behind the scenes: How ES6 Modules are imported
//1. index.html
<script type="module" defer src="index.js"></script>
// 2. getCoords.js
const getCoords = function () {
//return latitude and longitude
return { lat: 57.9, lng: 63.99 }
}
export { getCoords }
// 3. displayCountry.js
const displayCountry = function (lat, lng) {
//some code here
console.log(`You're in India. Welcome to India`);
}
export { displayCountry }
//4. index.js
import { getCoords } from './assets/getCoords'
import { displayCountry } from './assets/displayCountry'
const { lat: latitude, lng: longitude } = getCoords()
displayCountry(latitude, longitude)
In the above example, let's understand the parsing of index.js file step by step:
Asynchronous downloading of getCoords.js
and displayCountry.js
Linking imports to getCoords.js
and displayCountry.js
respectively.
Execution of getCoords.js
and displayCountry.js
respectively.
Execution of index.js
Few things to keep in mind,
objects
, functions
, classes
, or variables
available to the outside world it’s as simple as exporting
them and then importing
them where needed in other files.export const frontendFrameworks = ['React', 'Angular', 'Vue']
const hobbies = ['Singing', 'Football', 'Movies']
// Not available outside module
export const student = {
name: 'Nikhil',
birthYear: '1999',
education: 'Graduate',
university: 'ABC'
}
export {frontendFrameworks, student}
export { frontendFrameworks, student as user }
default
keyword:export default function displayCity(){
console.log(`You're in Mumbai. Welcome to Mumbai!`)
}
import
keyword, members to be imported in curly brackets and then the location of the module relative to the current file:import {frontendFrameworks, student} from 'app.js'
import student as user from 'app.js'
import * as Utils from 'app.js'
/* While accessing */
Utils.displayCity()
giving it a name of your choice
. In the following example Cart
is the name given to the imported default member:import Cart from 'app.js'
import Cart, { frontendFrameworks, student } from 'app.js'
Now as development of a project is completed, modules are coded with their functionalities. Let's move forward with NPM.
NPM
is a package manager for Node.js with hundreds of thousands of packages. Although it does create some of your directory structure/organization, this is not the main purpose.package.json
file, then any time you (or anyone else) needs to get started with your project they can just run npm install
and immediately have all of the dependencies installed. On top of this, it is also possible to specify what versions your project depends upon to prevent updates from breaking your project.$ npm init
/*Ths would initialize npm in the folder*/
$ npm install
/*This will install npm, you will see a package.json file and
inside the folder a node_ modules folder is added.
This node_modules contains all dependencies required for a project*/
/*Package .json consists of */
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
}
/*Now if we want to have leaflet library for our project*/
$npm install leaflet@version_num
/*The leaflet library gets added to node_modules folder
and in package.json leaflet is added into dependencies.
Package.json changes to*/
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"leaflet": "^1.7.1"
},
"author": "",
"license": "ISC",
}
Tip: While uploading your code to Git or copying it from some source to destination, never include node_modules
folder. There is no sense to move that folder as all the dependencies can be installed just by npm install
.
purpose of npm
is. As a Javascript developer (both client-side and server-side), NPM is an indispensable tool in modern and huge applications workflow.index.html
in browser. As browser doesn't understand modules. We need to convert these entire modules to scripts this process is called BundlingThere are various tools for Bundling
, Webpack
and Parcel
are most used. Webpack
requires a config file to setup the configurations but Parcel
doesn't require configuration.
Parcel
and how it is used?$ npm install parcel --save-dev
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"leaflet": "^1.7.1"
},
"devDependencies": {
"parcel": "^2.0.0-beta.2"
},
"author": "",
"license": "ISC",
}
$ npx parcel index.html
html
, css
, js
files into scripts and creates a folder named dist
.Parcel
includes a feature of Hot Module Replacement
. Hot Module Replacement (HMR) improves the development experience by automatically updating modules in the browser at runtime without needing a whole page refresh. This means that the application state can be retained as you change small things. This will only apply in development; HMR is automatically disabled when bundling in production mode.if (module.hot) {
module.hot.accept()
}
assets
like css
, sass
which means:/*importing module*/
/*Lets say we want to import react library*/
import React from './node_modules/react'
/*No need to specify path as shown above*/
import React from 'react';
parcel index.html
.{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "parcel index.html",
"build": "parcel build index.html"
},
"dependencies": {
"leaflet": "^1.7.1"
},
"devDependencies": {
"parcel": "^2.0.0-beta.2"
},
"author": "",
"license": "ISC",
}
$ npm run start
and build a bundle for entire project with $npm run build
.Netlify
or any hosting services.