在Ubuntu / Debian上安装最新的Node.js和NPM

Welcome to our guide on how to install Latest Node.js and NPM on Ubuntu & Debian Linux distributions. Node.js is a free and open source server-side programming language which runs on various platforms (Linux, Windows, Unix, macOS). Node.js is a JavaScript runtime built on Chrome’s V8 engine for building fast and scalable network applications.

Step 1: Add Node.js APT Repository

The most recent packages of Node.js are available on a APT repository. First, update your system and install some dependencies.

sudo apt-get update
sudo apt-get -y install curl dirmngr apt-transport-https lsb-release ca-certificates vim

If you want to know the latest release, check Node.js Releases page

Add Node.js None LTS repository

If you want to go with the latest upstream version, add APT for that version. E.g for Node.js 11.x. It will be added like this.

curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -

This is most fit for Development on your Local machine.

Add Node.js LTS repository

For production use, I recommend you do the installation of the latest LTS release. As of this article update, this is Node.js 10.x.

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

Step 2: Install Latest Node.js on Ubuntu / Debian

After adding the repository, proceed to install Node.js and NPM.

sudo apt-get -y install nodejs

You can also development tools to build native addons:

sudo apt-get install gcc g++ make

Confirm versions.

$ node --version
v11.13.0

$ npm --version
6.7.0

Step 3: Install Yarn package manager ( Bonus and Optional)

If you need yarn package manager, install it by running:

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install yarn

Confirm installation.

$ yarn --version
1.15.2

Step 4: Test Node.js

Let’s create a simple Nodejs app as a test that Node.js is working.

mkdir /tmp/node-demo
cd /tmp/node-demo

initialize nodejs project with default package.json file:

$ npm init -y
Wrote to /tmp/node-demo/package.json:

{
  "name": "node-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Create an index.js

vim index.js

Add:

const express = require('express');

const PORT = 8080;
const HOST = '0.0.0.0';

const app = express();
app.get('/', (req, res) => {
  res.send('Hello Node.js World\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

Install express package with npm command:

$ npm install -save express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN node-demo@1.0.0 No description
npm WARN node-demo@1.0.0 No repository field.

+ express@4.16.4
added 48 packages from 36 contributors and audited 121 packages in 2.555s
found 0 vulnerabilities

Add start script to our packge.json file

"start": "node index.js"

You can now run Nodejs application:

$ node index.js 
Running on http://0.0.0.0:8080

OR

$ npm start
 Running on http://0.0.0.0:8080

The application can be started with debugging enabled using:

$ node --inspect index.js 
Debugger listening on ws://127.0.0.1:9229/49cd62a8-88e0-4b21-b898-f79a79e9d5dc
For help, see: https://nodejs.org/en/docs/inspector
Running on http://0.0.0.0:8080

If you visit your Server IP on port 8080, you should see Node.js application output.

There you go!. Node.js has been successful installed on Ubuntu / Debian Linux distribution. Have a happy Node Development.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值