|
1 | 1 | Node.js PyPI distribution |
2 | 2 | ===================== |
3 | 3 |
|
4 | | -This repository contains the script used to repackage the [releases][nodejsdl] of [Node.js][nodejs] as [Python binary wheels][wheel]. This document is intended for maintainers; see the [package README][pkgreadme] for rationale and usage instructions. |
| 4 | +[Node.js][nodejs] is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser. |
5 | 5 |
|
6 | | -The repackaged artifacts are published as the [node-js PyPI package][pypi]. |
| 6 | +The [nodejs-bin][pypi] Python package redistributes Node.js so that it can be used as a dependency of Python projects. With `nodejs-bin` you can call `nodejs`, `npm` and `npx` from both the [command line](#command-line-usage) and a [Python API](#python-api-usage). |
| 7 | + |
| 8 | +**Note: this is an unofficial Node.js distribution.** |
| 9 | + |
| 10 | +**This is intended for use within Python virtual environments and containers, it should probably not be used for global instillation.** |
| 11 | + |
| 12 | +This PyPI distribution is provided by <https://github.com/samwillis/nodejs-pypi>. |
7 | 13 |
|
8 | 14 | [nodejs]: https://nodejs.org/ |
9 | | -[nodejsdl]: https://nodejs.org/en/download/ |
10 | | -[wheel]: https://github.com/pypa/wheel |
11 | | -[pkgreadme]: README.pypi.md |
12 | 15 | [pypi]: https://pypi.org/project/nodejs-bin/ |
13 | 16 |
|
14 | | -This tool is based on the work of the creators of the [Zig language][ziglang], see [the original][basedon]. Thank you! |
| 17 | +Install |
| 18 | +------- |
| 19 | + |
| 20 | +To install: |
| 21 | + |
| 22 | +```shell |
| 23 | +pip install nodejs-bin |
| 24 | +``` |
| 25 | + |
| 26 | +You can specify the Node.js version to install with: |
15 | 27 |
|
16 | | -[ziglang]: https://ziglang.org |
17 | | -[basedon]: https://github.com/ziglang/zig-pypi |
| 28 | +```shell |
| 29 | +pip install nodejs-bin==<version> |
18 | 30 |
|
19 | | -Preparation |
20 | | ------------ |
| 31 | +# Example: |
| 32 | +pip install nodejs-bin==16.15.1 |
| 33 | +``` |
21 | 34 |
|
22 | | -The script requires Python 3.5 or later. |
| 35 | +Command Line Usage |
| 36 | +------------------ |
23 | 37 |
|
24 | | -Install the dependencies: |
| 38 | +To run Node.js from the command line, use: |
25 | 39 |
|
26 | 40 | ```shell |
27 | | -pip install wheel twine libarchive-c |
| 41 | +python -m nodejs |
| 42 | +# or (see below) |
| 43 | +node |
28 | 44 | ``` |
29 | 45 |
|
30 | | -The `libarchive-c` Python library requires the native [libarchive][] library to be available. |
| 46 | +`npm` and `npx` are also available as `python -m nodejs.npm` and `python -m nodejs.npx`. |
| 47 | + |
| 48 | +Additionally, the standard `node`, `npm` and `npx` commands are also added to your Python environment's `bin` directory. This is usually on your `PATH` and so they should be available in your shell environment. |
31 | 49 |
|
32 | | -[libarchive]: https://libarchive.org/ |
| 50 | +Python API Usage |
| 51 | +---------------- |
33 | 52 |
|
34 | | -Building wheels |
35 | | ---------------- |
| 53 | +`node-bin` has a simple Python API that wraps the Node.js command line with the [Python `subprocess`](https://docs.python.org/3/library/subprocess.html). |
36 | 54 |
|
37 | | -Run the repackaging script: |
| 55 | +For `node`, `npm` and `npx` there are `.call()`, `.run()` and `.Popen()` methods that match the equivalent `subprocess` methods. |
38 | 56 |
|
39 | | -```shell |
40 | | -python make_wheels.py |
| 57 | +To run Node.js from a Python program and return the exit code: |
| 58 | + |
| 59 | +```python |
| 60 | +from nodejs import node, npm, npx |
| 61 | + |
| 62 | +# Run Node.js and return the exit code. |
| 63 | +node.call(['script.js', 'arg1', ...], **kwargs) |
| 64 | + |
| 65 | +# Run npm and return the exit code. |
| 66 | +npm.call(['command', 'arg1', ...], **kwargs) |
| 67 | + |
| 68 | +# Run npx and return the exit code. |
| 69 | +npx.call(['command', 'arg1', ...], **kwargs) |
41 | 70 | ``` |
42 | 71 |
|
43 | | -This command will download the Node.js release archives for every supported platform and convert them to binary wheels, which are placed under `dist/`. The Node.js version and platforms are configured in the script source. |
| 72 | +The `call(args, **kwargs)` functions wrap [`subprocess.call()`](https://docs.python.org/3/library/subprocess.html#subprocess.call), passes though all `kwargs` and returns the exit code of the process. |
44 | 73 |
|
45 | | -The process of converting release archives to binary wheels is deterministic, and the output of the script should be bit-for-bit identical regardless of the environment and platform it runs under. To this end, it prints the SHA256 hashes of inputs and outputs; the hashes of the inputs will match the ones on the [Node.js downloads page][nodejsdl], and the hashes of the outputs will match the ones on the [PyPI downloads page][pypidl]. |
| 74 | +To run Node.js from a Python program and return a [CompletedProcess](https://docs.python.org/3/library/subprocess.html#subprocess.CompletedProcess) object: |
46 | 75 |
|
47 | | -[pypidl]: https://pypi.org/project/node-js/#files |
| 76 | +```python |
| 77 | +from nodejs import node, npm, npx |
48 | 78 |
|
49 | | -Uploading wheels |
50 | | ----------------- |
| 79 | +# Run Node.js and return the exit code. |
| 80 | +node.run(['script.js', 'arg1', ...], **kwargs) |
51 | 81 |
|
52 | | -Run the publishing utility: |
| 82 | +# Run npm and return the exit code. |
| 83 | +npm.run(['command', 'arg1', ...], **kwargs) |
53 | 84 |
|
54 | | -```shell |
55 | | -twine dist/* |
| 85 | +# Run npx and return the exit code. |
| 86 | +npx.run(['command', 'arg1', ...], **kwargs) |
56 | 87 | ``` |
57 | 88 |
|
58 | | -This command will upload the binary wheels built in the previous step to PyPI. |
| 89 | +The `run(args, **kwargs)` functions wrap [`subprocess.run()`](https://docs.python.org/3/library/subprocess.html#subprocess.run), passes though all `kwargs` and returns a `CompletedProcess`. |
| 90 | + |
| 91 | +Additionally, to start a Node.js process and return a `subprocess.Popen` object, you can use the `Popen(args, **kwargs)` functions: |
| 92 | + |
| 93 | +```python |
| 94 | +from nodejs import node, npm, npx |
| 95 | + |
| 96 | +# Start Node.js and return the Popen object. |
| 97 | +node_process = node.Popen(['script.js', 'arg1', ...], **kwargs) |
| 98 | + |
| 99 | +# Start npm and return the Popen object. |
| 100 | +npm_process = npm.Popen(['command', 'arg1', ...], **kwargs) |
| 101 | + |
| 102 | +# Start npx and return the Popen object. |
| 103 | +npx_process = npx.Popen(['command', 'arg1', ...], **kwargs) |
| 104 | +``` |
| 105 | + |
| 106 | +The `Popen(args, **kwargs)` functions wrap [`subprocess.Popen()`](https://docs.python.org/3/library/subprocess.html#subprocess.Popen), passes though all `kwargs` and returns a [`Popen` object](https://docs.python.org/3/library/subprocess.html#popen-objects). |
| 107 | + |
| 108 | +The `nodejs.node` api is also available as `nodejs.run` and `nodejs.call` and `nodejs.Popen`. |
| 109 | + |
| 110 | +Finally, there are a number of convenient attributes on the `nodejs` module: |
| 111 | + |
| 112 | + * `nodejs.node_version`: the version of Node.js that is installed. |
| 113 | + * `nodejs.path`: the path to the Node.js executable. |
| 114 | + |
59 | 115 |
|
60 | 116 | License |
61 | 117 | ------- |
62 | 118 |
|
63 | | -This script is distributed under the terms of the [MIT (Expat) license](LICENSE.txt). |
| 119 | +The [Node.js license](https://raw.githubusercontent.com/nodejs/node/master/LICENSE). |
0 commit comments