📜  cai nodejs ubuntu - Javascript (1)

📅  最后修改于: 2023-12-03 15:14:05.326000             🧑  作者: Mango

Introduction to Node.js on Ubuntu

Introduction

Node.js is a powerful JavaScript runtime built on Chrome's V8 JavaScript engine. It allows you to build scalable and high-performance network applications. In this guide, we will explore how to install and use Node.js on Ubuntu.

Table of Contents
Installing Node.js on Ubuntu
Method 1: Using the NodeSource repository
  1. Begin by updating the package index:
$ sudo apt update
  1. Install the required dependencies:
$ sudo apt install curl dirmngr apt-transport-https lsb-release ca-certificates
  1. Add the Node.js repository using the NodeSource setup script:
$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
  1. Install Node.js and npm:
$ sudo apt install nodejs
  1. Verify the installation by checking the Node.js and npm versions:
$ node -v
$ npm -v
Method 2: Using the Ubuntu package manager
  1. Update the package index:
$ sudo apt update
  1. Install Node.js and npm using the package manager:
$ sudo apt install nodejs npm
  1. Verify the installation:
$ node -v
$ npm -v
Creating a Simple Node.js Application

To create a simple Node.js application, follow these steps:

  1. Create a new directory for your application:
$ mkdir myapp
$ cd myapp
  1. Initialize the project with a package.json file:
$ npm init -y
  1. Create a file named app.js and write your Node.js code in it.

  2. Run your Node.js application:

$ node app.js
Using npm (Node Package Manager)

npm is the default package manager for Node.js. You can use it to install packages or modules for your Node.js applications. Here are a few useful npm commands:

  • Install a package: npm install package_name
  • Install a package as a development dependency: npm install package_name --save-dev
  • Uninstall a package: npm uninstall package_name
  • Update a package: npm update package_name
  • Search for packages: npm search package_name

For more details, refer to the npm documentation.

Conclusion

In this guide, we have covered the installation of Node.js on Ubuntu and how to create a simple Node.js application. Additionally, we introduced npm, the Node.js package manager. With Node.js, you can build fast and scalable server-side applications using JavaScript.