Search This Blog

2020/12/24

export & exports in Node.js

 

We will look into export & import functionality of node.js

Reusable code written in js file is exported then called from other places by importing this functionality.


Lets create a project as follows

sangram@sangram-HP-Laptop-15-bs0xx:~/projects$ mkdir destructering/

sangram@sangram-HP-Laptop-15-bs0xx:~/projects/destructering$ npm init -y

sangram@sangram-HP-Laptop-15-bs0xx:~/projects/destructering$ touch index.js


Open Visual Studio Code editor here & add a folder utils,Inside utils as index.js


utils/index.js


function Add(a,b)

{

return a+b;

}

function Substract(a,b)

{

return a-b;

}

function Multiply(a,b)

{

return a*b;

}

function Divide(a,b)

{

return a/b;

}


module.exports ={Add,Substract,Multiply,Divide}


At bottom of this file we have module.export which exports our four functions.

One can export some function while keeping other unexported.e.g.


module.exports ={Add,Multiply}


Here we are exporting only two functions.


We can assign names different from function name at time of exports as follows


module.exports ={sub:Substract,div:Divide}


Other syntax to export:

exports.Add= function(a,b)

{

return a+b;

}

exports.Substract = function (a,b)

{

return a-b;

}

exports.Multiply = function (a,b)

{

return a*b;

}

exports.Divide = function (a,b)

{

return a/b;

}

exports.PI = 3.14;

Not neccessary to import only function you can import const also here PI is imported.

ES6 Syntax:

export function Add(a,b)

{

return a+b;

}

export function Substract(a,b)

{

return a-b;

}

export function Multiply(a,b)

{

return a*b;

}

export function Divide(a,b)

{

return a/b;

}

export const PI = 3.14;


Another Way in Es6:

function Add(a, b) {

return a + b;

}

function Substract(a, b) {

return a - b;

}

function Multiply(a, b) {

return a * b;

}

function Divide(a, b) {

return a / b;

}

const PI = 3.14;

export { Add, Multiply, PI };

 

To test Es6 syntax install babel:

npm install --save-dev babel-cli babel-preset-es2015 rimraf

into package.json add two more command

"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",

"start": "npm run build && node dist/index.js"

and create .babelrc file in root add following to it.

 

{

"presets": ["es2015"]

}


to run project us npm start.


Default Export:

Lets create new file area.js in util to show default export syntax.

utils/area.js


export default function cube(x) {

return x * x * x;

}


Per module only one default export is allowed.

Its imported as follows

      import cube from './utils/area.js'

 

Now I added one more function beside cube as follows.

export default function cube(x) {

return x * x * x;

}

export function square(x)

{

return x * x;

}

Newly added function is imported as follows

import {square} from './utils/area.js'

 

My index.js file where i am testing this function looks like below.


import {Add,Multiply,PI} from'./utils/index.js'

import cube from './utils/area.js'

import {square} from './utils/area.js'


var addition = Add(5,7)

console.log(`addition: ${addition}`);


var multiply = Multiply(5,7)

console.log(`multiply: ${multiply}`);


console.log(`PI = ${PI}`);


var cube_area = cube(3);

console.log(`cube_area: ${cube_area}`);


var square_area = square(3);

console.log(`square_area : ${square_area }`);



Exports From :

create middleman.js inside utils. And add following

 

export { square } from './area.js';


Now replace following

     import {square} from './utils/area.js'

with

       import {square} from './utils/middleman.js'

in index.js our entrypoint file.

 

Output:

sangram@sangram-HP-Laptop-15-bs0xx:~/projects/destructering$ npm start


> destructering@1.0.0 start

> npm run build && node dist/index.js

> destructering@1.0.0 build

> rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files


index.js -> dist/index.js

utils/area.js -> dist/utils/area.js

utils/index.js -> dist/utils/index.js

utils/middleman.js -> dist/utils/middleman.js

addition: 12

multiply: 35

PI = 3.14

cube_area: 27

square_area : 9

 

Our code will run with same output.

 

Here in middleman.js we are exporting square by taking from area.js.

No comments:

Post a Comment