Please indicate the source for reprinting: Grape City official website, Grape City provides developers with professional development tools, solutions and services, and empowers developers.
introduction
An important concept in Node.js is dependency management. This article will take you to understand the various modes of dependency management and how Node.js loads dependencies.
It is very simple to write modular code in Node.js. We can use a single js file to non-modularize the content of all applications.
Here you may ask, what is a module and what does it do.
In the implementation of large projects, there will be a lot of division of labor and collaboration. In order to make the division of labor more convenient and smooth, we can encapsulate the written code, reuse it or provide it to a third party. Organize and compile all modules into a complete program in the packaging stage of the project.
All in all, a module is a grouping of code to facilitate sharing and reuse in development. These modules allow us to decompose complex applications. So that we can better understand the code, find and fix bugs. Based on CommonJS, the require keyword is used in Node.js to obtain a JavaScript file.
begin
We created a directory for the project, initialized it with npm init, and created two JavaScript files, app.js and appMsg.js.
At this point, the two .js files are empty, let's continue to update the appMsgs.js file
Here you can see the usage of module.exports, which exposes attributes or objects in a given file (appMsgs.js), which can be used in another file. In this example, the file is app.js
In this system, every file can access module.exports, so some items in the appMsgs.js file are public. The following is a display of the specific use of these contents:
Use the require keyword to refer to the file, and when used, it will return an object that represents the modular code segment. We assign it to the variable appMsgs variable, and then use the attribute in the console.log statement. Get the following output:
Execute JavaScript and construct a return object. This object can be a class constructor, or it can be an object that contains many elements or some simple attributes.
Therefore, by managing require and module.exports, we can create these modular applications.
The required function loads the code and only loads it once. If others request this object through require, they will only get the cached version of this object.
Next look at other methods
Modify the code to no longer expose an object, but export the entire function. Every function call will execute this code
Here is how it is used in the app.js file
No need to call properties, just like executing functions. The difference with function execution is that every time this code is executed, the code in the function will be re-executed
The following is the result of the operation
The above are the two modes of module.exports and their differences. In another common mode, we need to know how to use it as a constructor
Below is the updated app.js file
Essentially, this is the same as creating pseudo-classes in JavaScript and allowing instances of pseudo-classes to be created. The following is the output after the change
The following is another example of this pattern
We create a new file called userRepo.js
Below is the execution result of app.js and this change
It is very common to use require for a single file, but don’t forget another mode: the dependencies between folders
Folder relevance
Before officially introducing folder dependencies, let's first understand how Nodejs finds dependencies. Don't ignore this content in the previous example:
var appMsgs = require("./appMsgs")
Node.js will look for the appMsgs.js file, and will also look for appMsgs as a directory, and record it no matter which one it finds first.
Then we create a folder named logger and create an index.js file in this folder
app.js file, it uses require to call this module
It is worth noting in this example:
var logger = require("./logger/index.js")
The content is completely correct, but if you change it to the following content:
var logger = require("./logger")
Because there is no logger.js in the project, when there is a logger directory, index.js will be loaded as the starting point of the logger by default. This is why we named index.js, the result of this code:
Seeing this, you may be wondering why bother to complete the extra steps of creating a folder and inex.js?
The reason is that we may be combining a complex dependency, and this dependency may have other dependencies. The caller of the logger does not need to know that there are many other dependencies.
This is a form of encapsulation. When we build more complex content, we can build them with multiple files and use a single file on the user side. Folders are a great way to manage these dependencies.
Node Package Manager (NPM)
Another content to be introduced again is NPM. You must understand its functions and bring a lot of convenience. The method used is also very simple.
We can install dependencies using npm
npm install underscore;
Then you can simply require in app.js
We can see how to use the functions provided by the underscore package at the position marked in red. In addition, when we need to use this module, we do not specify the file path, just use its name, Node.js will load this module from the node\u modules folder in the application
Below is its output
to sum up
This article introduced how Nodejs manages its dependencies and saw some patterns that can be used in our application. I hope to help you in your development and learning.
Further reading
Welcome to download and try the pure front-end table control SpreadJS to optimize the performance of the table function module in the web system.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。