Repository: sindresorhus/run-node Branch: main Commit: 8489f3b0fbfd Files: 10 Total size: 8.1 KB Directory structure: gitextract_0gyit1k6/ ├── .editorconfig ├── .gitattributes ├── .github/ │ ├── security.md │ └── workflows/ │ └── main.yml ├── .npmrc ├── license ├── package.json ├── readme.md ├── run-node └── run-npx ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 ================================================ FILE: .gitattributes ================================================ * text=auto eol=lf ================================================ FILE: .github/security.md ================================================ # Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. ================================================ FILE: .github/workflows/main.yml ================================================ name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 20 - 18 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test ================================================ FILE: .npmrc ================================================ package-lock=false ================================================ FILE: license ================================================ MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: package.json ================================================ { "name": "run-node", "version": "3.0.0", "description": "Run the Node.js binary no matter what", "license": "MIT", "repository": "sindresorhus/run-node", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "bin": { "run-node": "./run-node", "run-npx": "./run-npx" }, "engines": { "node": ">=18" }, "scripts": { "test": "./run-node --version" }, "files": [ "run-node", "run-npx" ], "keywords": [ "run", "node", "nodejs", "node.js", "npx", "npm", "find", "binary", "bin", "execute", "which", "detect", "path", "env", "bash", "shell", "sh" ] } ================================================ FILE: readme.md ================================================ # run-node > Run the Node.js binary no matter what You can't always assume running `$ node file.js` will just work. The user might have the `node` binary in a non-standard location. They might be using a Node.js version manager like `nvm`, which is sourced in a subshell and not available from the outside. Or they might have `node` installed as a local dependency in an npm project. It also depends from where you're trying to run it. For example, GUI apps on macOS doesn't inherit the [`$PATH`](https://en.wikipedia.org/wiki/PATH_(variable)), so the `node` binary would not be found. Most projects that depend on Node.js just end up telling the user to manually set the full path to the `node` binary in some project specific settings. Now every project has to do this. [Ugh...](https://gist.github.com/cookrn/4015437) I prefer things to *just* work. With this module it will. This Bash script uses some tricks to find the Node.js binary on your system and run it. A companion `run-npx` script is also provided to reliably run `npx`. Can be used from any environment that can spawn a process (Shell, Python, Ruby, Swift, Objective-C, etc). ### npm #### Install ```sh npm install run-node ``` #### Usage ```sh ./node_modules/.bin/run-node file.js ``` Or in an [npm run script](https://docs.npmjs.com/cli/run-script): ```json { "start": "run-node file.js" } ``` To run `npx` commands: ```sh ./node_modules/.bin/run-npx package-name ``` When run within an npm script context, `run-node` will use the same Node.js binary that npm is using (via the `npm_node_execpath` environment variable), ensuring consistency with the parent process. When run directly outside of npm scripts, if the [`node`](https://www.npmjs.com/package/node) package is found in the local `node_modules` directory, that binary will be used instead. ### Manually #### Install Download the [run-node](run-node) file: ```sh curl -sSLO https://github.com/sindresorhus/run-node/raw/main/run-node && chmod +x run-node ``` For `npx` support, also download [run-npx](run-npx): ```sh curl -sSLO https://github.com/sindresorhus/run-node/raw/main/run-npx && chmod +x run-npx ``` #### Usage ```sh ./run-node file.js ``` Or with `npx`: ```sh ./run-npx package-name ``` #### Customizable cache path and error message The cache path and error message are defined by the `RUN_NODE_CACHE_PATH` and `RUN_NODE_ERROR_MSG` environment variables. You could use them in a script or add them to your `~.bashrc`. Default config: ```sh export RUN_NODE_ERROR_MSG="Couldn't find the Node.js binary. Ensure you have Node.js installed. Open an issue on https://github.com/sindresorhus/run-node" export RUN_NODE_CACHE_PATH="/home/username/.node_path" ``` If the `RUN_NODE_CACHE_PATH` environment variable is defined explicitly, the script it points to will be sourced before looking for a `node` binary. You can use this script to override your `PATH` variable so that a specific `node` binary is found. ================================================ FILE: run-node ================================================ #!/usr/bin/env bash # MIT License © Sindre Sorhus # If npm_node_execpath is set and exists, use it directly if [[ -n $npm_node_execpath && -x $npm_node_execpath ]]; then exec "$npm_node_execpath" "$@" fi if [[ -f "$PWD/node_modules/node/bin/node" ]]; then PATH="$PWD/node_modules/node/bin:$PATH" export PATH fi if [[ -z $RUN_NODE_CACHE_PATH ]]; then PATH_CACHE="$HOME"/.node_path else PATH_CACHE="$RUN_NODE_CACHE_PATH" if [[ -f "$PATH_CACHE" ]]; then . "$PATH_CACHE" export PATH fi fi get_user_path() { [[ -x "/usr/libexec/path_helper" ]] && eval $(/usr/libexec/path_helper -s) echo "$($SHELL -i -l -c 'echo -e "\n"PATH=\"$PATH:\$PATH\""\n"' 2>/dev/null | grep "^PATH=")" > "$PATH_CACHE" } set_path() { if [[ -f "$PATH_CACHE" ]]; then . "$PATH_CACHE" else get_user_path . "$PATH_CACHE" fi export PATH } has_node() { command -v node >/dev/null 2>&1 } if ! has_node; then set_path # Retry by deleting old path cache if ! has_node; then rm "$PATH_CACHE" set_path fi fi if has_node; then node "$@" else if [[ -z $RUN_NODE_ERROR_MSG ]]; then echo "Couldn't find the Node.js binary. Ensure you have Node.js installed. Open an issue on https://github.com/sindresorhus/run-node" else echo "$RUN_NODE_ERROR_MSG" fi fi ================================================ FILE: run-npx ================================================ #!/usr/bin/env bash # MIT License © Sindre Sorhus # If npm_node_execpath is set and exists, prefer that over local node package if [[ -n $npm_node_execpath && -x $npm_node_execpath ]]; then # Don't add local node to PATH when npm_node_execpath is available # This ensures consistency with the parent npm process : elif [[ -f "$PWD/node_modules/node/bin/node" ]]; then PATH="$PWD/node_modules/node/bin:$PATH" export PATH fi if [[ -z $RUN_NODE_CACHE_PATH ]]; then PATH_CACHE="$HOME"/.node_path else PATH_CACHE="$RUN_NODE_CACHE_PATH" if [[ -f "$PATH_CACHE" ]]; then . "$PATH_CACHE" export PATH fi fi get_user_path() { [[ -x "/usr/libexec/path_helper" ]] && eval $(/usr/libexec/path_helper -s) echo "$($SHELL -i -l -c 'echo -e "\n"PATH=\"$PATH:\$PATH\""\n"' 2>/dev/null | grep "^PATH=")" > "$PATH_CACHE" } set_path() { if [[ -f "$PATH_CACHE" ]]; then . "$PATH_CACHE" else get_user_path . "$PATH_CACHE" fi export PATH } has_npx() { command -v npx >/dev/null 2>&1 } if ! has_npx; then set_path # Retry by deleting old path cache if ! has_npx; then rm "$PATH_CACHE" set_path fi fi if has_npx; then npx "$@" else if [[ -z $RUN_NODE_ERROR_MSG ]]; then echo "Couldn't find the npx binary. Ensure you have npm/npx installed. Open an issue on https://github.com/sindresorhus/run-node" else echo "$RUN_NODE_ERROR_MSG" fi fi