Table of Contents
About NodeJS
NodeJS模块
加载(require)
导出(exports)
模块对象(module)
写在最后
Home Web Front-end JS Tutorial An in-depth analysis of the installation method and module system of Nodejs

An in-depth analysis of the installation method and module system of Nodejs

Aug 17, 2021 am 10:25 AM
nodejs Install Module system

This article will introduce to you the installation method of Nodejs and the module system of Nodejs.

An in-depth analysis of the installation method and module system of Nodejs

About NodeJS

Node.js is a Javascript runtime environment based on the Chrome V8 engine. [Recommended learning: "nodejs Tutorial"]

  • In our daily development, most Web projects use the front endJS is written, and the backend is written in server-side language, such as JAVA PHP GO, but because Node# The birth of ## allows front-end developers to use JS to write server code, so the birth of Node can be said to make the front-end shine, and both the front-end and the back-end blossom.
  • Especially
  • Node provides the basis for the development environment to run. Front-end frameworks like Vue React that we usually use have become very Powerful, it can be said to be an essential basic device for the front end.
  • Node There are more people using it, and more and more people are using JS on our front-end to contribute to open source. The npm library has become a A very large code warehouse. In the npm package management system, we can find the plug-ins and wheels we need. We can use them directly, which also saves us developers money. A lot of valuable time.
  • The introduction and function of more
  • NodeJS has been introduced very clearly in this article. If you are interested, you can take a look at Portal.

NodeJS installation

  • NodeJSWe can download it from his official website.

An in-depth analysis of the installation method and module system of Nodejs

    Choose the appropriate version for you to download. Since my computer has already installed it, I won’t demonstrate it anymore. Just open the installation program and go to the next step. Okay, if you want to see a more detailed installation guide and global configuration guide, you can see this article:
  • Node.js Installation and Environment Configuration for Windows
  • After installation, we need to check If the installation is successful, you can open the terminal and enter
  • node -v.

An in-depth analysis of the installation method and module system of Nodejs

    You can see that we have installed the latest version of
  • node #16.6.1.
NodeJS uses

After installing
    node
  • , we can type node in the terminal to enter Interactive mode and enter a must-have code for our programmers hello world.

An in-depth analysis of the installation method and module system of Nodejs

Of course we can also create a
    JS
  • file for execution.
    echo test>helloWorld.js
    Copy after login
    rrree

  • 我们只需要键入node+文件名即可执行这个文件。

NodeJS模块

  • Node应用由模块组成,采用的CommonJS模块规范。每一个文件就是一个模块,拥有自己独立的作用域,变量,以及函数等,对其他的模块都不可见,而文件路径就是模块名,所以我们需要了解不同模块之间是怎么交互怎么互相使用的。
  • 模块可以多次加载,但是只会在第一次加载时运行一次,模块加载的顺序,按照其在代码中出现的顺序。
  • 在编写每个模块时,都有requireexportsmodule三个预先定义好的变量可供使用。

加载(require)

  • require意为需要的,也就是说我们可以通过require来引入我们需要的模块,
let x=require('./hello')
let y=require('./hello.js')
Copy after login
  • 我们可以这样引入一个模块,require后面可以接收一个地址,可以是绝对路径也可以是相对路径。
  • 值得注意的是,我们的后缀.js扩展名可以省略不写。

导出(exports)

  • exports意为导出,也有一种说法是暴露,我们一般可以使用exports用于导出模块公有函数和属性。
/* hiNode.js */
exports.addIce=function(){
  console.log('我在加冰')
}
Copy after login
  • 我们在hiNode.js文件中暴露了一个函数addIce,这样我们可以在别的地方require这个模块并使用该函数。
/* helloWorld.js */
let x=require('./hiNode')
x.addIce()
Copy after login
  • 我们在终端键入node helloWorld.js

An in-depth analysis of the installation method and module system of Nodejs

  • 成功引入了模块并使用函数。

模块对象(module)

  • Node中我们通过module可以访问到当前模块的一些信息。
/* hiNode.js */
exports.addIce=function(){
  console.log('我在加冰')
}
console.log(module)
Copy after login

An in-depth analysis of the installation method and module system of Nodejs

  • 可以看到我们打印的module里面有它导出的函数,文件名,路径等信息。实际上当我们require一个模块的时候,它读取的就是该文件的module.exports变量。
  • 这个module.exports变量一般是对象的形式如上图,所以我们经常最常用到module是为了改写module.exports变量这个导出变量,我们可以改成函数形式。
/* hiNode.js */
module.exports=function(){
  console.log('直接可以调用,我是一个函数')
}
Copy after login
/* helloWorld.js */
let x=require('./hiNode')
x()
Copy after login
  • 如上我们直接可以使用x是因为require了一个函数,模块默认导出对象被替换为一个函数。

An in-depth analysis of the installation method and module system of Nodejs

写在最后

总的来说NodeJS应用是由模块组成的,我们可以在js文件导出exports函数等变量,在另一个js文件进行导入require这个模块。

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of An in-depth analysis of the installation method and module system of Nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
Is nodejs a backend framework? Is nodejs a backend framework? Apr 21, 2024 am 05:09 AM

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

What are the global variables in nodejs What are the global variables in nodejs Apr 21, 2024 am 04:54 AM

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

What is the difference between npm and npm.cmd files in the nodejs installation directory? What is the difference between npm and npm.cmd files in the nodejs installation directory? Apr 21, 2024 am 05:18 AM

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

How to connect nodejs to mysql database How to connect nodejs to mysql database Apr 21, 2024 am 06:13 AM

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

Is nodejs a back-end development language? Is nodejs a back-end development language? Apr 21, 2024 am 05:09 AM

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

Can nodejs write front-end? Can nodejs write front-end? Apr 21, 2024 am 05:00 AM

Yes, Node.js can be used for front-end development, and key advantages include high performance, rich ecosystem, and cross-platform compatibility. Considerations to consider are learning curve, tool support, and small community size.

Is there a big difference between nodejs and java? Is there a big difference between nodejs and java? Apr 21, 2024 am 06:12 AM

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Which one to choose between nodejs and java? Which one to choose between nodejs and java? Apr 21, 2024 am 04:40 AM

Node.js and Java each have their pros and cons in web development, and the choice depends on project requirements. Node.js excels in real-time applications, rapid development, and microservices architecture, while Java excels in enterprise-grade support, performance, and security.

See all articles