The code snippet on the left demonstrates two ways of exporting functions from a module. For Example...
At the top of the new file, converters.js, the function celsiusToFahrenheit() is declared.
After that function, on the next line of code, the first approach for exporting a function from a module is shown.
In this case, the already-defined function celsiusToFahrenheit() is assigned
to module.exports.celsiusToFahrenheit.
Below that, an alternative approach for exporting a function from a module is shown.
In this second case, a new function expression is declared and assigned to module.exports.fahrenheitToCelsius.
This new method is designed to convert Fahrenheit values back to Celsius.
Both approaches successfully store a function within the module.exports object.
module.exports is an object that is built-in to the Node.js runtime environment.
Other files can now import this object, and make use of these two functions, with another
feature that is built-in to the Node.js runtime environment: the require() function.
So, for Node Modules, the EXPORTING code looks like...
module.exports.celsiusToFahrenheit = celsiusToFahrenheit;
OR
module.exports.fahrenheitToCelsius = function(fahrenheit) {
return (fahrenheit - 32) * (5/9);
};
And the IMPORTING code looks like...
const { celsiusToFahrenheit } = require('./converters.js');
OR if there were more than one function to use you would say...
const { celsiusToFahrenheit, secondFunction, ...etc... } = require('./converters.js');
OR you could even create them individually like this...
const allModules = require('./converters.js');
const celsiusToFahrenheit = allModules.celsiusToFahrenheit;
const secondFunction = allModules.secondFunction;
const myThirdFunction = allModules.thirdFunction;
secret-messages.js
import { toggleHiddenElement, changeToFunkyColor } from './dom-functions.js';
const buttonElement = document.getElementById('secret-button');
const pElement = document.getElementById('secret-p');
buttonElement.addEventListener('click', () => {
toggleHiddenElement(pElement);
changeToFunkyColor(buttonElement);
});
Note...
If you need to import a method/function and change its name (maybe you have 2 different files that
have identically named methods) then you can change the name(s) by...
import {validate as validateUsername} from "./username-validation.js/";
import {validate as validatePassword} from "./password-validation.js/";
And apparently you can import everything from a file by...
import * as MyClasses from "./MyClass.js"
// use MyClasses.MyClass, MyClasses.MyOtherClass, MyClasses.More
NOTE...
This does not work when running on a local machine but when running from a server the clicking of
the button seems to work.
So it works when running via github... so you can try
clicking HERE to run it
Using default when exporting/importing.
ES6 provides two ways to export a module from a file: named export and default export.
// exports from ./MyComponent.js file
export const MyComponent = () => {}
export const MyComponent2 = () => {}
// export
const MyComponent = () => {}
export default MyComponent;
// import
import MyDefaultComponent from "./MyDefaultExport.js";
//The naming of import is completely independent in default export and we can use any name we like.
So, for EXPORTING, we use braces {} when exporting one or more named methods, eg...
export {method1, method2, method3, ...};
And/Or we could export it at the time of creating the method...
export const MyComponent = (domElement) => {return ...};
and do not use braces for the default export...
export default MyDefaultMethod;
And, for IMPORTING, we use braces {} with named imports, eg...
import {method1, method2, ...} from "./MyModules.js"
and do not use braces for the default import...
import MyDefaultMethodName from "./MyModules.js"
(Note... on default imports we need to name the default method and we can name it
anything we like, it is completely independent from the .js file)
You could read the MDN Web Document on JavaScript Modules
HERE
if you like.
And you could read the Codecademy Document on getting user input in Node.js
HERE
if you like.