Kā modulārā programmēšana darbojas Node.js

Moduļi ir viena no Node.js pamatīpašībām.

Veidojot lietojumprogrammu, kodam kļūstot arvien sarežģītākam, visu kodu nevar ievietot vienā failā.

Kad tas kļūst nepārvaldāms, varat izmantot mezgla moduļa modeli, lai rakstītu dažādus failus un eksportētu tos (ieskaitot funkcijas, objektus un metodes) uz galveno failu.

Tagad jūs varētu jautāt - kas īsti ir a module?

Vienkārši sakot, a modulenav nekas cits kā JavaScript fails. Tieši tā.

Izmantojot mezgla modulāro funkcionalitāti, mēs varam importēt savus ārējos failus, galveno (vietējo) mezglu moduļus un NPM moduļus. Šajā rakstā mēs detalizēti apspriedīsim katru no šiem.

Kā importēt savus failus

Šajā rakstā mēs apspriedīsim, kā mēs varam eksportēt un importēt savus failus.

Būtībā ir divi faili: calculate.jsno kurienes mēs eksportēsim un main.jsuz kuru importēsim šo failu.

mduleexport.png

Mums abi faili ir vienā mapē, lai tas būtu vienkārši.

image.png

Kā importēt funkciju

//---- Exported file [calculate.js] ---- const add = (a,b)=>{ return a + b } module.exports = add 

Šeit mēs eksportējam funkciju sauc add, izmantojot module.exports. Tad šī funkcija tiek importēta citā failā, izmantojot requiremetodi.

Mezglā katrs fails tiek dēvēts par a moduleun exportsir objekta moduļa īpašums.

Tagad mēs varam izsaukt funkciju citā failā, tas ir main.js, nododot argumentus, kā parādīts zemāk.

//------ Main File[main.js] ---- const add = require('./calculate') //name of the desired file const result = add(2,4) console.log(result); //Output : 6 

Kā importēt objektu

Mēs varam arī eksportēt visu objektu un piekļūt tajā esošajām dažādajām metodēm.

//---- Exported file [calculate.js] ---- const add = { result : (a,b)=>{ return a + b } } module.exports = add 

Mēs eksportējām objektu addun importējām to uz mūsu galveno failu, izmantojot requiremetodi.

Tagad mēs varam piekļūt objekta resultmetodei, addizmantojot .punktu operatoru:

//---- Main file[main.js] ---- const add = require('./calculate') const result = add.result(5,8) console.log(result) //Output : 13 

Vēl viens veids, kā mēs varam eksportēt iepriekš minēto objektu, ir tikai eksportēt mums nepieciešamo metodi, nevis visu objektu.

//---- Exported file [calculate.js] ---- const add = { result : (a,b)=>{ return a + b } } module.exports = add.result 

Kā redzat, mēs importējam resultmetodi addobjektā. Tātad šo metodi var tieši izmantot galvenajā failā.

Tā ir laba prakse, ja jums nav nepieciešams viss objekts, bet jums ir nepieciešamas tikai dažas tā metodes / funkcijas. Tas arī padara mūsu kodu drošāku.

//---- Main file[main.js] ---- const add = require('./calculate') const result = add(5,8) console.log(result) //Output : 13 

Kā importēt funkciju veidotāju:

Funkciju konstruktoru galvenokārt izmanto, lai izveidotu jaunu objekta instanci, kurai ir tādas pašas īpašības kā galvenajam objektam / funkcijai.

Šajā gadījumā mēs izveidojam jaunu objekta “Pievienot” gadījumu, izmantojot newatslēgvārdu. Šo procesu, kurā mēs izveidojam objekta gadījumu, sauc par “instantiation”.

Tad mēs eksportējam šo gadījumu, izmantojot module.exports:

//---- Exported file [calculate.js] ---- function Add (){ this.result = (a,b)=>{ return a + b } } module.exports = new Add() 

Tagad mēs varam to importēt mūsu galvenajā failā un piekļūt tā rezultāta metodei, lai iegūtu aprēķināto vērtību.

//---- Main file[main.js] ---- const add = require('./calculate2') const result = add.result(1,3) console.log(result); //Output : 4 

Tādā veidā mēs varam eksportēt un importēt funkciju konstruktoru.

Ir vēl viens veids, kā mēs to varam izdarīt, tas ir, izveidojot jauno instanci galvenajā failā, nevis eksportētajā failā, kā parādīts iepriekš module.exports = new Add().

Mēs redzēsim, kā tas darbojas, eksportējot ES6 klases, kas darbojas līdzīgi funkciju konstruktoriem.

Kā importēt ES6 klases

classir īpaša veida funkcija, kur classatslēgvārds palīdz to inicializēt. Tā izmanto constructormetodi, lai saglabātu īpašības.

Tagad mēs eksportēsim visu, classizmantojot module.exports:

//---- Exported file [calculate.js] ---- const Add = class{ constructor(a,b){ this.a = a; this.b = b; } result(){ return this.a + this.b } } module.exports = Add; 

Tagad mūsu galvenajā failā mēs izveidojam jaunu gadījumu, izmantojot newatslēgvārdu, un piekļūstam resultmetodei, lai iegūtu aprēķināto vērtību.

 //---- Main file[main.js] ---- const add = require('./calculate') const result = new add(2,5) console.log(result.result()); //Output : 7 

Kā importēt mezglu pamata (vietējos) moduļus

Tā vietā, lai katru reizi izveidotu savus pielāgotos moduļus, Node nodrošina moduļu komplektu, kas atvieglo mūsu dzīvi.

Mēs apspriedīsim dažus moduļus, taču visu sarakstu varat atrast oficiālajā mezgla API dokumentā šeit.

Mezglu moduļu importēšana ir līdzīga tam, kā jūs importējat savus moduļus. Jūs izmantojat to pašu require()funkciju, lai piekļūtu tai savā failā.

But there are some modules which you may have used unknowingly which do not need to be imported. For example console.log() – we have used the console module many times without fetching it in our own local file as these methods are available globally.

Let's look at one of the Core Native Modules which is File System (fs).

There are n number of operations we can perform with the file system module such as reading a file, writing a file, and updating it, to name a few.

We are going to use the fs module to read a file. Even in this method, there are two ways we can perform this action: one by using the synchronous function fs.readFileSync(), and the other by asynchronous function fs.readFile().

We'll discuss synchronous-asynchronous Node functions in future posts.

Today, we'll use the asynchronous version, that is fs.readFile().

For this example, we have created two files: main.js, where we are going to perform the file reading operation, and file.txt which is the file we are going to read.

image.png

Thefile.txt contains some text in it.

Hello World!

Now, we use the fs module to read the file, without importing it, as shown below:

fs.readFile('./file.txt','utf-8',(err,data)=>{ if (err) throw err console.log(data); })

It will throw an error as fs is not defined. That is because the file system fs module is not available globally like the console module is.

ReferenceError: fs is not defined at Object. (C:\Users\Sarvesh Kadam\Desktop\Training\blog\code snippets\Node Modular Pattern\main.js:3:1) at Module._compile (internal/modules/cjs/loader.js:1256:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1277:10) at Module.load (internal/modules/cjs/loader.js:1105:32) at Function.Module._load (internal/modules/cjs/loader.js:967:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) at internal/main/run_main_module.js:17:47 

Therefore, we need to import all the data from the file system module using the require() function and store all that data in a variable fs.

const fs = require('fs') fs.readFile('./file.txt','utf-8',(err,data)=>{ if (err) throw err console.log(data); })

Now you can name that variable anything. I named it fs for readability and it's the standard which most developers follow.

Using the fs variable we can access the readFile() method where we passed three arguments Those arguments are file path, character encoding utf-8, and the callback function to give an output.

You might ask why we're passing utf-8 as our argument in the readFile()?

Because it encodes the value and gives the text as an output rather than giving a buffer as shown below:

The callback function, in turn, has two arguments: an error (err) and the actual content in the file (data). Then we print that data in the console.

//Output: Hello World! 

How to Import NPM Modules

So what exactly is Node Package Manager?

Pakotne ir koda daļa, kuru pārvalda pakešu pārvaldnieks. Tas nav nekas cits kā programmatūra, kas pārvalda pakotņu instalēšanu un atjaunināšanu.

NPM saskaņā ar oficiālo dokumentāciju:

NPM ir pasaulē lielākais programmatūras reģistrs. Atvērtā pirmkoda izstrādātāji no visiem kontinentiem izmanto koplietošanas un aizņemšanās paketes, izmantojot daudzas stundas, un daudzas organizācijas izmanto kopējo vērtību, lai pārvaldītu arī privāto attīstību.

Tātad NPM mēs izmantojam kāda cita atvērtā pirmkoda kodu, kuru pārvalda NPM, importējot to mūsu projektā.

NPM parasti tiek piegādāts ar Node JS, kad to lejupielādējat. Jūs varat pārbaudīt, vai NPM ir instalēts jūsu datorā, vienkārši palaižot komandu npm -vkomandrindā. Ja tas atgriež kādu versijas numuru, tas nozīmē, ka NPM ir veiksmīgi instalēts.

NPM reģistrs atrodas vietnē npmjs.com, kur varat atrast paketes, kuras varat izmantot.

Let's look at one of the packages called chalk which is basically used for terminal styling.

chalknpm2.jpg

In the above figure, we can see the weekly downloads of the package which suggests how popular is it.

Also, you can see that this package has dependencies in it. So this module which will serve as a dependency on our project is itself dependent on other modules.

This entire management process is taken care of by the Package Manager.

Even the source code is which is present on GitHub is given to us. We can navigate to it and verify if there are any open issues present.

One more thing before moving forward: the NPM packages come in different versions. The pattern which the version follows is semantic versioning.

As you can see, the latest version of the chalk module when I wrote this article is 4.1.0.

It follows the semantic versioning Major_changes.Minor_changes.Patch pattern.

Major_changes, as the name stands, are the significant changes made on the module which might affect your existing code.

Minor_changes are new enhancements or features along with defect fixes that have been added which should not affect your existing code.

Patch is the small bug fixes that will not crash your existing code.

You can learn more about semantic versioning on semver.org.

How to Install NPM

Now to import any package from NPM, you first need to initialize NPM on your local project folder by running the command on the command prompt:

npm init 

Once you run the above command, it will ask you for some data as shown below such as package name, version, and so on.

Much of this data can be kept as default as mentioned in the Round brackets ().

Also, the fields such as author and license are for the folks who created those NPM packages.

On the other hand, we are just importing and using them to create our own application.

package name: (code_npm) code_npm version: (1.0.0) 1.0.0 description: npm demo entry point: (index.js) index.js test command: test git repository: keywords: npm test author: Sarvesh license: (ISC) 

Once you enter all the fields, it will create a JSON file with values that have the above properties, and it'll ask you for confirmation like this:

Is this OK? (yes) yes 

Once you've confirmed yes it will create a package.json file with all the data you entered as illustrated below:

{ "name": "code_npm", "version": "1.0.0", "description": "npm demo", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "npm", "test" ], "author": "Sarvesh", "license": "ISC" } 

Also, you can see a script object that has a test property added. You can run it using the npm test command and it will give back the desired output like this:

"Error: no test specified" 

Now instead of doing this elongated method of initializing NPM and entering the custom properties values, you can simply run the command:

npm init -y 

Once you run this command, it will directly create a package.json file with the default values.

pkgjson.PNG

Now to install the latest version of the chalk package in your project, you need to execute the command:

npm install chalk 

You can also install any specific version you need of chalk by just adding @version number as shown below. Also instead of install you can simply put the short-hand i flag which stands for installation:

npm i [email protected] 

This will install two things, a node_modules folder, and a package-lock.json file.

folderdir.PNG

Also, it will add a new property called dependencies to our package.json file which contains the name of the package installed and its version.

"dependencies": { "chalk": "^4.0.0" } 

The node_module folder contains the packages folder and its dependency's folders. It gets modifies as and when the npm package gets installed.

The package-lock.json contains the code which makes NPM faster and more secure.

"chalk": { "version": "4.0.0", "resolved": "//registry.npmjs.org/chalk/-/chalk-4.0.0.tgz", "integrity": "sha512-N9oWFcegS0sFr9oh1oz2d7Npos6vNoWW9HvtCg5N1KRFpUhaAhvTv5Y58g880fZaEYSNm3qDz8SU1UrGvp+n7A==", "requires": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" } 

It mainly contains properties such as version, which is the semantic version number.

The resolved property is the directory or location from which the package was fetched. In this case it was fetched from chalk.

The integrity property is to make sure that we get the same code if we install the dependency again.

The requires object property represents the dependency of the chalk package.

Note: Do not make any changes to these two files node_modules and package-lock.json

How to Use NPM

Now once we've installed chalk to our project, we can import it to our root project file using the require() method. Then we can store that module in a variable called chalk.

const chalk = require('chalk') console.log(chalk.red("Hello World"))

Using the red() method of the chalk package, we have styled the "Hello World" text color in red.

On running the command node index.js we get the following output:

chalkop.png

Now there are many ways you can style your command line output using the chalk package. For more information you can refer to the Chalk official document on NPM.

Also, you can install the NPM packages globally (that is, on our operating system) rather than installing it in your local project by adding the -g flag on the command line (which stands for global, as mentioned below):

npm i nodemon -g 

This global package will not affect our package.json in any way since it is not installed locally.

We have installed the nodemon package globally which is used for automatic restart of a Node application when file changes in the directory are observed.

You can refer to nodemon for more information.

We can use the nodemon package by running the application using this command:

nodemon index.js 

It works similarly to node index.js, except it keeps an eye on the file changes and it restarts the application once changes are detected.

[nodemon] 2.0.6 [nodemon] to restart at any time, enter `rs` [nodemon] watching path(s): *.* [nodemon] watching extensions: js,mjs,json [nodemon] starting `node index.js` Hello World 

Note: The chalk styling will probably not work when you used nodemon.

Finally, we will go through the dev dependencies. There are some NPM packages or modules which we won't need in our project's production environment, but only for our development requirements.

We can install these modules in our project using the dev flag as shown below:

 npm i nodemon --save-dev 

It then creates a new property in the package.json called devDependencies:

"devDependencies": { "nodemon": "^2.0.6" } 

Conclusion

Using Node's Module Pattern, we can import from our own files by exporting them in form of functions, objects, function constructors, and ES6 classes.

And Node has its own set of Core (Native) Modules which we can use. Some of them are available globally, while some of them need to be imported locally in your project/folder.

NPM is a package manager that manages 3rd party open source code which we can use in our project. Before using NPM modules, you need to initialize NPM locally using npm init on your command line in the root of your project folder.

Izmantojot komandu, varat instalēt jebkuru NPM pakotni npm i . NPM pakotni var instalēt globāli, izmantojot -gkarodziņu. Arī paketi var padarīt atkarīgu no attīstības, izmantojot --save-devkarodziņu.

Paldies par lasīšanu! Ja jums patīk šis raksts, sazinieties ar mani Twitter vietnē, kad es turpinu dokumentēt savu mācīšanos.