关于javascript:Nodejs-Web开发第二章设置Nodejs1

10次阅读

共计 5238 个字符,预计需要花费 14 分钟才能阅读完成。

电子书举荐

Multithreaded JavaScript: Concurrency Beyond the Event Loop

Computers For Seniors For Dummies, 6th Edition

Mastering Large Datasets with Python

Learning Vulkan

NGINX Cookbook

Machine Learning Refined

Distributed Systems with Node.js: Building Enterprise-Ready Backend Services

Programming PyTorch for Deep Learning: Creating and Deploying Deep Learning Applications

Python For Offensive PenTest

Python Interviews: Discussions with Python Experts

Python Web Scraping

Refactoring at Scale: Regaining Control of Your Codebase

SQL Cookbook: Query Solutions and Techniques for All SQL Users, 2nd Edition

The Algorithm Design Manual
Web Development with ReasonML
Mastering Active Directory: Design, deploy, and protect Active Directory Domain Services for Windows Server 2022, 3rd Edition

Microsoft Excel Step by Step (Office 2021 and Microsoft 365)

JavaScript Syntax and Practices

Web Application Security: Exploitation and Countermeasures for Modern Web Applications

CompTIA PenTest+ PT0-002 Cert Guide, 2nd Edition

WebAssembly in Action

Advanced Python Development

在开始应用 Node.js 之前,你必须设置好你的开发环境。尽管开发环设置起来非常容易,但也有一些留神 事项须要思考,包含是否应用包管理系统装置 Node.js,满足装置本地代码 Node.js 包的要求,以及抉择哪 款编辑器开发 Node.js。在接下来的章节中,咱们将学习如何部署开发环境进行和非生产境进。
在本章中,咱们将学习以下内容。

  • 如何在 Linux、macOS 或 Windows 上应用源代码和预包装的二进制文件中装置 Node.js
  • 如何装置 node 包管理器(npm)和其余一些常用工具 Node.js 的模块零碎
  • Node.js 的模块零碎
  • ECMAScript 委员会对 Node.js 和 JavaScript 语言的改良

# 零碎要求

Node.js 能够运行在 POSIX 类操作系统、各种 UNIX 衍生零碎(例如 Solaris)、类 UNIX 操作系统(例 如 Linux、macOS 等)以及 Microsoft Windows 上。它也能够在大型和小型机器上运行,包含微型 ARM 设 备,如 Raspberry Pi——一种用于 DIY 软件 / 硬件我的项目的微型嵌入式计算机。
Node.js 当初能够通过包管理系统取得,从而缩小了应用源代码处编译和装置的须要。
因为许多 Node.js 包是用 C 或 C ++ 编写的,所以必须要应用 C 编译器(例如 GCC)、Python 2.7(或以 后)和 node-gyp 包。因为 Python2 将在 2019 年底完结保护,Node.js 社区正在从新编写兼容 Python3 的工 具。如果你打算在 Web 代码中应用加密性能,则还须要 OpenSSL 加密库。古代 UNIX 衍生糸统简直都携带 这些工具,应用源代码装置时 Node.js 时,Node.js 配置脚本将检测到这些工具是否存在。如果您须要安 装,Python 能够从 http://python.org 下载,OpenSSL 可从 http://openssl.org 下载。
当初咱们曾经介绍了运行 Node.js 的环境要求,让咱们学习如何装置 Node.js。

应用包管理器(Package Manager)装置 Node.js

装置 Node.js 的首选办法是应用包管理器提供的版本,如 apt get 或 MacPorts。软件包管理器用于维 护计算机上的软件的以后版本,确保在必要时更新依赖的包,使您的开发工作更加轻松,所有这 些都通过输出一个简略的命令实现,如 apt get update。让咱们先从软件包管理系统理解一下安 装过程。
无关从 package Manager 装置的正式阐明,请拜访 https://nodejs.org/en/downloa…。

应用 MacPorts 在 macOS 上装置 Node.js

MacPorts 我的项目 (http://www.macports.org/) 多年来始终在为 MacOS 零碎打包了一系列开源软件 包,包含 Node.js。默认状况下,MacOS 零碎的治理命令装置在 /opt/local/bin 下。在你应用用 MacPorts 网站上的安装程序装置 MacPorts 后,装置 Node.js 非常简单。咱们能够在 MacPorts 安
装命令目录中装置 Node.js 二进制文件:

$ port search nodejs npm
...
nodejs8 @8.16.2 (devel, net)
Evented I/O for V8 JavaScript
nodejs10 @10.16.3 (devel, net)
    Evented I/O for V8 JavaScript
nodejs12 @12.13.0 (devel, net)
    Evented I/O for V8 JavaScript
nodejs14 @14.0.0 (devel, net)
    Evented I/O for V8 JavaScript ...
npm6 @6.14.4 (devel)
    node package manager
$ sudo port install nodejs14 npm6
.. long log of downloading and installing prerequisites and Node $ which node
/opt/local/bin/node
$ node --version
v14.0.0

如果已依照设置 MacPorts 的阐明进行操作,则 MacPorts 目录已在 PATH 环境变量中。运 行 node、npm 或 npx 命令则非常简单。这证实 Node.js 曾经装置,并且装置的版本合乎您的要求。MacPorts 不是治理 macOS 上开源软件包的惟一工具。

应用 Homebrew 在 macOS 上装置 Node.js

Homebrew 是 macOS 的另一个开源软件包管理器,有人说它是 MacPorts 的完满替代品。Homebrew 主 页:http://brew.sh/。依据 Homebrew 网站阐明装置 Homebrew 并确保其设置正确后,请应用以下代 码:

$ brew update
... long wait and lots of output
$ brew search node
==> Searching local taps...
node libbitcoin-node node-build node@8 nodeenv leafnode llnode node node@10 node@12 nodebrew nodenv ==> Searching taps on GitHub... 
caskroom/cask/node-profiler
==> Searching blacklisted, migrated and deleted formulae...

而后,按如下形式装置 Node.js:

$ brew install node
...
==> Installing node
==> ownloading 
https://homebrew.bintray.com/bottles/node-14.0.0_1.high_sierra.bottle. tar.gz ########################... 100.0%
==> Pouring node-14.0.0_1.high_sierra.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
  /usr/local/Cellar/node/14.0.0_1: 4,660 files, 60MB

与 MacPorts 一样,Homebrew 将命令装置在一个公共目录下,默认为 /usr/local/bin。如果你曾经按 照 Homebrew 的阐明将该目录增加到你的 PATH 变量中,请按以下形式运行 Node.js 命令:

$ node --version v14.0.0

这阐明 Node.js 曾经装置,而且装置的版本与你要求的相符。当然,macOS 只是咱们可能应用的泛滥操作系统中的一个。

应用包管理系统在 Linux、*BSD 或 Windows 上装置 Node.js

Node.js 当初能够通过大多数包管理系统装置。Node.js 网站目前列出了一长 串 Linux、FreeBSD、OpenBSD、NetBSD、macOS 甚至 Windows 的 Node.js 打包版本。访 问 https://nodejs.org/en/downloa… 理解更多信息。
例如,在 Debian 和其余基于 Debian 的 Linux 发行版(如 Ubuntu)上,应用以下命令:

$ curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
[sudo] password for david:
## Installing the NodeSource Node.js 14.x repo...
## Populating apt-get cache...
... much apt-get output
## Run `sudo apt-get install -y nodejs` to install Node.js 13.x and npm ## You may also need development tools to build native addons:
 sudo apt-get install gcc g++ make
$ sudo apt-get install -y nodejs
... Much output
$ sudo apt-get install -y gcc g++ make build-essential ... Much output 

这会将 NodeSource APT 存储库增加到零碎中,更新包数据,并筹备零碎,以便您能够装置 Node.js 包。它还领导咱们如何装置 Node.js 以及所需的编译器和开发人员工具。
要下载其余 Node.js 版本(此示例显示版本 14.x),请批改响应的 URL:

$ node --version 
v14.0.0

这些命令将装置在 /usr/bin 目录中,咱们能够测试下载的版本是否合乎咱们的要求。
Windows 10 内置零碎 Linux 零碎工具,简称 WSL。Windows 开始成为适宜 Unix/Linux 极客应用的零碎。

在 WSL 中装置 Node.js

WSL 容许您在 Windows 上装置 Ubuntu、openSUSE 或 SUSE Linux Enterprise。所有这三个都能够通 过 Windows 10 内置的商店下载。您可能须要更新 Windows 设施以能力应用 WSL。为了获得最佳体验,请 装置 WSL2,这是对 WSL 的一次重大改良,提供了 Windows 和 Linux 之间更好的集成。
装置 WSL 后,Linux 特定的阐明将在 Linux 子系统中装置 Node.js。
** 要装置 WSL,请参阅 https://msdn.microsoft.com/en… commandline/wsl/install-win10。
要理解和装置 WSL2,见 https://docs.microsoft.com/ en-us/windows/wsl/wsl2-index。**
该过程可能须要晋升 Windows 上的权限。

在 Windows 应用 治理 员权限关上 PowerShell

在 Windows 上装置工具时运行的某些命令须要在具备晋升权限的 PowerShell 窗口中执行。我之所以提到 这一点,是因为在启用 WSL 的过程中,须要在 PowerShell 窗口中运行命令。
过程很简略:

  1. 在“开始”菜单中,在应用程序的搜寻框中输出 PowerShell。弹出的菜单将列出 PowerShell。
  2. 右键单击 PowerShell 选项。
  3. 弹出的上下文菜单将有一个名为“以管理员身份运行”的选项,点击这个选项。

生成的命令窗口将具备管理员权限,标题栏将显示管理员:Windows PowerShell。
在某些状况下,您可能无奈应用包管理系统装置 Node.js。

正文完
 0