


An in-depth analysis of the installation method and module system of Nodejs
This article will introduce to you the installation method of Nodejs and the 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 asJAVA
PHP
GO
, but becauseNode# The birth of ## allows front-end developers to use
JSto write server code, so the birth of
Nodecan 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
Vuethat 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
JSon our front-end to contribute to open source. The
npmlibrary has become a A very large code warehouse. In the
npmpackage 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
- NodeJS
We can download it from his
official website.
- 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
.
- You can see that we have installed the latest version of
- node
#16.6.1
.
After installing
- node
- , we can type
node
in the terminal to enter Interactive mode and enter a must-have code for our programmershello world
.
- JS
- file for execution.
echo test>helloWorld.js
rrree
- 我们只需要键入
node
+文件名
即可执行这个文件。
NodeJS模块
-
Node
应用由模块组成,采用的CommonJS
模块规范。每一个文件就是一个模块,拥有自己独立的作用域,变量,以及函数等,对其他的模块都不可见,而文件路径就是模块名,所以我们需要了解不同模块之间是怎么交互怎么互相使用的。 - 模块可以多次加载,但是只会在第一次加载时运行一次,模块加载的顺序,按照其在代码中出现的顺序。
- 在编写每个模块时,都有
require
、exports
、module
三个预先定义好的变量可供使用。
加载(require)
-
require
意为需要的,也就是说我们可以通过require
来引入我们需要的模块,
let x=require('./hello') let y=require('./hello.js')
- 我们可以这样引入一个模块,
require
后面可以接收一个地址,可以是绝对路径也可以是相对路径。 - 值得注意的是,我们的后缀
.js
扩展名可以省略不写。
导出(exports)
-
exports
意为导出,也有一种说法是暴露,我们一般可以使用exports
用于导出模块公有函数和属性。
/* hiNode.js */ exports.addIce=function(){ console.log('我在加冰') }
- 我们在
hiNode.js
文件中暴露了一个函数addIce
,这样我们可以在别的地方require
这个模块并使用该函数。
/* helloWorld.js */ let x=require('./hiNode') x.addIce()
- 我们在终端键入
node helloWorld.js
。
- 成功引入了模块并使用函数。
模块对象(module)
- 在
Node
中我们通过module
可以访问到当前模块的一些信息。
/* hiNode.js */ exports.addIce=function(){ console.log('我在加冰') } console.log(module)
- 可以看到我们打印的
module
里面有它导出的函数,文件名,路径等信息。实际上当我们require
一个模块的时候,它读取的就是该文件的module.exports
变量。 - 这个
module.exports
变量一般是对象的形式如上图,所以我们经常最常用到module
是为了改写module.exports
变量这个导出变量,我们可以改成函数形式。
/* hiNode.js */ module.exports=function(){ console.log('直接可以调用,我是一个函数') }
/* helloWorld.js */ let x=require('./hiNode') x()
- 如上我们直接可以使用
x
是因为require
了一个函数,模块默认导出对象被替换为一个函数。
写在最后
总的来说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!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
