Node.js Wheel Binaries
The `nodejs-wheel-binaries` library provides unofficial Node.js and npm binaries packaged as a Python wheel. This allows Python projects to easily access and execute Node.js without requiring a system-wide Node.js installation, particularly useful in isolated or CI/CD environments. The current version is 24.14.1, and it typically releases new versions corresponding to major Node.js updates.
Warnings
- gotcha This library provides Node.js and npm binaries for execution, not a Python wrapper or API. You cannot `import nodejs` and call Python functions; you must use `subprocess` to run the discovered binaries (`node_bin`, `npm_bin`).
- gotcha The package does not automatically add `node` or `npm` to your system's PATH. You must explicitly import and use `find_nodejs_bin()` and `find_npm_bin()` to locate the executables within the Python environment.
- gotcha Other Node.js ecosystem tools like `npx` or `yarn` are not directly provided by this wheel. If needed, you must install them globally using the `npm_bin` found by this package (e.g., `subprocess.run([npm_bin, 'install', '-g', 'npx'])`).
- breaking While the Python library aims for stability, the included Node.js binary itself is subject to Node.js's release cycle. Changes in Node.js behavior, deprecations, or API shifts (e.g., in module resolution, core APIs) can break your Node.js code, even if the Python library still correctly provides the binary. This is especially relevant when updating to new major Node.js versions (e.g., v18 to v20, v20 to v22).
Install
-
pip install nodejs-wheel-binaries
Imports
- find_nodejs_bin
from nodejs_wheel_binaries import find_nodejs_bin
- find_npm_bin
from nodejs_wheel_binaries import find_npm_bin
Quickstart
import subprocess
from nodejs_wheel_binaries import find_nodejs_bin, find_npm_bin
# Find the Node.js executable
node_bin = find_nodejs_bin()
npm_bin = find_npm_bin()
if node_bin:
print(f"Node.js binary found at: {node_bin}")
try:
# Run a simple Node.js command to get its version
result = subprocess.run([node_bin, '--version'], capture_output=True, text=True, check=True)
print(f"Node.js version: {result.stdout.strip()}")
# Example: Run a simple JS script directly
js_code = "console.log('Hello from Node.js via Python!');"
result = subprocess.run([node_bin, '-e', js_code], capture_output=True, text=True, check=True)
print(f"Node.js script output: {result.stdout.strip()}")
except FileNotFoundError:
print("Node.js executable not found in PATH after discovery.")
except subprocess.CalledProcessError as e:
print(f"Error running Node.js command: {e}")
print(f"Stderr: {e.stderr}")
else:
print("Node.js binary could not be found by the wheel.")
if npm_bin:
print(f"\nNPM binary found at: {npm_bin}")
try:
# Run a simple NPM command to get its version
result = subprocess.run([npm_bin, '--version'], capture_output=True, text=True, check=True)
print(f"NPM version: {result.stdout.strip()}")
except subprocess.CalledProcessError as e:
print(f"Error running NPM command: {e}")
print(f"Stderr: {e.stderr}")