NodeJS is introduced

What is?

  • Nodejs is a JavaScript (server-side) runtime environment.
  • Based on the JavaScript language, it encapsulates some server-side runtime objects to support the interaction between JavaScript language and operating system.
  • NodeJs is a JavaScript environment based on Chrome’s V8 engine.
Diagram the Runtime environment: OS: NodeJS ____________ hold \ MAC Linux/Windows/money) | | Base Language: JavaScriptCopy the code

What is the difference between NodeJS and JavaScript?

  • Nodejs is a JavaScript runtime environment (platform), and JavaScript is a programming language.

NodeJS installation

Installation Methods summary

  • Method 1: Download the binary installation file.
  • Method 2: Install using system package management.
  • Method 3: Compile the source code.
  • After a successful installation, the Node command is automatically added to our system environment variable path.
  • We use the node command to access the NodeJS executable command-line tool.

Only install NodeJS

  • Installation address: nodejs.cn/download/
  • You do not need to select any option during installation.
  • Check the version.
node -v
npm -v
Copy the code

Install NodeJS based on NVM

What is NVM?

  • Node Version Manager (NVM) is a node version management tool.

What is NPX?

  • Incidentally: NPX is a new command added after npm@5.
  • This allows us to use some CLI functions without installing modules in the current environment.
  • Usage Scenarios:
Create-react-app is installed globally
npm i -g create-react-app
create-react-app some-repo
# create-react-app is not installed in the project or globally at this point.
npx create-react-app some-repo
Copy the code

1. Install the NVM on Windows

  • Quick installation address:Github.com/coreybutler…, the choice ofnvm-setup.zipInstall.
# NVM common command
View all existing versions of Node
nvm ls-remote
NVM install 6.14.4
nvm install <version>
# Enable the specified version of Node, such as NVM use 6.14.4
nvm use <version>
Uninstall the specified version of Node, such as NVM uninstall 6.14.4
nvm uninstall <version>
# Check all node versions locally
nvm ls
# Check the current node version
nvm version
Copy the code
  • Download not fluent can replace Taobao image.
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
Copy the code
  • Check the installed NodeJS version and NPM version.
node -v
npm -v
Copy the code

2. Install the NVM on a MAC

1.2.1 installation NVM

  • Installation tutorial :github.com/nvm-sh/nvm/…
# macThe curl - o - https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash# linuxWget - o - https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bashCopy the code
  • nvmthebash_profileWrite the following configuration in the file
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Copy the code

NodeJS low-level dependencies

  • The main dependent submodules have the following contents:
    • V8 engine: is mainly the parsing of JS grammar, with it to identify JS grammar.
    • libuv: C language implementation of a high-performance asynchronous non-blocking IO library, used to implement node.js event loop.
    • http-parser/llhttp: The bottom layer processes HTTP requests, packets, and request packets.
    • openssl: Processing encryption algorithms, a wide range of frameworks.
    • zlib: Handles compression and other content.

NodeJS built-in module

  • The main thing about Node.js is that it implements a set of CommonJS modularity specifications, with some common modules built in.
    • fs: a file system that reads and writes data from disks in the current installed system
    • path: path system, which can handle problems between paths.
    • crypto: encryption-related modules that can encrypt our content in a standard way.
    • dns: Handle DNS related content, such as we can set up DNS server and so on.
    • http: Set up an HTTP server, send HTTP requests, listen for responses, and so on.
    • readline: Reads a line from stdin. It can read, add, or delete content from our command line.
    • osOperating system level apis, such as telling us the current system type and parameters.
    • vm: a virtual machine module dedicated to handling sandbox, the bottom layer is mainly to call v8 related apis for code parsing.

CommonJS

CommonJS profile

  • Each file is equivalent to a module.
  • Each module has its own scope.
  • Variables/functions and classes inside modules are private and invisible to the outside world.

CommonJS case

  • Module exports :module.exports
function add(a,b){  console.log(a+b) }
function decrease(a,b){  console.log(a-b) }
module.exports = {  add,  decrease }
Copy the code
  • Module introduction :require
let cal = require('./calculate')
cal.add(10.5) cal.decrease(100.50)
Copy the code

CommonJS introduction and source code analysis

  • V8 engine: github.com/v8/v8
  • chromium.googlesource.com/v8/v8.git

NodeJS peripheral tools

1. debug

  • Breakpoints can be used for step by step debugging to print variable results in the current context to locate where specific problems occur.
  • Specific: we can debug node.js step by step using vscode or our own break points.
  • For JS memory leaks, we can also print each memory snapshot with the help of a tool to compare the problem in the code.

2. quickjs

  • Quickjs is a JS parsing engine with a light weight and a small amount of code, similar to the V8 engine.
  • Its biggest characteristic is very light weight, which can also be reflected from the source code, its main characteristics and advantages:
    • Lightweight and easy to embed: just a few C files, no external dependencies, a simple “Hello Word” program on x86 costs 180 KiB.
    • Extremely low startup time and fast interpreter: run the ECMAScript test suite 15,600 times in about 100 seconds on a single-core desktop PC. The runtime instance’s life cycle completes in less than 300 milliseconds.
    • Nearly complete implementation of ES2019 support, including: modules, asynchronous generators and full Annex B support (traditional Web compatibility). Many of the new initiatives introduced in ES2020 will also continue to be supported.
    • Pass 100% ECMAScript Test Suite testing.
    • Javascript sources can be compiled into executable files with no external dependencies.

Deno vs. NodeJS

  • Similarities:
    • Deno is also based on Chrome’s V8 engine and is a JavaScript runtime environment that encapsulates some system-level calls.
    • Deno apps can also be developed using JavaScript.
  • Difference:
    • Deno develops some upper-level modules based on Rust and typescript, so we can write TS directly in deno applications.
    • Deno supports URL loading modules and features such as top level await.
// Top level await [email protected],deno default support.
const strings = await import(`/i18n/${navigator.language}`);
Copy the code