NodeJS 是一个 JavaScript 框架,可让您轻松构建快速的网络应用程序。在本指南中,我们将深入研究如何在 Ubuntu 18.04 上安装 NodeJS。
首先,使用以下命令将 NodeJS PPA 添加到您的系统。
sudo apt-get install software-properties-common
Sample Output Next, add the NodeJS PPA.
curl -sL https://deb.nodesource.com/setup_11.x | sudo -E bash -
Sample Output Great! In our next step, we are going to run the command for installing NodeJS.
成功添加 NodeJS PPA 后,现在可以使用以下命令安装 NodeJS。
sudo apt-get install nodejs
Sample Output This command not only installs NodeJS but also NPM (NodeJS Package Manager) and other dependencies as well.
成功安装 NodeJS 后,您可以使用以下简单命令测试 NodeJS 的版本。
node -v
Sample Output For NPM, run
npm -v
Sample Output
这是一个可选步骤,您可以使用它来测试 NodeJS 是否按预期工作。我们将创建一个显示文本“恭喜! Node.JS 已成功安装!”让我们创建一个 NodeJS 文件并将其命名为 nodeapp.js
vim nodeapp.js
添加以下内容
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Congratulations! node.JS has successfully been installed !\n');
}).listen(3000, " server-ip);
console.log('Server running at https://server-ip:3000/');
保存并退出文本编辑器使用以下命令启动应用程序
node nodeapp.js
Sample Output This will display the content of the application on port 3000. Ensure the port is allowed on the firewall of your system.
ufw allow 3000/tcp
ufw reload
现在打开浏览器并浏览服务器地址,如图所示
https://server-ip:3000
如果您想从 Ubuntu 系统中卸载 NodeJS,请运行以下命令。
sudo apt-get remove nodejs
该命令将删除包但保留配置文件。要删除包和配置文件,请运行:
sudo apt-get purge nodejs
最后一步,您可以运行以下命令来删除任何未使用的文件并释放磁盘空间
sudo apt-get autoremove
伟大的!我们已经成功安装并测试了NodeJS的安装。我们还学习了如何从 Ubuntu 中卸载 NodeJS 并清理空间。