When you start doing cool stuffs with javascript in terminal instead of browser.
you write javascript code in file like index.js
, that file is known as a module.
Now there are two types of modules
we have already discussed about default module environment provided by nodejs -> commonjs
mkdir hello-es6-modules
cd hello-es6-modules
npm init -y
package.json
{
"name": "hello-es6-modules",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
now create two new files
first one -> hello-es6-modules/index.js
is it necessary to create a file with the same name mentioned in package.json
"main": "index.js"
? NO.
If you are not going to publish it to npm using npm publish
This field "main"
does not serve any purpose.
second one -> hello-es6-modules/oceans.js
package.json
and add the line ` βtypeβ:βmoduleβ `
{
"name": "hello-es6-modules",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
hello-es6-modules/oceans.js
write the code
const seahorse = 'It is a Seahorse';
export default seahorse;
hello-es6-modules/index.js
write
import oceansFromNowhere from './oceans.js'
console.log(oceansFromNowhere)
hello-es6-modules/index.js
, who is ready to console.log | print | give output. node index.js
It is a Seahorse
the above program was the example of β default export β
hello-es6-modules/oceans.js
clear up everything and write the code const shell = "I live in the ocean";
// you can export like this
export const fish = "I am a fish";
// and also like this too
export { shell };
// both exports works the same way
hello-es6-modules/index.js
import { shell, fish } from './oceans.js';
console.log(shell);
console.log(fish);
node index.js
I live in the ocean
I am a fish
hello-es6-modules/oceans.js
clear up everything and write the code const shell = "I live in the ocean";
export const fish = "I am a fish";
export { shell };
const king = "I am the king of the ocean";
export default king;
hello-es6-modules/index.js
import TheKiNg from './oceans.js';
import { shell, fish } from './oceans.js';
// or you can import with one line
// import TheKiNg, { shell, fish } from './oceans.js';
console.log(shell)
console.log(fish)
console.log(TheKiNg)
node index.js
I live in the ocean
I am a fish
I am the king of the ocean
export default king;
from a moduleimport TheKiNg from './oceans.js'
export const fish;
import { fish, sayHello } from './oceans.js'
import { fish as waterFish , sayHello } from './oceans.js'
hello-es6-modules/index.js
with the statement
import isStrange from './oceans.js';
hello-es6-modules/oceans.js
to export write
export default "this is not strange";
// export default const strange = "this is not strange"; not allowed
hello-es6-modules/oceans.js
to export type export default function () {
return 'I am river';
}
// allowed
// export default function river () {
// return 'I am river';
// }
with arrow functions
export default () => {
return 'I am river';
}
// not allowed
// export default const river = () => {
// return 'I am river';
// }
// or
// export default () => "I am river";
hello-es6-modules/oceans.js
to export type
export default class {
constructor() {
this.name = 'ocean';
}
say() {
return `I am ${this.name}`;
}
}
// allowed
// export default class Ocean {
// constructor() {
// this.name = 'ocean';
// }
// say() {
// return `I am ${this.name}`;
// }
// }
you can use names too. If you want