The official website of Grape City, grape city for developers to provide professional development tools, solutions and services, enabling developers.
The introduction
An important concept in Node.js is dependency management. This article will take you through the various modes of dependency management and how Node.js loads dependencies. Node.js is very easy to write modularized code, we can use a single JS file to write all the content of the application modularized. Here you may ask what a module is and what it does. In the implementation of large-scale projects, there will be a lot of division of labor and cooperation. To make the division of labor more convenient and smooth, we can encapsulate the code we have written and reuse it or provide it to a third party for use. All modules are organized and compiled into a complete program during the project encapsulation phase. In summary, modules are groups of code that can be easily shared and reused in development. These modules allow us to decompose complex applications. This allows us to better understand the code, find and fix bugs. Based on CommonJS, node. js uses the require keyword to get a JavaScript file.
start
We create a directory for the project, initialize it with NPM init, and create two JavaScript files app.js and appmsg.js.
Now that both.js files are empty, let’s continue updating the appmsgs.js file
Module.exports is used here. This method exposes properties or objects in a given file (appmsgs.js) that can be used in another file. In this case, the file is app.js. Every file in this system has access to module.exports, so some items in the appmsgs.js file are exposed. Here’s how they are used:
Refer to the file using the require keyword, which returns an object representing the modular code snippet when used. We assign it to the variable appMsgs variable, and then use the property in the console.log statement. We get the following output:
Execute JavaScript to construct a return object. This object can be a class constructor, or it can be an object containing many elements or some simple attributes. So, by managing require and Module.exports, we can create these modular applications. The required functionality loads the code and only loads once. If someone else requests this object through require, they will only get a cached version of the object.
Now let’s look at other methods
The code is modified so that instead of exposing an object, the entire function is exported. This code is executed for every function call. Here’s how it works in app.js files
You don’t need to call the property, just execute it like a function. The difference is that every time you execute this code, the code inside the function gets re-executed and here’s what happens
Module. exports (exports, exports, exports, exports, exports
Below is the updated app.js file
This is essentially the same as creating pseudo classes in JavaScript and allowing instances of pseudo classes to be created. Here is the output after the change
Here is another example of this pattern. We create a new file called userrebo.js
Below is app.js and the result of this change
It’s common to use require for individual files, but don’t forget another pattern: dependencies between folders
Folder correlation
Before getting into folder dependencies, let’s take a look at how Nodejs finds dependencies. Don’t forget the previous example:
var appMsgs = require("./appMsgs")
Copy the code
Node.js looks for the appMsgs. Js file and also looks for appMsgs as a directory, logging whichever it finds first. Next we create a folder called Logger and create an index.js file in that folder
The app.js file, which calls this module with require
In this example it is worth noting:
var logger = require("./logger/index.js")
Copy the code
This is absolutely correct, but if you change it to read:
var logger = require("./logger")
Copy the code
Since there is no logger.js in the project, by default when there is a logger directory, index.js is loaded as the starting point for Logger. That’s why we named index.js, the result of this code:
Looking at this, you might be wondering why bother going through the extra steps of creating folders and inex.js. The reason is that we may be composing a complex dependency that may have other dependencies. The logger caller does not need to know that many other dependencies exist. This is a form of encapsulation, and when we build more complex content, we can build them from multiple files while using a single file on the client side. Folders are a good way to manage these dependencies.
Node Package Manager (NPM)
The other thing I want to introduce again is NPM. You must know what it does and it brings a lot of convenience. The method used is also very simple. We can install dependencies using NPM
npm install underscore;
You can then simply require it in app.js
We can see how the red position uses the functionality provided by the underline package. In addition, when we need to use this module, we don’t specify the file path, just use its name, and Node.js will load the module from the Node \ U modules folder in the application
Here is its output
conclusion
This article explained how Nodejs manages its dependencies and saw some patterns that can be used in our application. I hope I can help you with your development and learning.
Develop reading
If you’re already familiar with the content of Node.js, you can also learn about optimizing table performance in web systems.