Construction of basic environment
April 2, 2022
Ming Yuan - ZEPP HEALTH

Preface

Node.js is a platform that uses JavaScript to build fast and scalable server applications. N ode.js is a runtime library, and npm is the package manager of N ode.js modules. In order to let novices build the basic environment needed for development as soon as possible, this doc is specially created. This article mainly focuses on the environment configuration under the Ubuntu Linux.

Use nvm to install and use Node.JS

Nvm is a node.js version management tool. You can install and switch between different versions of node.js. It is recommended to use nvm to download and install node.js.

Download nvm

Detailed doc: https://github.com/nvm-sh/nvm
Please refer to the official doc for specific usage details.

1.Open the terminal and run either of the following two lines of commands.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh

2.Configuration environment variables.

  • Execute the command vim~/.bashrc to enter the file.
image1
  • Press i to enter input mode (also known as edit mode), start editing text, move the cursor to the last line, and add the following fields:

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

image2
  • After pressing the esc key to save, : wq exits.

3.Verify whether the installation is successful

Execute nvm -v , if the output version number indicates that the installation was successful.

Or command -v nvm , if the output nvm means the installation was successful.

Install and switch node.js

1.Enter nvm insatll --lts to install the latest LTS version of node.

image5

2.enter nvm use xxxxxxxx to switch to any version.

Install npm under Linux

NPM is a package management tool installed with Node.JS. It can solve many problems in NodeJS code deployment. The common usage scenarios are as follows:

  • Allows users to download third-party packages written by others from the NPM server for local use.
  • Allows users to download and install command line programs written by others from the NPM server for local use.
  • Allows users to upload packages or command-line programs written by themselves to NPM servers for use by others.

Since the new version of node.js has been integrated with npm , npm has also been installed before. You can also enter the command npm -v to test whether the installation is successful. If the corresponding version number appears, the installation is successful.

Editor recommendation

VS Code Introduction

Visual Studio Code ( VS Code for short) is a free cross-platform source code editor developed by Microsoft. The software supports syntax highlighting, code auto-completion (also known as IntelliSense), code refactoring functions, and has built-in command line tools and Git version control system. It is recommended to use VS Code for software development first.

VS Code Installation

1.Download

Official download link: https://code.visualstudio.com/download

Select the version of .deb to download:

image8

2.Installation

Open end point in the directory where the installation package is located and enter sudo dpkg -i xxxx.deb


$ sudo dpkg -i code_1.66.0-1648620611_amd64.deb

image9

3.Search

Then you can see the VSCode icon on the Ubuntu application page and click to start.

image10
image11

Hello World Code Demo

Try to start creating the simplest Node.js application "Hello World". Create an empty folder called "hello" with cmd, cd into the folder and open the folder with vscode:

mkdir hello
cd hello

Create a file in the hello folder app.js as follows:

image12

Use the node tool to compile and run the app.js. The specific method is cd to enter the file directory where the app.js is located or use shortcut ctrl + ' in vscode to open the integrated end point of vscode (Integrated Terminal) and type the command:

node app.js

The results are as follows:

image13