This is the sixth day of my participation in the August Text Challenge.More challenges in August

Introduction to the Node. Js

Node.js is a cross-platform JavaScript runtime environment based on Chrome V8 engine.

How do I quickly switch node.js versions on the same device?

  • N: a global NPM open source package, is dependent on NPM to install, use globally.
  • FNM: fast and simple, compatible with. Node-version and. NVMRC files
  • NVM: Standalone package, Node Version Manager

The characteristics of the Node. Js

Asynchronous I/O

When Node performs an IO operation, it returns and resumes the operation after the response, rather than blocking the thread and wasting CPU loops waiting.

Single thread

Node maintains the single-threaded nature of JS in the browser. In the browser, there are browser processes, plug-in processes, GPU processes, and renderers.

  • Advantages: No need to worry about state synchronization, no deadlocks, no performance overhead associated with thread context switching.
  • Disadvantages: Cannot take advantage of multi-core cpus. Errors can cause the entire application to quit, resulting in poor robustness. The CPU cannot continue to execute due to large computing usage.

cross-platform

Compatible withWindowsand*nixPlatform, mainly due to the operating system and node.js upper layer of the construction of a platform architecture. Node is suitable for IO intensive areas.

Modularization mechanism

What is modularity?

To break a large program into small interdependent files based on function or business and splice them together in a simple way.

2. Why modularity? What are the problems without modularity

All script tags must be in the correct order, otherwise they will rely on errors.

Global variables have name conflicts and cannot be reclaimed.

IIFE/namespace can cause a lot of problems with code readability.

CommonJS specification

Node.js supports the CommonJS module specification and loads modules synchronously.

Loading way

  1. Load the built-in module require(‘fs’)
  2. Load file modules with relative or absolute paths
  • require(‘/User/… /file.js’)
  • require(‘./file.js’)
  1. Load NPM package require(‘lodash’)

Npm package lookup principles

For example, require(‘lodash’) would look in the following order:

  • Current directory node_modules

  • If not, ndoe_modules of the parent directory

  • If not, recurse up the path to node_modules in the root directory

  • If package.json.main is not found, the file will be loaded. If package.json is not found, the file will be searched for index.js, index.json, and index.node in sequence

The cache

Require. The cache contains loaded modules. The reason for the cache is synchronous loading

  1. When the file module search time, if every require needs to search again, the performance will be poor;
  2. In real development, modules may contain side effect code.

Other modular specifications

  • AMD is RequireJS in the promotion process of standardized output, asynchronous loading, advocate dependency preloading;

  • CMD is the standardized output of SeaJS in the promotion process, asynchronous loading, advocating the nearest dependence;

  • The UMD specification is compatible with AMD and CommonJS schemas

  • ES Modules(ESM), a language-level modularity specification that is context-independent and compiled with Babel

Package management mechanism

Introduce NPM

NPM is the package manager for Node.js. Js and package.json. A package can contain multiple modules, and a module can depend on multiple packages. Common commands are as follows:

npm init
npm config
npm run [cmd]
npm install [pkg]
npm uninstall [pkg]
npm update [pkg]
npm info [pkg]
npm publish
Copy the code

Here’s a look at what’s in package.json

  • The name package name
  • Version version number
  • Main entry file
  • Scripts Execute scripts
  • Dependencies
  • DevDependencies Develops dependencies
  • Repository code hosts the address

There are many more configurations to discover.

Many packages will be updated frequently, so how to write the version number? Convenient for us to use in development?

You can use thisWeb siteView each version of the package.

Rely on

The bottom two are rarely used

  • Dependencies Packages required for normal application execution after application publication

  • DevDependencies develops dependencies, only for the development environment

  • PeerDependencies are equally dependent, such as a Webpack plug-in that depends on a particular version of Webpack

  • BundledDependencies Package dependencies (NPM run Pack), which must be declared in devDep or DEP

  • OptionalDependencies Optional Dependency

Private NPM

Normal packages are hosted on the nPMJs.com platform, but for some internal packages, such as private situations, companies often have private NPMS that can be hosted on a private platform.

Sometimes download some packages may encounter slow problems, you can set up taobao private agent, improve the download speed.

In addition, there are some cases of locking packages, which can cause compatibility problems if the development cycle is long or a colleague accidentally upgrades his or her own package version. You can use package-lock.json to lock files to prevent automatic version upgrades.

Now the NPM version is 7, after which there are tools such as YARN and PNPM, PNPM solves some problems caused by flattening.

In addition, the course also covers asynchronous programming, Web application development, development debugging, and online deployment. I’ll continue taking notes in the next one.