ServBay Documentation: Setting a Specific Node.js Version for Your Projects, Scripts, and Websites
ServBay is a powerful local web development environment that supports multiple programming languages and technology stacks, including Node.js. In real-world development, you may need to use different Node.js versions for different projects, individual scripts, or even for an entire website. For example, a legacy project might depend on Node.js 14, while a new project may require new features available in Node.js 20. ServBay offers flexible ways to meet these needs.
This article will walk you through how to easily specify the Node.js version for your scripts and websites in ServBay, as well as how to leverage project-level configuration.
Specify Node.js Version and Configuration for a Project Using the .servbay.config File
For projects that require strict control over the Node.js version and related environment variables, ServBay provides project-level configuration via a .servbay.config
file placed in the project root directory. With this file, you can configure the Node.js version, package registry addresses, caching directories, and more for that specific project—ensuring environment isolation and consistency.
A .servbay.config
file that sets a specific Node.js version and configures the npm/yarn registry and cache directories might look like this:
bash
NODE_VERSION=20
YARN_CONFIG_REGISTRY=https://registry.npmmirror.com/ # Example: Use a specific mirror
NPM_CONFIG_REGISTRY=https://registry.npmmirror.com/ # Example: Use a specific mirror
NPM_CONFIG_CACHE=/Applications/ServBay/tmp/npm/cache # Example: Specify cache path
YARN_CONFIG_CACHE=/Applications/ServBay/tmp/yarn/cache # Example: Specify cache path
1
2
3
4
5
2
3
4
5
Notes:
NODE_VERSION
: Specifies the major version of Node.js to use for this project. ServBay will find and use the installed Node.js version that matches this.YARN_CONFIG_REGISTRY
,NPM_CONFIG_REGISTRY
: Sets the npm or yarn package manager registry address the project should use.NPM_CONFIG_CACHE
,YARN_CONFIG_CACHE
: Configures the cache directory for the project's npm or yarn package manager. The example uses subdirectories under ServBay’s default temp directory/Applications/ServBay/tmp/
.
Generally, place the .servbay.config
file in your project’s root directory. For detailed configurations and usage, refer to the official ServBay documentation’s section on .servbay.config files.
Run Scripts with Specific Node.js Versions via Command Line
If you don’t want to set a Node.js version at the project level and only need to temporarily use a specific Node.js version for a script or command, you can use ServBay’s command line aliases.
Within the ServBay terminal environment (you can open the terminal from the ServBay application interface), you can easily specify the Node.js version for a script by using the node-<version>
command. ServBay automatically creates such aliases for each installed Node.js version.
For example, to run script.js
with Node.js 14, you can use:
bash
$ node-14 script.js
Hello World
Node.js Version: 14.17.0 # Example output, version may vary
$ node-16 script.js
Hello World
Node.js Version: 16.3.0 # Example output, version may vary
$ node-20 script.js
Hello World
Node.js Version: 20.10.0 # Example output, version may vary
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Example Explanation
$ node-14 script.js
: Runs thescript.js
file using the installed Node.js 14 version in ServBay (the exact 14.x.x version depends on your installation).$ node-16 script.js
: Runs the script with Node.js 16.$ node-20 script.js
: Runs the script with Node.js 20.
This method allows you to flexibly test and run scripts with different Node.js versions, or quickly execute tasks requiring a specific Node.js version, all without changing your global Node.js version.
Specify Node.js Version for Your Website via the ServBay Web Interface
ServBay lets you set the default Node.js runtime version for each hosted website directly from the management interface. This is especially useful for websites that depend on Node.js as a backend service (such as Node.js framework apps behind Caddy or Nginx reverse proxies) or require a particular Node.js environment.
Steps
Open the ServBay Management Interface: Launch the ServBay application and access the management console via your browser (usually at
http://localhost:5200
or via a link in the ServBay app window).Navigate to Website Management: In the sidebar, click on
Websites
.Select and Edit Your Website: From the list, locate the website you wish to configure (for example, one you created at
/Applications/ServBay/www/servbay.demo
). Click the edit button (gear or pencil icon) next to the site, or click the site name to open its details.Set Node.js Version: In the website’s settings page, find the option related to Node.js version—usually a dropdown menu. Select the Node.js version you want the site to use by default.
Save Your Changes: After choosing a version, be sure to click the
Save
orApply
button at the bottom of the page to save the changes.Restart the Website or ServBay (If Needed): Some settings may require a restart of the associated web server (such as Caddy or Nginx) or the entire ServBay for changes to take effect. ServBay will typically prompt you if a restart is necessary.
Important Notes
- Ensure Node.js Version Is Installed and Running: The Node.js version you select must already be installed and active in the
Packages
page of ServBay. If it’s not installed or running, go to thePackages
page to install or start it first. - Site Type: This setting primarily affects sites that use Node.js as their runtime environment (such as backend applications built with Express.js, Next.js, or NestJS). For static sites or those running entirely on PHP, Python, Go, etc., this setting may not apply or have limited effect.
- Compatibility Testing: After changing your site’s Node.js version, be sure to thoroughly test your site to ensure all dependencies and services work properly under the new version, with no compatibility issues.
- How This Relates to
.servbay.config
: The website-level setting provides a default Node.js version. If a.servbay.config
file is present in the site directory and specifies aNODE_VERSION
, the.servbay.config
takes precedence, allowing for more granular control.
Summary
ServBay is a comprehensive local development environment offering multiple flexible and powerful ways to manage and specify your Node.js runtime. You can use the .servbay.config
file in your project root for fine-grained project-level control and environment variable configuration, quickly run scripts with a specific Node.js version using intuitive node-<version>
command line aliases, or directly set the default Node.js version for your website via the management interface. These features ensure you can easily handle various Node.js version requirements across different projects and scenarios—whether you’re maintaining legacy projects, testing new features, or keeping your development environment in sync with production. ServBay delivers reliable support, significantly improving your development efficiency and environment management capabilities.