Directory/Contents:

picture

Learn about Node.js

This sentence is put on the homepage of Node.js Chinese website and English official website

Node.js is a JavaScript runtime based on the Chrome V8 engine

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine .

Runtime, the runtime environment of a programming language

NodeJS extends JS, so that JS can be separated from the browser and run directly on the physical machine, which means that NodeJS can directly manage and control machine resources.


Download and install Node

Download the corresponding version directly from the official website and install it

To verify that the installation was successful, you can use the command line

 C:\Users\mild>node -v v16.15.1

Node hello world

Open the command line, node, as follows

 C:\Users\mild>node Welcome to Node.js v16.15.1. Type ".help" for more information. > console.log("hello world") hello world undefined >

exit node

There are three ways

 > (To exit, press Ctrl+C again or Ctrl+D or type .exit)

Double click Ctrl C

Ctrl D

.exit


NPM——(npm,Node Package Manager)

Just like maven, gradle to Java

pip to python

Nuget to C++

npm is a package management tool that ships with node and is often used to download out-of-the-box code for use in personal projects

Check npm version

 > npm -v

package.json

Just like for Java projects managed usingpom.xmlmaven

like describing the 3rd party libraries used in the projectsetup.pyPython

package.jsonUsed to describe and manage plugins, dependencies used in the projectNode


generate apackage.json

Order

 npm init

or use

 npm init -y

Example

C:\Users\mild\Desktop\codings\test>npm init npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead. This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults.  See `npm help init` for definitive documentation on these fields and exactly what they do.  Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file.  Press ^C at any time to quit. package name: (test) version: (1.0.0) description: It is a test init entry point: (index.js) test command: git repository: keywords: author: Ading license: (ISC) About to write to C:\Users\mild\Desktop\codings\test\package.json:  {   "name": "test",   "version": "1.0.0",   "description": "It is a test init",   "main": "index.js",   "scripts": {     "test": "echo \"Error: no test specified\" && exit 1"   },   "author": "Ading",   "license": "ISC" }   Is this OK? (yes) npm notice npm notice New minor version of npm available! 8.11.0 -> 8.15.1 npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.15.1 npm notice Run npm install -g npm@8.15.1 to update! npm notice  C:\Users\mild\Desktop\codings\test>

Generatedpackage.json

 {   "name": "test",   "version": "1.0.0",   "description": "It is a test init",   "main": "index.js",   "scripts": {     "test": "echo \"Error: no test specified\" && exit 1"   },   "author": "Ading",   "license": "ISC" }

in the file is a JSON object where you can customize npm commands"scripts"

{    "test": "echo " Error: no test specified " && exit 1"  },

npm install

Syntax for installing third-party libraries

 npm install <package-name>

The following parameters are usually used in this command:

  • --saveInstall and add entries to the file 'spackage.jsondependencies

  • --save-devInstall and add entries to the file 'spackage.jsondevDependencies

Because the dependencies are divided into the following twopackage.json

  1. devDependenciesUsually tools for development (e.g. libraries for testing)

  2. dependenciesis related to the application in the production environment


npm install -g  Global mode installation, recommended for modules referenced by many projects-g

node_modules

If only runnpm install

Then install it according to the dependencies described in and install it to the current folder/node_modulesnpmpackage.json

(Modules installed in non-global mode will be in the current folder /node_modules )

So where will it be installed in global mode?

This needs to be checked

npm config get prefix

 C:\Users\mild>npm config get prefix C:\software\nodejs\npm_installation_path\node_global

When I saw get, I naturally thought of set

So, to reset the path of the global installation then

npm config set prefix absolute_path

 C:\Users\mild>npm config set prefix C:\data\nodejs\node_global C:\Users\mild>npm config get prefix C:\data\nodejs\node_global

If the absolute path is not used, the relative directory will be set in the current directory


look backnpm install

When importing third-party modules using require in Node code

  1. Node will look for the module in node_modules;

  2. The module also has package.json itself, and also depends on third-party modules, then Node will load the main file defined by the main field

Then such dependencies come and go, and a dependency tree structure will be formed; there must be cases where the same dependencies are depended on by multiple modules, and npm will first count all dependencies, and then install them into the node_modules directory uniformly. (This avoids that the same dependency may be downloaded and installed many times)

npm cache

To avoid downloading modules from Remote Repository every time, npm will have a local cache

View cached address

npm config get cache

Similarly, set the cache address

npm config set cache absolute_path

as follows

C:\Users\mild>npm config get cacheC:\software\nodejs\npm_installation_path\node_cache
C:\Users\mild>npm config set cache C:\data\nodejs\node_cache
C:\Users\mild>npm config get cacheC:\data\nodejs\node_cache

Since it is a cache, it can be cleared

npm cache clean -force

Module versioning andpackage-lock.json

package.jsonThe imported third-party module version can be controlled through the fuzzy version number matching rule , but the dependencies contained in the third-party module version cannot be controlled

In later versions, addednpm5.0.0package-lock.json

The version information of , and the content of the file is automatically generatedpackage.json

In the actual development process, only need to submit

package.jsonandpackage-lock.json

to the code repository, then if you want to install the project environment, you only need to

npm install

using the installation and command line tools (CLI))npmvue3vue3-cli(

Install globally

 # 最新稳定版 vue3 npm install vue@next
C:\Users\mild>npm install vue@nextadded 21 packages in 12snpm noticenpm notice New minor version of npm available! 8.11.0 -> 8.15.1npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.15.1npm notice Run npm install -g [email protected] to update!npm notice

This is to remind me to upgrade , so upgrade directlynpm

npm install -g npm@8.15.1
# 安装vue-clinpm install -g @vue/cli
C:\Users\mild>npm install -g @vue/clinpm WARN deprecated urix@0.1.0: Please see https://github.com/lydell/urix#deprecatednpm WARN deprecated source-map-url@0.4.1: See https://github.com/lydell/source-map-url#deprecatednpm WARN deprecated resolve-url@0.2.1: https://github.com/lydell/resolve-url#deprecatednpm WARN deprecated source-map-resolve@0.5.3: See https://github.com/lydell/source-map-resolve#deprecatednpm WARN deprecated subscriptions-transport-ws@0.11.0: The `subscriptions-transport-ws` package is no longer maintained. We recommend you use `graphql-ws` instead. For help migrating Apollo software to `graphql-ws`, see https://www.apollographql.com/docs/apollo-server/data/subscriptions/#switching-from-subscriptions-transport-ws    For general help using `graphql-ws`, see https://github.com/enisdenjo/graphql-ws/blob/master/README.md
added 848 packages in 2m

It can be seen that there are 5 deprecated warning messages (WARN Deprecated)

A search on the Internet, said to configure Taobao mirror, the command is like this

npm config set registry https://registry.npm.taobao.org

I found that it starts with again . I was curious and got it before setting it up.npm config set

C:\Users\mild>npm config get registryhttp://registry.npm.taobao.org/

What you get is the mirror website of the protocol, so set just changes the protocolhttp

then downloadcnpm

npm install -g cnpm

After installing the Taobao mirror, re-install itvue-cli

cnpm install -g @vue/cli

After the installation is complete, you can check whether the installation is successfulvue-cli

C:\data\nodejs\node_global>vue --version @vue/cli 5.0.8

It may happen that the vue command cannot be found, that is, the vue command is not added to the system path variable, and you can go directly to the folder with the vue command, that is, in the displayed foldernpm config get prefix

A quick way to open system properties: , enterCtrl+Rsysdm.cpl


End of this article

refer to

Node.js Chinese website (nodejs.cn) http://nodejs.cn/

The difference between Node.js and the browser (nodejs.cn) http://nodejs.cn/learn/differences-between-nodejs-and-the-browser

Vue3 official website installation documentation https://v3.cn.vuejs.org/guide/installation.html