Home  >  Article  >  Web Front-end  >  Let’s talk about the installation and configuration files of Node.js

Let’s talk about the installation and configuration files of Node.js

PHPz
PHPzOriginal
2023-04-17 15:29:29704browse

Node.js is an open source cross-platform JavaScript runtime environment that allows JavaScript to run on the server. Node.js uses an event-driven, non-blocking I/O model and can be used to build fast and scalable network applications. . This article will introduce the installation and configuration files of Node.js.

1. Installation of Node.js

1.1 Download from the official website

Node.js official website provides installation packages for Windows, Mac and Linux systems. Users can follow Select the corresponding installation package for your system to download. The URL is: https://nodejs.org.

1.2 Install using NVM

NVM (Node Version Manager) is a tool for managing and switching multiple Node.js versions, which can help users use different versions in different environments more conveniently. Node.js. The use of NVM is also very simple. Just enter the following command in the terminal to install:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

After the installation is complete, you can check the version of nvm through the following command:

nvm --version

1.3 Environment variable configuration

In Windows systems, after installing Node.js, you need to add the path of Node.js to the environment variable PATH before you can use the Node.js command. In Windows 10, you can use the following methods to configure environment variables:

  1. Right-click "This PC (My Computer)" on the desktop;
  2. Select "Properties";
  3. Click "Advanced System Settings";
  4. Click "Environment Variables";
  5. Find the "Path" variable in "System Variables" and click "Edit";
  6. Click "New" in the editing window and enter the Node.js installation path;
  7. Click "OK" to save the settings.

2. Configuration file

After installing Node.js, you need to make some configurations to better use Node.js.

2.1 npm configuration

npm (Node Package Manager) is the default package management tool for Node.js, which can be used to install and manage Node.js modules. The configuration file of npm is ~/.npmrc (Linux/Mac) or %userprofile%.npmrc (Windows). Users can configure npm according to their own needs.

2.1.1 Modify the mirror source

Since the default npm mirror source may cause problems such as slow speed and download failure when used in China, you can modify the mirror source to the domestic Taobao source. or cnpm source. Enter the following command in the terminal to modify the mirror source:

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

2.1.2 Report errors

Some errors may occur when installing modules with npm. You can report the errors to the development through the following configuration Author:

npm set loglevel=http

2.1.3 Set the Node.js installation path

In Windows systems, npm will install the globally installed Node.js module in the C drive by default, and the C drive usually It is a system disk, which may cause problems such as system lag, so you can modify the npm installation path to another drive letter. Enter the following command in the terminal to modify the installation path of npm:

npm config set prefix "D:\Program Files\nodejs\node_global"  # 修改全局包安装路径
npm config set cache "D:\Program Files\nodejs\node_cache"  # 修改缓存路径

2.2 Node.js configuration

The configuration file of Node.js is ~/.bashrc (Linux) or %userprofile %.bash_profile (Mac) or %userprofile%.bashrc (Windows), you can add custom Node.js environment variables to this file.

2.2.1 Setting NODE_ENV

NODE_ENV is an important environment variable in Node.js, which can be used to distinguish the development environment from the production environment. In Linux and Mac systems, NODE_ENV can be set in the following ways:

export NODE_ENV=production

In Windows systems, NODE_ENV can be set by adding the following code in ~/.bashrc or %userprofile%.bash_profile:

set NODE_ENV=production

2.2.2 Add alias

You can set aliases for commonly used commands in the terminal to make it more convenient for users to use. For example, you can set an alias for the node command:

In Linux and Mac systems:

alias n=node

In Windows systems:

doskey n=node

The above is the Node.js introduced in this article Installation and configuration files, I hope it will be helpful to everyone's learning.

The above is the detailed content of Let’s talk about the installation and configuration files of Node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn