- Node.js Web Development
- David Herron
- 931字
- 2021-06-11 18:48:16
Installing from the source on POSIX-like systems
Installing the prepackaged Node.js distributions is the preferred installation method. However, installing Node.js from a source is desirable in a few situations:
- It can let you optimize the compiler settings as desired.
- It can let you cross-compile, say, for an embedded ARM system.
- You might need to keep multiple Node.js builds for testing.
- You might be working on Node.js itself.
Now that you have a high-level view, let's get our hands dirty by mucking around in some build scripts. The general process follows the usual configure, make, and make install routine that you may have already performed with other open source software packages. If not, don't worry, we'll guide you through the process.
The official installation instructions are in README.md, contained in the source distribution at https://github.com/nodejs/node/blob/master/README.md.
Installing prerequisites
There are three prerequisites: a C compiler, Python, and the OpenSSL libraries. The Node.js compilation process checks for their presence and will fail if the C compiler or Python is not present. These sorts of commands will check for their presence:
$ cc --version
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin $ python
Python 2.7.16 (default, Oct 16 2019, 00:35:27)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Go to https://github.com/nodejs/node/blob/master/BUILDING.md for details on the requirements.
The specific method for installing these depends on your OS.
The Node.js build tools are in the process of being updated to support Python 3.x. Python 2.x is in an end-of-life process, slated for the end of 2019, so it is therefore recommended that you update to Python 3.x.
Before we can compile the Node.js source, we must have the correct tools installed and on macOS, there are a couple of special considerations.
Installing developer tools on macOS
Developer tools (such as GCC) are an optional installation on macOS. Fortunately, they're easy to acquire.
You start with Xcode, which is available for free through the Macintosh app store. Simply search for Xcode and click on the Get button. Once you have Xcode installed, open a Terminal window and type the following:
$ xcode-select --install
This installs the Xcode command-line tools:
For additional information, visit http://osxdaily.com/2014/02/12/install-command-line-tools-mac-os-x/.
Now that we have the required tools installed, we can proceed with compiling the Node.js source.
Installing from the source for all POSIX-like systems
Compiling Node.js from the source follows this familiar process:
- Download the source from http://nodejs.org/download.
- Configure the source for building using ./configure.
- Run make, then make install.
The source bundle can be downloaded through your browser or as follows, substituting your preferred version:
$ mkdir src $ cd src $ wget https://nodejs.org/download/release/v14.0.0/node-v14.0.0.tar.gz $ tar xvfz node-v14.0.0.tar.gz $ cd node-v14.0.0
Now, we configure the source so that it can be built. This is just like with many other open source packages and there is a long list of options to customize the build:
$ ./configure --help
To cause the installation to land in your home directory, run it this way:
$ ./configure --prefix=$HOME/node/14.0.0 ..output from configure
If you're going to install multiple Node.js versions side by side, it's useful to put the version number in the path like this. That way, each version will sit in a separate directory. It will then be a simple matter of switching between Node.js versions by changing the PATH variable appropriately:
# On bash shell:
$ export PATH=${HOME}/node/VERSION-NUMBER/bin:${PATH}
# On csh
$ setenv PATH ${HOME}/node/VERSION-NUMBER/bin:${PATH}
A simpler way to install multiple Node.js versions is by using the nvm script, which will be described later.
If you want to install Node.js in a system-wide directory, simply leave off the --prefix option and it will default to installing in /usr/local.
After a moment, it'll stop and will likely have successfully configured the source tree for installation in your chosen directory. If this doesn't succeed, the error messages that are printed will describe what needs to be fixed. Once the configure script is satisfied, you can move on to the next step.
With the configure script satisfied, you compile the software:
$ make .. a long log of compiler output is printed $ make install
If you are installing on a system-wide directory, perform the last step this way instead:
$ make $ sudo make install
Once installed, you should make sure that you add the installation directory to your PATH variable, as follows:
$ echo 'export PATH=$HOME/node/14.0.0/bin:${PATH}' >>~/.bashrc $ . ~/.bashrc
Alternatively, for csh users, use this syntax to make an exported environment variable:
$ echo 'setenv PATH $HOME/node/14.0.0/bin:${PATH}' >>~/.cshrc $ source ~/.cshrc
When the build is installed, it creates a directory structure, as follows:
$ ls ~/node/14.0.0/ bin include lib share $ ls ~/node/14.0.0/bin
node npm npx
Now that we've learned how to install Node.js from the source on UNIX-like systems, we get to do the same on Windows.
Installing from the source on Windows
The BUILDING.md document referenced previously has instructions. You can use the build tools from Visual Studio or the full Visual Studio 2017 or 2019 product:
- Visual Studio 2019: https://www.visualstudio.com/downloads/
- The build tools: https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019
Three additional tools are required:
- Git for Windows: http://git-scm.com/download/win
- Python: https://www.python.org/
- OpenSSL: https://www.openssl.org/source/ and https://wiki.openssl.org/index.php/Binaries
- The Netwide Assembler (NASM) for OpenSSL: https://www.nasm.us/
Then, run the included .\vcbuild script to perform the build.
We've learned how to install one Node.js instance, so let's now take it to the next level by installing multiple instances.
- Android Wearable Programming
- Web程序設計及應用
- Learning Data Mining with Python
- Visual Basic程序設計教程
- jQuery開發基礎教程
- Babylon.js Essentials
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- QPanda量子計算編程
- Building Slack Bots
- jQuery從入門到精通(微課精編版)
- Mobile Forensics:Advanced Investigative Strategies
- JavaScript前端開發基礎教程
- Java 9:Building Robust Modular Applications
- Spring MVC Blueprints
- Mastering VMware vSphere Storage