├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .npmrc ├── license ├── package.json ├── readme.md └── run-node /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | tidelift: npm/run-node 3 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v1 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "run-node", 3 | "version": "2.0.0", 4 | "description": "Run the Node.js binary no matter what", 5 | "license": "MIT", 6 | "repository": "sindresorhus/run-node", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "bin": "run-node", 14 | "engines": { 15 | "node": ">=10" 16 | }, 17 | "scripts": { 18 | "test": "./run-node --version" 19 | }, 20 | "files": [ 21 | "run-node" 22 | ], 23 | "keywords": [ 24 | "run", 25 | "node", 26 | "nodejs", 27 | "node.js", 28 | "find", 29 | "binary", 30 | "bin", 31 | "execute", 32 | "which", 33 | "detect", 34 | "path", 35 | "env", 36 | "bash", 37 | "shell", 38 | "sh" 39 | ] 40 | } 41 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # run-node 2 | 3 | > Run the Node.js binary no matter what 4 | 5 | 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. 6 | 7 | This Bash script uses some tricks to find the Node.js binary on your system and run it. 8 | 9 | Can be used from any environment that can spawn a process (Shell, Python, Ruby, Swift, Objective-C, etc). 10 | 11 | ### npm 12 | 13 | #### Install 14 | 15 | ``` 16 | $ npm install run-node 17 | ``` 18 | 19 | #### Usage 20 | 21 | ``` 22 | $ ./node_modules/.bin/run-node file.js 23 | ``` 24 | 25 | Or in an [npm run script](https://docs.npmjs.com/cli/run-script): 26 | 27 | ```json 28 | { 29 | "start": "run-node file.js" 30 | } 31 | ``` 32 | 33 | If the [`node`](https://www.npmjs.com/package/node) package is found in the local `node_modules` directory (for instance, if you have it installed as a [devDependency](https://docs.npmjs.com/files/package.json#devdependencies) of your npm project), this is the `node` binary that will be used. 34 | 35 | ### Manually 36 | 37 | #### Install 38 | 39 | Download the [run-node](run-node) file: 40 | 41 | ``` 42 | $ curl -sSLO https://github.com/sindresorhus/run-node/raw/main/run-node && chmod +x run-node 43 | ``` 44 | 45 | #### Usage 46 | 47 | ``` 48 | ./run-node file.js 49 | ``` 50 | 51 | #### Customizable cache path and error message 52 | 53 | 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`. 54 | 55 | Default config: 56 | 57 | ```sh 58 | 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" 59 | export RUN_NODE_CACHE_PATH="/home/username/.node_path" 60 | ``` 61 | 62 | 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. 63 | 64 | ## Maintainers 65 | 66 | - [Sindre Sorhus](https://github.com/sindresorhus) 67 | - [Mathias Fredriksson](https://github.com/mafredri) 68 | 69 | --- 70 | 71 |
72 | 73 | Get professional support for this package with a Tidelift subscription 74 | 75 |
76 | 77 | Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies. 78 |
79 |
80 | -------------------------------------------------------------------------------- /run-node: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # MIT License © Sindre Sorhus 3 | 4 | if [[ -f "$PWD/node_modules/node/bin/node" ]]; then 5 | PATH="$PWD/node_modules/node/bin:$PATH" 6 | export PATH 7 | fi 8 | 9 | if [[ -z $RUN_NODE_CACHE_PATH ]]; then 10 | PATH_CACHE="$HOME"/.node_path 11 | else 12 | PATH_CACHE="$RUN_NODE_CACHE_PATH" 13 | if [[ -f "$PATH_CACHE" ]]; then 14 | . "$PATH_CACHE" 15 | export PATH 16 | fi 17 | fi 18 | 19 | get_user_path() { 20 | [[ -x "/usr/libexec/path_helper" ]] && eval $(/usr/libexec/path_helper -s) 21 | echo "$($SHELL -i -l -c 'echo -e "\n"PATH=\"$PATH:\$PATH\""\n"' 2>/dev/null | grep "^PATH=")" > "$PATH_CACHE" 22 | } 23 | 24 | set_path() { 25 | if [[ -f "$PATH_CACHE" ]]; then 26 | . "$PATH_CACHE" 27 | else 28 | get_user_path 29 | . "$PATH_CACHE" 30 | fi 31 | 32 | export PATH 33 | } 34 | 35 | has_node() { 36 | command -v node >/dev/null 2>&1 37 | } 38 | 39 | if ! has_node; then 40 | set_path 41 | 42 | # Retry by deleting old path cache 43 | if ! has_node; then 44 | rm "$PATH_CACHE" 45 | set_path 46 | fi 47 | fi 48 | 49 | if has_node; then 50 | node "$@" 51 | else 52 | if [[ -z $RUN_NODE_ERROR_MSG ]]; then 53 | echo "Couldn't find the Node.js binary. Ensure you have Node.js installed. Open an issue on https://github.com/sindresorhus/run-node" 54 | else 55 | echo "$RUN_NODE_ERROR_MSG" 56 | fi 57 | fi 58 | --------------------------------------------------------------------------------