Skip to main content
In this article, we demonstrate how to update your detect and build scripts to dynamically set the Node.js version specified by the user in the package.json file. The package.json file includes an “engines” section where you can declare the required Node.js version. In our example, the package.json file specifies Node.js version 18.18.0.

Updating the Detect Script

The detect script now sets a default Node.js version and then attempts to extract the user-specified version from the package.json file by reading the “engines.node” property. Finally, it writes the appropriate “provides” and “requires” fields to the buildpack build plan, which the build script uses later to download and install the correct Node.js version.
Ensure that your package.json file contains the “engines” section with the correct Node.js version.
To test the container state, run:
Expected output for docker ps:
Verify the application response with:
Output:

Updating the Build Script

Previously, the build script hardcoded Node.js version 18.18.1. In the updated script, a default Node.js version (18.18.0) is defined, and the desired version is later retrieved from the build plan. This retrieved version is then used to dynamically construct the download URL for the appropriate Node.js tarball. Below is the revised build script:
After building, you can check the container status with:
Expected output:
And confirm the application response again:
Output:

Dynamically Constructing the Node.js Download URL

The build script is now updated to dynamically include the user-specified Node.js version. After retrieving the version from the build plan, the script prints the version for verification and constructs the download URL based on that version. The snippet below shows the modified section:
Run the container check again with:
Expected output:
Verify the endpoint:
Output:
Remember to delete the old container and rebuild the image to ensure the changes take effect.

Verifying the Buildpack with package.json

Once again, note that the version requested (as printed in the logs) is 18.18.0. The updated package.json looks like this:
The console output during the build might resemble:
After confirming this output, delete the old container and rebuild the image. The build output will then verify that the correct Node.js version is used. To test the dynamic version retrieval, update the package.json “engines.node” value to 18.18.1 and rebuild the image. The build logs should now indicate that Node.js version 18.18.1 is installed:
Console output for the new build may include:
And later:
This confirms that the buildpack now successfully supports dynamic Node.js version selection based on the user’s specification in package.json. Happy building!

Additional Resources

For further details, visit the following helpful resources:

Watch Video