Introduction
Why we’re using Node.js: Node.js came into existence because we can only run JS in a browser and wanted it to run on your machine as a standalone application.
Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine. using non-blocking I/O
What is a module? In Node.js, a module is a collection of JavaScript functions and objects that can be used by external applications
Prerequisites
- You will need Node.js and npm installed on your development environmeny
- node –version: v12.16.1 (for this tutorial)
- By having Node.js installed you will also have npm installed;
- this tutorial uses version node -version 6.13.7.
- You should also be familiar with the
package.jsonfile, and experience with npm commands would be useful as well. To gain this experience, follow - Step 1 — Creating a
package.jsonFile. - It will also help to be comfortable with the Node.js REPL (Read-Evaluate-Print-Loop). You will use this to test your module. If you need more information on this, read our guide on How To Use the Node.js REPL.
Step 1 — Creating a Module
This step will guide you through creating your first Node.js module. You will use the Node.js built-in exports property to make the function and array available to external programs.
In your terminal, make a new folder called covid-data-module and move into it:
- mkdir covid-data-module
- cd covid-data-module
Overview
- Create a
package.jsonfile - Create the file that will be loaded when your module is required by another application
- Test your module
Create a package.json file
- To create a
package.jsonfile, on the command line, in the root directory of your Node.js module, runnpm init:- For scoped modules, run
npm init --scope=@scope-name - For unscoped modules, run
npm init
- For scoped modules, run
- Provide responses for the required fields (
nameandversion), as well as themainfield:name: The name of your module.version: The initial module version. We recommend following semantic versioning guidelines and starting with1.0.0.main: The name of the file that will be loaded when your module is required by another application. The default name isindex.js.
Initialize npm so other programs can import this module later in the tutorial:
- npm init -y
You used the -y flag to skip the usual prompts to customize your package.json.
"name": "covid-data-module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
const getCovidStats = async() => {
try {
const response = await fetch('https://covidtracking.com/api/us');
const usa = await response.json();
console.log(usa);
}
catch (err) {
console.log(`Error: ${err}`);
}
};
exports.printStats = function (){
getCovidStats();
}
Once your package.json file is created, create the file that will be loaded when your module is required. The default name for this file is index.js.
In the file, add a function as a property of the exports object. This will make the function available to other code:
The exports keyword references a global object available in every Node.js module. All functions and objects stored in a module’s exports object are exposed when other Node.js modules import it.
Save and exit the file.
Publish Module to NPM
If you don’t have an npm account – Create one below:
Login using your credentials:
npm login
After logging in, you’re ready to publish!
npm publish
-
- For private packages and unscoped packages, use
npm publish. - For scoped public packages, use
npm publish --access public
- For private packages and unscoped packages, use
Test your Module
Create another directory
mkdir TestMyModule
Switch into the directory
cd TestMyModule
Create your test script – I called mine test.js
var mymodule = require('covid-data-module')
mymodule.printStats()
Create the test node project
npm init
Install your created module
npm install covid-data-module
Execute the test script
node test.js
Sources: https://www.digitalocean.com/community/tutorials/how-to-create-a-node-js-module