├── .all-contributorsrc ├── .editorconfig ├── .gitattributes ├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.yml │ ├── bug_types.yml │ └── feature_request.yml ├── pull_request_template.md └── workflows │ └── workflow.yml ├── .gitignore ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── ava.config.js ├── eslint.config.js ├── gulpfile.js ├── package-lock.json ├── package.json ├── prettier.config.js ├── src ├── constant.js ├── constant.test.js ├── helpers │ └── versions.test.js ├── lts.js ├── lts.test.js ├── main.d.ts ├── main.js ├── main.test-d.ts ├── main.test.js ├── nvm.js ├── nvm.test.js ├── options.js └── options.test.js └── tsconfig.json /.all-contributorsrc: -------------------------------------------------------------------------------- 1 | { 2 | "projectName": "node-version-alias", 3 | "projectOwner": "ehmicky", 4 | "repoType": "github", 5 | "repoHost": "https://github.com", 6 | "files": [ 7 | "README.md" 8 | ], 9 | "imageSize": 100, 10 | "commit": true, 11 | "linkToUsage": false, 12 | "contributors": [ 13 | { 14 | "login": "ehmicky", 15 | "name": "ehmicky", 16 | "avatar_url": "https://avatars2.githubusercontent.com/u/8136211?v=4", 17 | "profile": "https://fosstodon.org/@ehmicky", 18 | "contributions": [ 19 | "code", 20 | "design", 21 | "ideas", 22 | "doc" 23 | ] 24 | }, 25 | { 26 | "login": "AdrieanKhisbe", 27 | "name": "Adrien Becchis", 28 | "avatar_url": "https://avatars1.githubusercontent.com/u/2601132?v=4", 29 | "profile": "https://twitter.com/adrieankhisbe", 30 | "contributions": [ 31 | "code", 32 | "test", 33 | "ideas" 34 | ] 35 | }, 36 | { 37 | "login": "hongaar", 38 | "name": "Joram van den Boezem", 39 | "avatar_url": "https://avatars.githubusercontent.com/u/205834?v=4", 40 | "profile": "https://joram.dev", 41 | "contributions": [ 42 | "bug" 43 | ] 44 | }, 45 | { 46 | "login": "0xdevalias", 47 | "name": "Glenn 'devalias' Grant", 48 | "avatar_url": "https://avatars.githubusercontent.com/u/753891?v=4", 49 | "profile": "http://www.devalias.net/", 50 | "contributions": [ 51 | "doc", 52 | "question" 53 | ] 54 | } 55 | ], 56 | "contributorsPerLine": 7, 57 | "skipCi": true, 58 | "commitConvention": "none" 59 | } 60 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | max_line_length = 80 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.yml: -------------------------------------------------------------------------------- 1 | name: Bug report 2 | description: Report a bug 3 | title: Please replace with a clear and descriptive title 4 | labels: [bug] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: Thanks for reporting this bug! 9 | - type: checkboxes 10 | attributes: 11 | label: Guidelines 12 | options: 13 | - label: 14 | Please search other issues to make sure this bug has not already 15 | been reported. 16 | required: true 17 | - label: 18 | If this is related to a typo or the documentation being unclear, 19 | please click on the relevant page's `Edit` button (pencil icon) and 20 | suggest a correction instead. 21 | required: true 22 | - type: textarea 23 | attributes: 24 | label: Describe the bug 25 | placeholder: A clear and concise description of what the bug is. 26 | validations: 27 | required: true 28 | - type: textarea 29 | attributes: 30 | label: Steps to reproduce 31 | placeholder: | 32 | Step-by-step instructions on how to reproduce the behavior. 33 | Example: 34 | 1. Type the following command: [...] 35 | 2. etc. 36 | validations: 37 | required: true 38 | - type: textarea 39 | attributes: 40 | label: Configuration 41 | placeholder: Command line options and/or configuration file, if any. 42 | validations: 43 | required: true 44 | - type: textarea 45 | attributes: 46 | label: Environment 47 | description: | 48 | Enter the following command in a terminal and copy/paste its output: 49 | ```bash 50 | npx envinfo --system --binaries --browsers --npmPackages node-version-alias 51 | ``` 52 | validations: 53 | required: true 54 | - type: checkboxes 55 | attributes: 56 | label: Pull request (optional) 57 | description: 58 | Pull requests are welcome! If you would like to help us fix this bug, 59 | please check our [contributions 60 | guidelines](../blob/main/CONTRIBUTING.md). 61 | options: 62 | - label: I can submit a pull request. 63 | required: false 64 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_types.yml: -------------------------------------------------------------------------------- 1 | name: Bug report (TypeScript types) 2 | description: Report a bug about TypeScript types 3 | title: Please replace with a clear and descriptive title 4 | labels: [bug] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: Thanks for reporting this bug! 9 | - type: checkboxes 10 | attributes: 11 | label: Guidelines 12 | options: 13 | - label: 14 | Please search other issues to make sure this bug has not already 15 | been reported. 16 | required: true 17 | - type: textarea 18 | attributes: 19 | label: Describe the bug 20 | placeholder: A clear and concise description of what the bug is. 21 | validations: 22 | required: true 23 | - type: textarea 24 | attributes: 25 | label: Steps to reproduce 26 | description: | 27 | Please reproduce the bug using the [TypeScript playground](https://www.typescriptlang.org/play) or [Bug workbench](https://www.typescriptlang.org/dev/bug-workbench), then paste the URL here. 28 | validations: 29 | required: true 30 | - type: textarea 31 | attributes: 32 | label: Environment 33 | description: | 34 | Enter the following command in a terminal and copy/paste its output: 35 | ```bash 36 | npx envinfo --system --binaries --browsers --npmPackages node-version-alias,typescript --npmGlobalPackages typescript 37 | ``` 38 | validations: 39 | required: true 40 | - type: checkboxes 41 | attributes: 42 | label: Pull request (optional) 43 | description: 44 | Pull requests are welcome! If you would like to help us fix this bug, 45 | please check our [contributions 46 | guidelines](../blob/main/CONTRIBUTING.md). 47 | options: 48 | - label: I can submit a pull request. 49 | required: false 50 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.yml: -------------------------------------------------------------------------------- 1 | name: Feature request 2 | description: Suggest an idea for this project 3 | title: Please replace with a clear and descriptive title 4 | labels: [enhancement] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: Thanks for suggesting a new feature! 9 | - type: checkboxes 10 | attributes: 11 | label: Guidelines 12 | options: 13 | - label: 14 | Please search other issues to make sure this feature has not already 15 | been requested. 16 | required: true 17 | - type: textarea 18 | attributes: 19 | label: Which problem is this feature request solving? 20 | placeholder: I'm always frustrated when [...] 21 | validations: 22 | required: true 23 | - type: textarea 24 | attributes: 25 | label: Describe the solution you'd like 26 | placeholder: This could be fixed by [...] 27 | validations: 28 | required: true 29 | - type: checkboxes 30 | attributes: 31 | label: Pull request (optional) 32 | description: 33 | Pull requests are welcome! If you would like to help us fix this bug, 34 | please check our [contributions 35 | guidelines](../blob/main/CONTRIBUTING.md). 36 | options: 37 | - label: I can submit a pull request. 38 | required: false 39 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 🎉 Thanks for sending this pull request! 🎉 2 | 3 | Please make sure the title is clear and descriptive. 4 | 5 | If you are fixing a typo or documentation, please skip these instructions. 6 | 7 | Otherwise please fill in the sections below. 8 | 9 | **Which problem is this pull request solving?** 10 | 11 | Example: I'm always frustrated when [...] 12 | 13 | **List other issues or pull requests related to this problem** 14 | 15 | Example: This fixes #5012 16 | 17 | **Describe the solution you've chosen** 18 | 19 | Example: I've fixed this by [...] 20 | 21 | **Describe alternatives you've considered** 22 | 23 | Example: Another solution would be [...] 24 | 25 | **Checklist** 26 | 27 | Please add a `x` inside each checkbox: 28 | 29 | - [ ] I have read the [contribution guidelines](../blob/main/CONTRIBUTING.md). 30 | - [ ] I have added tests (we are enforcing 100% test coverage). 31 | - [ ] I have added documentation in the `README.md`, the `docs` directory (if 32 | any) 33 | - [ ] The status checks are successful (continuous integration). Those can be 34 | seen below. 35 | -------------------------------------------------------------------------------- /.github/workflows/workflow.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | on: [push, pull_request] 3 | jobs: 4 | combinations: 5 | uses: ehmicky/dev-tasks/.github/workflows/build.yml@main 6 | secrets: inherit 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.swp 3 | npm-debug.log 4 | node_modules 5 | /core 6 | .eslintcache 7 | .lycheecache 8 | .npmrc 9 | .yarn-error.log 10 | !.github/ 11 | /coverage 12 | /build 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 5.0.1 2 | 3 | ## Bug fixes 4 | 5 | - Fix [`lts/-1`](https://github.com/nvm-sh/nvm#long-term-support) shortcut 6 | 7 | # 5.0.0 8 | 9 | ## Breaking changes 10 | 11 | - Minimal supported Node.js version is now `18.18.0` 12 | 13 | # 4.1.0 14 | 15 | ## Features 16 | 17 | - Add a [`signal` option](README.md#signal) to cancel 18 | 19 | # 4.0.0 20 | 21 | ## Breaking changes 22 | 23 | - Minimal supported Node.js version is now `16.17.0` 24 | 25 | # 3.4.1 26 | 27 | ## Bug fixes 28 | 29 | - This `lts/-1`, `lts/-2`, etc. aliases. Those were 30 | [one LTS version too high](https://github.com/ehmicky/node-version-alias/issues/8). 31 | 32 | # 3.4.0 33 | 34 | ## Features 35 | 36 | - Improve tree-shaking support 37 | 38 | # 3.3.0 39 | 40 | ## Features 41 | 42 | - Reduce npm package size by 68% 43 | 44 | # 3.2.0 45 | 46 | ## Features 47 | 48 | - Reduce npm package size 49 | 50 | # 3.1.0 51 | 52 | ## Features 53 | 54 | - Add TypeScript types 55 | 56 | # 3.0.1 57 | 58 | ## Dependencies 59 | 60 | - Upgrade [`all-node-versions`](https://github.com/ehmicky/all-node-versions) 61 | 62 | # 3.0.0 63 | 64 | ## Breaking changes 65 | 66 | - Minimal supported Node.js version is now `14.18.0` 67 | 68 | # 2.0.0 69 | 70 | ## Breaking changes 71 | 72 | - Minimal supported Node.js version is now `12.20.0` 73 | - This package is now an ES module. It can only be loaded with an `import` or 74 | `import()` statement, not `require()`. See 75 | [this post for more information](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). 76 | 77 | # 1.0.1 78 | 79 | ## Dependencies 80 | 81 | - Upgrade `normalize-node-version` 82 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | This text is available in 4 | [many other languages](https://www.contributor-covenant.org/translations). 5 | 6 | ## Our Pledge 7 | 8 | We as members, contributors, and leaders pledge to make participation in our 9 | community a harassment-free experience for everyone, regardless of age, body 10 | size, visible or invisible disability, ethnicity, sex characteristics, gender 11 | identity and expression, level of experience, education, socio-economic status, 12 | nationality, personal appearance, race, religion, or sexual identity and 13 | orientation. 14 | 15 | We pledge to act and interact in ways that contribute to an open, welcoming, 16 | diverse, inclusive, and healthy community. 17 | 18 | ## Our Standards 19 | 20 | Examples of behavior that contributes to a positive environment for our 21 | community include: 22 | 23 | - Demonstrating empathy and kindness toward other people 24 | - Being respectful of differing opinions, viewpoints, and experiences 25 | - Giving and gracefully accepting constructive feedback 26 | - Accepting responsibility and apologizing to those affected by our mistakes, 27 | and learning from the experience 28 | - Focusing on what is best not just for us as individuals, but for the overall 29 | community 30 | 31 | Examples of unacceptable behavior include: 32 | 33 | - The use of sexualized language or imagery, and sexual attention or advances of 34 | any kind 35 | - Trolling, insulting or derogatory comments, and personal or political attacks 36 | - Public or private harassment 37 | - Publishing others' private information, such as a physical or email address, 38 | without their explicit permission 39 | - Other conduct which could reasonably be considered inappropriate in a 40 | professional setting 41 | 42 | ## Enforcement Responsibilities 43 | 44 | Community leaders are responsible for clarifying and enforcing our standards of 45 | acceptable behavior and will take appropriate and fair corrective action in 46 | response to any behavior that they deem inappropriate, threatening, offensive, 47 | or harmful. 48 | 49 | Community leaders have the right and responsibility to remove, edit, or reject 50 | comments, commits, code, wiki edits, issues, and other contributions that are 51 | not aligned to this Code of Conduct, and will communicate reasons for moderation 52 | decisions when appropriate. 53 | 54 | ## Scope 55 | 56 | This Code of Conduct applies within all community spaces, and also applies when 57 | an individual is officially representing the community in public spaces. 58 | Examples of representing our community include using an official e-mail address, 59 | posting via an official social media account, or acting as an appointed 60 | representative at an online or offline event. 61 | 62 | ## Enforcement 63 | 64 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 65 | reported to the community leaders responsible for enforcement at 66 | ehmicky+report@gmail.com All complaints will be reviewed and investigated 67 | promptly and fairly. 68 | 69 | All community leaders are obligated to respect the privacy and security of the 70 | reporter of any incident. 71 | 72 | ## Enforcement Guidelines 73 | 74 | Community leaders will follow these Community Impact Guidelines in determining 75 | the consequences for any action they deem in violation of this Code of Conduct: 76 | 77 | ### 1. Correction 78 | 79 | **Community Impact**: Use of inappropriate language or other behavior deemed 80 | unprofessional or unwelcome in the community. 81 | 82 | **Consequence**: A private, written warning from community leaders, providing 83 | clarity around the nature of the violation and an explanation of why the 84 | behavior was inappropriate. A public apology may be requested. 85 | 86 | ### 2. Warning 87 | 88 | **Community Impact**: A violation through a single incident or series of 89 | actions. 90 | 91 | **Consequence**: A warning with consequences for continued behavior. No 92 | interaction with the people involved, including unsolicited interaction with 93 | those enforcing the Code of Conduct, for a specified period of time. This 94 | includes avoiding interactions in community spaces as well as external channels 95 | like social media. Violating these terms may lead to a temporary or permanent 96 | ban. 97 | 98 | ### 3. Temporary Ban 99 | 100 | **Community Impact**: A serious violation of community standards, including 101 | sustained inappropriate behavior. 102 | 103 | **Consequence**: A temporary ban from any sort of interaction or public 104 | communication with the community for a specified period of time. No public or 105 | private interaction with the people involved, including unsolicited interaction 106 | with those enforcing the Code of Conduct, is allowed during this period. 107 | Violating these terms may lead to a permanent ban. 108 | 109 | ### 4. Permanent Ban 110 | 111 | **Community Impact**: Demonstrating a pattern of violation of community 112 | standards, including sustained inappropriate behavior, harassment of an 113 | individual, or aggression toward or disparagement of classes of individuals. 114 | 115 | **Consequence**: A permanent ban from any sort of public interaction within the 116 | community. 117 | 118 | ## Attribution 119 | 120 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 121 | version 2.0, available at 122 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 123 | 124 | Community Impact Guidelines were inspired by 125 | [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). 126 | 127 | [homepage]: https://www.contributor-covenant.org 128 | 129 | For answers to common questions about this code of conduct, see the FAQ at 130 | https://www.contributor-covenant.org/faq. Translations are available at 131 | https://www.contributor-covenant.org/translations. 132 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributions 2 | 3 | 🎉 Thanks for considering contributing to this project! 🎉 4 | 5 | These guidelines will help you send a pull request. 6 | 7 | If you're submitting an issue instead, please skip this document. 8 | 9 | If your pull request is related to a typo or the documentation being unclear, 10 | please click on the relevant page's `Edit` button (pencil icon) and directly 11 | suggest a correction instead. 12 | 13 | This project was made with ❤️. The simplest way to give back is by starring and 14 | sharing it online. 15 | 16 | Everyone is welcome regardless of personal background. We enforce a 17 | [Code of conduct](CODE_OF_CONDUCT.md) in order to promote a positive and 18 | inclusive environment. 19 | 20 | # Development process 21 | 22 | First fork and clone the repository. If you're not sure how to do this, please 23 | watch 24 | [these videos](https://egghead.io/courses/how-to-contribute-to-an-open-source-project-on-github). 25 | 26 | Run: 27 | 28 | ```bash 29 | npm install 30 | ``` 31 | 32 | Make sure everything is correctly setup with: 33 | 34 | ```bash 35 | npm test 36 | ``` 37 | 38 | We use Gulp tasks to lint, test and build this project. Please check 39 | [dev-tasks](https://github.com/ehmicky/dev-tasks/blob/main/README.md) to learn 40 | how to use them. You don't need to know Gulp to use these tasks. 41 | 42 | # Requirements 43 | 44 | Our coding style is documented 45 | [here](https://github.com/ehmicky/eslint-config#coding-style). Linting and 46 | formatting should automatically handle it though. 47 | 48 | After submitting the pull request, please make sure the Continuous Integration 49 | checks are passing. 50 | 51 | We enforce 100% test coverage: each line of code must be tested. 52 | 53 | New options, methods, properties, configuration and behavior must be documented 54 | in all of these: 55 | 56 | - the `README.md` 57 | - the `docs` directory (if any) 58 | 59 | Please use the same style as the rest of the documentation and examples. 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2025 ehmicky 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Node](https://img.shields.io/badge/-Node.js-808080?logo=node.js&colorA=404040&logoColor=66cc33)](https://www.npmjs.com/package/node-version-alias) 2 | [![TypeScript](https://img.shields.io/badge/-Typed-808080?logo=typescript&colorA=404040&logoColor=0096ff)](/src/main.d.ts) 3 | [![Codecov](https://img.shields.io/badge/-Tested%20100%25-808080?logo=codecov&colorA=404040)](https://codecov.io/gh/ehmicky/node-version-alias) 4 | [![Mastodon](https://img.shields.io/badge/-Mastodon-808080.svg?logo=mastodon&colorA=404040&logoColor=9590F9)](https://fosstodon.org/@ehmicky) 5 | [![Medium](https://img.shields.io/badge/-Medium-808080.svg?logo=medium&colorA=404040)](https://medium.com/@ehmicky) 6 | 7 | Resolve Node.js version aliases like `latest`, `lts` or `erbium`. 8 | 9 | Those aliases are used by Node.js version managers like 10 | [`nvm`](https://github.com/nvm-sh/nvm), 11 | [`nvs`](https://github.com/jasongin/nvs), [`n`](https://github.com/tj/n), 12 | [`nave`](https://github.com/isaacs/nave), 13 | [`nodeenv`](https://github.com/ekalinin/nodeenv) or 14 | [`nodist`](https://github.com/nullivex/nodist). 15 | 16 | This resolves them to a `"major.minor.patch"` version string. The following 17 | aliases are supported: 18 | 19 | - [`latest`](https://github.com/tj/n#specifying-node-versions), 20 | [`stable`](https://github.com/nvm-sh/nvm#usage), 21 | [`node`](https://github.com/nvm-sh/nvm#usage), 22 | [`current`](https://github.com/tj/n#specifying-node-versions): latest version 23 | - [`lts`](https://github.com/jasongin/nvs#basic-usage) or 24 | [`lts/*`](https://github.com/nvm-sh/nvm#long-term-support): latest LTS version 25 | - [`lts/-1`](https://github.com/nvm-sh/nvm#long-term-support), 26 | [`lts/-2`](https://github.com/nvm-sh/nvm#long-term-support), etc.: 27 | first/second/etc. latest LTS version 28 | - [`lts/erbium`](https://github.com/nvm-sh/nvm#long-term-support), 29 | [`erbium`](https://github.com/nvm-sh/nvm#long-term-support), etc.: specific 30 | LTS, using its [name](https://github.com/nodejs/Release) (case-insensitive) 31 | - nvm custom aliases (including `default`) 32 | - [`system`](https://github.com/nvm-sh/nvm#system-version-of-node): Node.js 33 | version when `nvm` is deactivated 34 | - [`iojs`](https://github.com/nvm-sh/nvm#usage): always `4.0.0` 35 | - [`unstable`](https://github.com/nvm-sh/nvm#usage): always `0.11.6` 36 | 37 | Normal version ranges (like `12.1.0`, `12` or `>=10`) are valid inputs too. 38 | 39 | # Examples 40 | 41 | ```js 42 | import nodeVersionAlias from 'node-version-alias' 43 | 44 | // Note: the following examples might be out-of-sync with the actual versions 45 | console.log(await nodeVersionAlias('latest')) // 19.3.0 46 | console.log(await nodeVersionAlias('lts')) // 18.12.1 47 | console.log(await nodeVersionAlias('lts/erbium')) // 12.22.12 48 | console.log(await nodeVersionAlias('erbium')) // 12.22.12 49 | console.log(await nodeVersionAlias('lts/-2')) // 14.21.2 50 | 51 | // Normal version ranges 52 | console.log(await nodeVersionAlias('10.0.0')) // 10.0.0 53 | console.log(await nodeVersionAlias('10')) // 10.24.1 54 | console.log(await nodeVersionAlias('^10')) // 10.24.1 55 | console.log(await nodeVersionAlias('>=10')) // 19.3.0 56 | 57 | // Allowed options 58 | await nodeVersionAlias('latest', { 59 | // Use a mirror for Node.js binaries 60 | mirror: 'https://npmmirror.com/mirrors/node', 61 | // Do not cache the list of available Node.js versions 62 | fetch: true, 63 | // Cancels when the signal is aborted 64 | signal: new AbortController().signal, 65 | }) 66 | ``` 67 | 68 | # Install 69 | 70 | ```bash 71 | npm install node-version-alias 72 | ``` 73 | 74 | This package works in Node.js >=18.18.0. 75 | 76 | This is an ES module. It must be loaded using 77 | [an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c), 78 | not `require()`. If TypeScript is used, it must be configured to 79 | [output ES modules](https://www.typescriptlang.org/docs/handbook/esm-node.html), 80 | not CommonJS. 81 | 82 | # Usage 83 | 84 | ## nodeVersionAlias(alias, options?) 85 | 86 | `alias`: `string`\ 87 | `options`: [`Options?`](#options)\ 88 | _Returns_: `Promise` 89 | 90 | The return value resolves to a `"major.minor.patch"` version string. 91 | 92 | ### Options 93 | 94 | #### mirror 95 | 96 | _Type_: `string`\ 97 | _Default_: `https://nodejs.org/dist` 98 | 99 | Base URL to fetch the list of available Node.js versions. Can be customized (for 100 | example `https://npmmirror.com/mirrors/node`). 101 | 102 | The following environment variables can also be used: `NODE_MIRROR`, 103 | `NVM_NODEJS_ORG_MIRROR`, `N_NODE_MIRROR` or `NODIST_NODE_MIRROR`. 104 | 105 | #### fetch 106 | 107 | _Type_: `boolean`\ 108 | _Default_: `undefined` 109 | 110 | The list of available Node.js versions is cached for one hour by default. If the 111 | `fetch` option is: 112 | 113 | - `true`: the cache will not be used 114 | - `false`: the cache will be used even if it's older than one hour 115 | 116 | #### signal 117 | 118 | _Type_: 119 | [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) 120 | 121 | Cancels when the signal is aborted. 122 | 123 | # CLI 124 | 125 | [`nve`](https://github.com/ehmicky/nve) can be used to 126 | [run `node-version-alias` as a CLI](https://github.com/ehmicky/nve#examples-list-versions). 127 | The [`--mirror`](https://github.com/ehmicky/nve#--mirror) and 128 | [`--fetch`](https://github.com/ehmicky/nve#--fetch) CLI flags are available. 129 | 130 | ```bash 131 | # Prints latest Node.js version 132 | $ nve latest 133 | 19.3.0 134 | 135 | # Prints latest Node.js 8 version 136 | $ nve 8 137 | 8.17.0 138 | 139 | # Prints latest Node.js 12, 10 and 8 versions 140 | $ nve 12,10,8 141 | 12.22.1 142 | 10.24.1 143 | 8.17.0 144 | ``` 145 | 146 | # See also 147 | 148 | - [`nvexeca`](https://github.com/ehmicky/nve): Run a specific Node.js version 149 | (programmatic) 150 | - [`get-node`](https://github.com/ehmicky/get-node): Download Node.js 151 | - [`normalize-node-version`](https://github.com/ehmicky/normalize-node-version): 152 | Normalize and validate Node.js versions 153 | - [`preferred-node-version`](https://github.com/ehmicky/preferred-node-version): 154 | Get the preferred Node.js version of a project or user 155 | - [`all-node-versions`](https://github.com/ehmicky/all-node-versions): List all 156 | available Node.js versions 157 | - [`fetch-node-website`](https://github.com/ehmicky/fetch-node-website): Fetch 158 | releases on nodejs.org 159 | 160 | # Support 161 | 162 | For any question, _don't hesitate_ to [submit an issue on GitHub](../../issues). 163 | 164 | Everyone is welcome regardless of personal background. We enforce a 165 | [Code of conduct](CODE_OF_CONDUCT.md) in order to promote a positive and 166 | inclusive environment. 167 | 168 | # Contributing 169 | 170 | This project was made with ❤️. The simplest way to give back is by starring and 171 | sharing it online. 172 | 173 | If the documentation is unclear or has a typo, please click on the page's `Edit` 174 | button (pencil icon) and suggest a correction. 175 | 176 | If you would like to help us fix a bug or add a new feature, please check our 177 | [guidelines](CONTRIBUTING.md). Pull requests are welcome! 178 | 179 | Thanks go to our wonderful contributors: 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 |
ehmicky
ehmicky

💻 🎨 🤔 📖
Adrien Becchis
Adrien Becchis

💻 ⚠️ 🤔
Joram van den Boezem
Joram van den Boezem

🐛
Glenn 'devalias' Grant
Glenn 'devalias' Grant

📖 💬
194 | 195 | 196 | 197 | 198 | 199 | -------------------------------------------------------------------------------- /ava.config.js: -------------------------------------------------------------------------------- 1 | export { default } from '@ehmicky/dev-tasks/ava.config.js' 2 | -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | export { default } from '@ehmicky/eslint-config' 2 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | export * from '@ehmicky/dev-tasks' 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-version-alias", 3 | "version": "5.0.1", 4 | "type": "module", 5 | "exports": { 6 | "types": "./build/src/main.d.ts", 7 | "default": "./build/src/main.js" 8 | }, 9 | "main": "./build/src/main.js", 10 | "types": "./build/src/main.d.ts", 11 | "files": [ 12 | "build/src/**/*.{js,json,d.ts}", 13 | "!build/src/**/*.test.js", 14 | "!build/src/{helpers,fixtures}" 15 | ], 16 | "sideEffects": false, 17 | "scripts": { 18 | "test": "gulp test" 19 | }, 20 | "description": "Resolve Node.js version aliases like 'latest', 'lts' or 'erbium'", 21 | "keywords": [ 22 | "nodejs", 23 | "node", 24 | "nvm", 25 | "npx", 26 | "versions", 27 | "versioning", 28 | "exec", 29 | "shell", 30 | "terminal", 31 | "command-line", 32 | "cli", 33 | "dependency-management", 34 | "es6", 35 | "javascript", 36 | "typescript", 37 | "library", 38 | "npmjs", 39 | "operating-system", 40 | "package-manager", 41 | "bash" 42 | ], 43 | "license": "Apache-2.0", 44 | "homepage": "https://www.github.com/ehmicky/node-version-alias", 45 | "repository": { 46 | "type": "git", 47 | "url": "git+https://github.com/ehmicky/node-version-alias.git" 48 | }, 49 | "bugs": { 50 | "url": "https://github.com/ehmicky/node-version-alias/issues" 51 | }, 52 | "author": "ehmicky (https://github.com/ehmicky)", 53 | "directories": { 54 | "lib": "src" 55 | }, 56 | "dependencies": { 57 | "all-node-versions": "^13.0.1", 58 | "filter-obj": "^6.1.0", 59 | "is-plain-obj": "^4.1.0", 60 | "normalize-node-version": "^14.0.1", 61 | "path-exists": "^5.0.0", 62 | "semver": "^7.7.1" 63 | }, 64 | "devDependencies": { 65 | "@ehmicky/dev-tasks": "^3.0.34", 66 | "@ehmicky/eslint-config": "^20.0.32", 67 | "@ehmicky/prettier-config": "^1.0.6", 68 | "got": "^13.0.0", 69 | "test-each": "^7.0.1" 70 | }, 71 | "engines": { 72 | "node": ">=18.18.0" 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /prettier.config.js: -------------------------------------------------------------------------------- 1 | export { default } from '@ehmicky/prettier-config' 2 | -------------------------------------------------------------------------------- /src/constant.js: -------------------------------------------------------------------------------- 1 | import { getNvmSystemVersion } from './nvm.js' 2 | 3 | // Resolve aliases whose name is a constant and is not LTS-related 4 | export const getConstantAlias = (alias) => { 5 | const versionRange = ALIASES[alias] 6 | 7 | if (versionRange === undefined) { 8 | return 9 | } 10 | 11 | if (typeof versionRange !== 'function') { 12 | return versionRange 13 | } 14 | 15 | return versionRange() 16 | } 17 | 18 | const ALIASES = { 19 | // Latest version (nave, nvm-windows, n, nvs, nodebrew, nodist, fish-nvm) 20 | latest: '*', 21 | // Latest version (nvm, nave, nodebrew) 22 | stable: '*', 23 | // Latest version (nvm) 24 | node: '*', 25 | // Latest version (n, fish-nvm) 26 | current: '*', 27 | // Version if nvm was not installed 28 | system: getNvmSystemVersion, 29 | // Alias from nvm. Now that iojs is merged to Node.js, it is always this 30 | // version. 31 | iojs: '4.0.0', 32 | // Old deprecated nvm alias 33 | unstable: '0.11', 34 | } 35 | -------------------------------------------------------------------------------- /src/constant.test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | import nodeVersionAlias from 'node-version-alias' 3 | import { each } from 'test-each' 4 | 5 | import { IOJS_VERSION, UNSTABLE_VERSION } from './helpers/versions.test.js' 6 | 7 | each(['latest', 'stable', 'node', 'current'], ({ title }, alias) => { 8 | test(`Latest version | ${title}`, async (t) => { 9 | const [version, latestVersion] = await Promise.all([ 10 | nodeVersionAlias(alias), 11 | nodeVersionAlias('*'), 12 | ]) 13 | t.is(version, latestVersion) 14 | }) 15 | }) 16 | 17 | test('iojs', async (t) => { 18 | const version = await nodeVersionAlias('iojs') 19 | t.is(version, IOJS_VERSION) 20 | }) 21 | 22 | test('unstable', async (t) => { 23 | const version = await nodeVersionAlias('unstable') 24 | t.is(version, UNSTABLE_VERSION) 25 | }) 26 | -------------------------------------------------------------------------------- /src/helpers/versions.test.js: -------------------------------------------------------------------------------- 1 | export const FULL_VERSION = '6.0.0' 2 | export const MAJOR_VERSION = '6' 3 | export const MAJOR_FULL_VERSION = '6.17.1' 4 | export const LATEST_BORON = '6.17.1' 5 | export const UNKNOWN_VERSION = '6.99.0' 6 | export const UNKNOWN_ALIAS = 'unknown' 7 | export const IOJS_VERSION = '4.0.0' 8 | export const UNSTABLE_VERSION = '0.11.16' 9 | -------------------------------------------------------------------------------- /src/lts.js: -------------------------------------------------------------------------------- 1 | import allNodeVersions from 'all-node-versions' 2 | 3 | // Normalize `lts`, `lts/*`, `lts/-num` and `[lts/]name` aliases used by 4 | // `.nvmrc` and others 5 | export const getLtsAlias = async (alias, allNodeOpts) => { 6 | const ltsMajors = await getLtsMajors(allNodeOpts) 7 | const major = getLtsMajor(alias, ltsMajors) 8 | 9 | if (major === undefined) { 10 | return 11 | } 12 | 13 | return major.latest 14 | } 15 | 16 | // Retrieve all major releases that are LTS 17 | export const getLtsMajors = async (allNodeOpts) => { 18 | const { majors } = await allNodeVersions(allNodeOpts) 19 | return majors.filter(isLts) 20 | } 21 | 22 | const isLts = ({ lts }) => lts !== undefined 23 | 24 | // Find the LTS that matches the alias 25 | const getLtsMajor = (alias, ltsMajors) => { 26 | if (LATEST_LTS.has(alias)) { 27 | return ltsMajors[0] 28 | } 29 | 30 | const major = getNumberedLts(alias, ltsMajors) 31 | 32 | if (major !== undefined) { 33 | return major 34 | } 35 | 36 | return getNamedLts(alias, ltsMajors) 37 | } 38 | 39 | // Those aliases mean the latest LTS 40 | // `lts` is used by n, nave, nvs, fish-nvm 41 | // `lts/*` is used by nvm, nave, nvs 42 | const LATEST_LTS = new Set(['lts', 'lts/*']) 43 | 44 | // `lts/-num` means the numth+1 latest LTS. 45 | // Used by nvm 46 | const getNumberedLts = (alias, ltsMajors) => { 47 | const result = NUMBER_LTS_REGEXP.exec(alias) 48 | return result === null || result[1] === '0' ? undefined : ltsMajors[result[1]] 49 | } 50 | 51 | const NUMBER_LTS_REGEXP = /^lts\/-(\d+)$/u 52 | 53 | // `lts/name` or just `name` means a specific LTS named likewise. 54 | // Used by nvm, nave, nvs, fish-nvm 55 | const getNamedLts = (alias, ltsMajors) => { 56 | const name = alias.replace(LTS_PREFIX, '').toLowerCase() 57 | return ltsMajors.find(({ lts }) => lts === name) 58 | } 59 | 60 | const LTS_PREFIX = 'lts/' 61 | -------------------------------------------------------------------------------- /src/lts.test.js: -------------------------------------------------------------------------------- 1 | import allNodeVersions from 'all-node-versions' 2 | import test from 'ava' 3 | import nodeVersionAlias from 'node-version-alias' 4 | import semver from 'semver' 5 | import { each } from 'test-each' 6 | 7 | import { LATEST_BORON } from './helpers/versions.test.js' 8 | 9 | const getLatestFromMajor = async (version) => { 10 | const { versions } = await allNodeVersions() 11 | const versionsA = versions.map(getNodeVersion) 12 | const majorVersion = semver.major(version) 13 | return versionsA.find((versionA) => semver.major(versionA) === majorVersion) 14 | } 15 | 16 | const getNodeVersion = ({ node }) => node 17 | 18 | each(['lts/-99', 'lts/-0', 'lts/unknown'], ({ title }, alias) => { 19 | test(`Validates "lts/" | ${title}`, async (t) => { 20 | await t.throwsAsync(nodeVersionAlias(alias)) 21 | }) 22 | }) 23 | 24 | each(['lts', 'lts/*'], ({ title }, alias) => { 25 | test(`Can target latest LTS | ${title}`, async (t) => { 26 | const version = await nodeVersionAlias(alias) 27 | t.is(await getLatestFromMajor(version), version) 28 | }) 29 | }) 30 | 31 | test('Can use "lts/-number"', async (t) => { 32 | const [ltsOne, ltsTwo] = await Promise.all([ 33 | nodeVersionAlias('lts/-1'), 34 | nodeVersionAlias('lts/-2'), 35 | ]) 36 | 37 | t.is(await getLatestFromMajor(ltsOne), ltsOne) 38 | t.is(await getLatestFromMajor(ltsTwo), ltsTwo) 39 | 40 | t.true(semver.gt(ltsOne, ltsTwo)) 41 | }) 42 | 43 | each(['lts/boron', 'boron'], ({ title }, alias) => { 44 | test(`Can use "lts" by name | ${title}`, async (t) => { 45 | const version = await nodeVersionAlias(alias) 46 | t.is(version, LATEST_BORON) 47 | }) 48 | }) 49 | -------------------------------------------------------------------------------- /src/main.d.ts: -------------------------------------------------------------------------------- 1 | import type { Options as AllNodeVersionsOptions } from 'all-node-versions' 2 | import type { 3 | Options as NormalizeVersionOptions, 4 | SemverVersion, 5 | } from 'normalize-node-version' 6 | 7 | export type { SemverVersion } 8 | 9 | // @ts-error @typescript-eslint/no-duplicate-type-constituents 10 | type UpstreamOptions = AllNodeVersionsOptions & NormalizeVersionOptions 11 | 12 | export interface Options { 13 | /** 14 | * Base URL. 15 | * Can be customized (for example `https://npmmirror.com/mirrors/node`). 16 | * 17 | * The following environment variables can also be used: `NODE_MIRROR`, 18 | * `NVM_NODEJS_ORG_MIRROR`, `N_NODE_MIRROR` or `NODIST_NODE_MIRROR`. 19 | * 20 | * @default 'https://nodejs.org/dist' 21 | */ 22 | mirror?: UpstreamOptions['mirror'] 23 | 24 | /** 25 | * Cancels when the signal is aborted. 26 | */ 27 | signal?: UpstreamOptions['signal'] 28 | 29 | /** 30 | * The list of available Node.js versions is cached for one hour by default. 31 | * If the `fetch` option is: 32 | * - `true`: the cache will not be used 33 | * - `false`: the cache will be used even if it's older than one hour 34 | * 35 | * @default `undefined` 36 | */ 37 | fetch?: UpstreamOptions['fetch'] 38 | } 39 | 40 | /** 41 | * Resolve Node.js version aliases like `latest`, `lts` or `erbium`. 42 | * Those aliases are used by Node.js version managers like 43 | * [`nvm`](https://github.com/nvm-sh/nvm), 44 | * [`nvs`](https://github.com/jasongin/nvs), [`n`](https://github.com/tj/n), 45 | * [`nave`](https://github.com/isaacs/nave), 46 | * [`nodeenv`](https://github.com/ekalinin/nodeenv) or 47 | * [`nodist`](https://github.com/nullivex/nodist). 48 | * 49 | * This resolves them to a `"major.minor.patch"` version string. The following 50 | * aliases are supported: 51 | * - [`latest`](https://github.com/tj/n#specifying-node-versions), 52 | * [`stable`](https://github.com/nvm-sh/nvm#usage), 53 | * [`node`](https://github.com/nvm-sh/nvm#usage), 54 | * [`current`](https://github.com/tj/n#specifying-node-versions): latest version 55 | * - [`lts`](https://github.com/jasongin/nvs#basic-usage) or 56 | * [`lts/*`](https://github.com/nvm-sh/nvm#long-term-support): latest LTS version 57 | * - [`lts/-1`](https://github.com/nvm-sh/nvm#long-term-support), 58 | * [`lts/-2`](https://github.com/nvm-sh/nvm#long-term-support), etc.: 59 | * first/second/etc. latest LTS version 60 | * - [`lts/erbium`](https://github.com/nvm-sh/nvm#long-term-support), 61 | * [`erbium`](https://github.com/nvm-sh/nvm#long-term-support), etc.: specific 62 | * LTS, using its [name](https://github.com/nodejs/Release) (case-insensitive) 63 | * - nvm custom aliases (including `default`) 64 | * - [`system`](https://github.com/nvm-sh/nvm#system-version-of-node): Node.js 65 | * version when `nvm` is deactivated 66 | * - [`iojs`](https://github.com/nvm-sh/nvm#usage): always `4.0.0` 67 | * - [`unstable`](https://github.com/nvm-sh/nvm#usage): always `0.11.6` 68 | * 69 | * Normal version ranges (like `12.1.0`, `12` or `>=10`) are valid inputs too. 70 | * 71 | * @example 72 | * ```js 73 | * // Note: the following examples might be out-of-sync with the actual versions 74 | * console.log(await nodeVersionAlias('latest')) // 18.4.0 75 | * console.log(await nodeVersionAlias('lts')) // 16.15.1 76 | * console.log(await nodeVersionAlias('lts/erbium')) // 12.22.12 77 | * console.log(await nodeVersionAlias('erbium')) // 12.22.12 78 | * console.log(await nodeVersionAlias('lts/-2')) // 14.19.3 79 | * 80 | * // Normal version ranges 81 | * console.log(await nodeVersionAlias('10.0.0')) // 10.0.0 82 | * console.log(await nodeVersionAlias('10')) // 10.24.1 83 | * console.log(await nodeVersionAlias('^10')) // 10.24.1 84 | * console.log(await nodeVersionAlias('>=10')) // 18.4.0 85 | * 86 | * // Allowed options 87 | * await nodeVersionAlias('latest', { 88 | * // Use a mirror for Node.js binaries 89 | * mirror: 'https://npmmirror.com/mirrors/node', 90 | * // Do not cache the list of available Node.js versions 91 | * fetch: true, 92 | * // Cancels when the signal is aborted 93 | * signal: new AbortController().signal, 94 | * }) 95 | * ``` 96 | */ 97 | export default function nodeVersionAlias( 98 | alias: string, 99 | options?: Options, 100 | ): Promise 101 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | import normalizeNodeVersion from 'normalize-node-version' 2 | import semver from 'semver' 3 | 4 | import { getConstantAlias } from './constant.js' 5 | import { getLtsAlias } from './lts.js' 6 | import { getNvmCustomAlias } from './nvm.js' 7 | import { getOpts } from './options.js' 8 | 9 | // Resolve Node.js version managers aliases like `latest`, `lts` or `erbium`. 10 | // First resolve them to a version range, then to a full version. 11 | const nodeVersionAlias = async (alias, opts) => { 12 | const { allNodeOpts, normalizeOpts } = getOpts(opts) 13 | const versionRange = await getVersionRange(alias, allNodeOpts) 14 | 15 | if (versionRange === undefined) { 16 | throw new Error(`Invalid Node.js version alias: ${alias}`) 17 | } 18 | 19 | const version = await normalizeNodeVersion(versionRange, normalizeOpts) 20 | return version 21 | } 22 | 23 | export default nodeVersionAlias 24 | 25 | const getVersionRange = async (alias, allNodeOpts) => { 26 | if (semver.validRange(alias) !== null) { 27 | return alias 28 | } 29 | 30 | const versionRange = await getConstantAlias(alias) 31 | 32 | if (versionRange !== undefined) { 33 | return versionRange 34 | } 35 | 36 | const versionRangeA = await getLtsAlias(alias, allNodeOpts) 37 | 38 | if (versionRangeA !== undefined) { 39 | return versionRangeA 40 | } 41 | 42 | return getRecursiveNvmAlias(alias, allNodeOpts) 43 | } 44 | 45 | // nvm custom aliases can be recursive 46 | const getRecursiveNvmAlias = async (alias, allNodeOpts) => { 47 | const aliasResult = await getNvmCustomAlias(alias) 48 | 49 | if (aliasResult === undefined || aliasResult === '') { 50 | return 51 | } 52 | 53 | return getVersionRange(aliasResult, allNodeOpts) 54 | } 55 | -------------------------------------------------------------------------------- /src/main.test-d.ts: -------------------------------------------------------------------------------- 1 | import nodeVersionAlias, { 2 | type Options, 3 | type SemverVersion, 4 | } from 'node-version-alias' 5 | import { expectAssignable, expectNotAssignable, expectType } from 'tsd' 6 | 7 | expectType(await nodeVersionAlias('alias')) 8 | // @ts-expect-error 9 | await nodeVersionAlias() 10 | // @ts-expect-error 11 | await nodeVersionAlias(true) 12 | 13 | await nodeVersionAlias('alias', {}) 14 | expectAssignable({}) 15 | // @ts-expect-error 16 | await nodeVersionAlias('alias', true) 17 | 18 | await nodeVersionAlias('alias', { mirror: 'http://example.com' }) 19 | expectAssignable({ mirror: 'http://example.com' }) 20 | // @ts-expect-error 21 | await nodeVersionAlias('alias', { mirror: true }) 22 | 23 | await nodeVersionAlias('alias', { signal: AbortSignal.abort() }) 24 | expectAssignable({ signal: AbortSignal.abort() }) 25 | // @ts-expect-error 26 | await nodeVersionAlias('alias', { signal: 'signal' }) 27 | 28 | await nodeVersionAlias('alias', { fetch: true }) 29 | await nodeVersionAlias('alias', { fetch: undefined }) 30 | expectAssignable({ fetch: true }) 31 | // @ts-expect-error 32 | await nodeVersionAlias('alias', { fetch: 'true' }) 33 | 34 | expectAssignable('1.2.3') 35 | expectAssignable('0.0.1') 36 | expectAssignable('10.10.10') 37 | expectAssignable('1.2.3-beta') 38 | expectNotAssignable('1.2.a') 39 | expectNotAssignable('1.2') 40 | expectNotAssignable('1') 41 | -------------------------------------------------------------------------------- /src/main.test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | import nodeVersionAlias from 'node-version-alias' 3 | 4 | import { 5 | FULL_VERSION, 6 | MAJOR_FULL_VERSION, 7 | MAJOR_VERSION, 8 | UNKNOWN_VERSION, 9 | } from './helpers/versions.test.js' 10 | 11 | test('Keep full versions', async (t) => { 12 | const version = await nodeVersionAlias(FULL_VERSION) 13 | t.is(version, FULL_VERSION) 14 | }) 15 | 16 | test('Resolve version ranges', async (t) => { 17 | const version = await nodeVersionAlias(MAJOR_VERSION) 18 | t.is(version, MAJOR_FULL_VERSION) 19 | }) 20 | 21 | test('Validates unknown version', async (t) => { 22 | await t.throwsAsync(nodeVersionAlias(UNKNOWN_VERSION)) 23 | }) 24 | 25 | test('Passes options to all-node-versions', async (t) => { 26 | await t.throwsAsync( 27 | nodeVersionAlias(FULL_VERSION, { mirror: INVALID_MIRROR, fetch: true }), 28 | ) 29 | }) 30 | 31 | const INVALID_MIRROR = 'not_valid_url' 32 | -------------------------------------------------------------------------------- /src/nvm.js: -------------------------------------------------------------------------------- 1 | import { execFile } from 'node:child_process' 2 | import { join } from 'node:path' 3 | import { env } from 'node:process' 4 | import { promisify } from 'node:util' 5 | 6 | import { pathExists } from 'path-exists' 7 | 8 | const pExecFile = promisify(execFile) 9 | 10 | // Retrieve nvm custom alias. 11 | export const getNvmCustomAlias = (alias) => runNvmCommand(`nvm_alias ${alias}`) 12 | 13 | // Retrieve Node.js version outside nvm 14 | export const getNvmSystemVersion = () => 15 | runNvmCommand('nvm deactivate >/dev/null && node --version') 16 | 17 | // nvm requires sourcing `nvm.sh` first 18 | const runNvmCommand = async (command) => { 19 | if (!env.NVM_DIR) { 20 | return 21 | } 22 | 23 | const nvmPath = join(env.NVM_DIR, 'nvm.sh') 24 | 25 | if (!(await pathExists(nvmPath))) { 26 | return 27 | } 28 | 29 | try { 30 | const { stdout } = await pExecFile('bash', [ 31 | '-c', 32 | `source "${nvmPath}" && ${command}`, 33 | ]) 34 | return stdout.trim() 35 | // Among possible errors: 36 | // - Setup issue: Bash or nvm not installed, Bash setup error, etc. 37 | // - Custom alias: alias does not exist 38 | // - `system`: no system `node` outside `nvm` 39 | } catch {} 40 | } 41 | -------------------------------------------------------------------------------- /src/nvm.test.js: -------------------------------------------------------------------------------- 1 | import { execFile } from 'node:child_process' 2 | import { createWriteStream } from 'node:fs' 3 | import { readFile, unlink, writeFile } from 'node:fs/promises' 4 | import { dirname, join } from 'node:path' 5 | import { env, version as processVersion } from 'node:process' 6 | import { pipeline } from 'node:stream/promises' 7 | import { fileURLToPath } from 'node:url' 8 | import { promisify } from 'node:util' 9 | 10 | import test from 'ava' 11 | import { got } from 'got' 12 | import nodeVersionAlias from 'node-version-alias' 13 | 14 | // eslint-disable-next-line import/max-dependencies 15 | import { FULL_VERSION, UNKNOWN_ALIAS } from './helpers/versions.test.js' 16 | 17 | const pExecFile = promisify(execFile) 18 | 19 | const NVM_URL = 'https://raw.githubusercontent.com/nvm-sh/nvm/master/nvm.sh' 20 | const NVM_DIR = dirname(fileURLToPath(import.meta.url)) 21 | const NVM_DIST = join(NVM_DIR, 'nvm.sh') 22 | 23 | // eslint-disable-next-line fp/no-mutation 24 | env.NVM_DIR = NVM_DIR 25 | 26 | // We test `nvm` by downloading it locally. It is a Bash script and is not on 27 | // `npm` so we need to download it. 28 | const downloadNvm = async () => { 29 | const response = await got.stream(NVM_URL) 30 | const stream = createWriteStream(NVM_DIST) 31 | await pipeline(response, stream) 32 | 33 | await commentLine() 34 | } 35 | 36 | // `nvm.sh` last line executes `nvm use`. We need to comment it for tests. 37 | const commentLine = async () => { 38 | const content = await readFile(NVM_DIST, 'utf8') 39 | const contentA = content.replace(COMMENTED_LINE, '# BODY') 40 | await writeFile(NVM_DIST, contentA) 41 | } 42 | 43 | const COMMENTED_LINE = 'nvm_process_parameters "$@"' 44 | 45 | const cleanupNvm = async () => { 46 | await unlink(NVM_DIST) 47 | } 48 | 49 | test.before(downloadNvm) 50 | test.after(cleanupNvm) 51 | 52 | // Run `nvm` command in tests 53 | const runNvmCommand = async (command) => { 54 | const { stdout } = await pExecFile('bash', [ 55 | '-c', 56 | `source "${NVM_DIST}" && ${command}`, 57 | ]) 58 | return stdout.trim() 59 | } 60 | 61 | test('Can use "system" alias', async (t) => { 62 | const version = await nodeVersionAlias('system') 63 | t.is(`v${version}`, processVersion) 64 | }) 65 | 66 | test('Can use nvm custom aliases', async (t) => { 67 | await runNvmCommand(`nvm alias a ${FULL_VERSION}`) 68 | 69 | try { 70 | const version = await nodeVersionAlias('a') 71 | t.is(version, FULL_VERSION) 72 | } finally { 73 | await runNvmCommand('nvm unalias a') 74 | } 75 | }) 76 | 77 | test('Can use nvm custom aliases recursively', async (t) => { 78 | await runNvmCommand(`nvm alias b ${FULL_VERSION} && nvm alias c b`) 79 | 80 | try { 81 | const version = await nodeVersionAlias('c') 82 | t.is(version, FULL_VERSION) 83 | } finally { 84 | await runNvmCommand('nvm unalias c && nvm unalias b') 85 | } 86 | }) 87 | 88 | test('Validates unknown nvm custom alias', async (t) => { 89 | await t.throwsAsync(nodeVersionAlias(UNKNOWN_ALIAS)) 90 | }) 91 | 92 | // Modifies environment variable so must be serial 93 | test.serial('Require nvm to look for custom aliases', async (t) => { 94 | await runNvmCommand(`nvm alias a ${FULL_VERSION}`) 95 | // eslint-disable-next-line fp/no-delete 96 | delete env.NVM_DIR 97 | 98 | try { 99 | await t.throwsAsync(nodeVersionAlias('a')) 100 | } finally { 101 | // eslint-disable-next-line require-atomic-updates, fp/no-mutation 102 | env.NVM_DIR = NVM_DIR 103 | await runNvmCommand('nvm unalias a') 104 | } 105 | }) 106 | 107 | test.serial('Handles non-existing NVM_DIR', async (t) => { 108 | await runNvmCommand(`nvm alias a ${FULL_VERSION}`) 109 | // eslint-disable-next-line fp/no-mutation 110 | env.NVM_DIR = '/doesNotExist' 111 | 112 | try { 113 | await t.throwsAsync(nodeVersionAlias('a')) 114 | } finally { 115 | // eslint-disable-next-line fp/no-mutation 116 | env.NVM_DIR = NVM_DIR 117 | await runNvmCommand('nvm unalias a') 118 | } 119 | }) 120 | -------------------------------------------------------------------------------- /src/options.js: -------------------------------------------------------------------------------- 1 | import isPlainObj from 'is-plain-obj' 2 | 3 | // Normalize options and assign default values 4 | export const getOpts = (opts = {}) => { 5 | if (!isPlainObj(opts)) { 6 | throw new TypeError(`Options must be a plain object: ${opts}`) 7 | } 8 | 9 | const { fetch: fetchOpt, mirror, signal } = opts 10 | const normalizeOpts = { fetch: fetchOpt, mirror, signal } 11 | const allNodeOpts = { fetch: fetchOpt, mirror, signal } 12 | return { normalizeOpts, allNodeOpts } 13 | } 14 | -------------------------------------------------------------------------------- /src/options.test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava' 2 | import nodeVersionAlias from 'node-version-alias' 3 | import { each } from 'test-each' 4 | 5 | import { FULL_VERSION } from './helpers/versions.test.js' 6 | 7 | each([true, { fetch: 0 }], ({ title }, opts) => { 8 | test(`Validates options | ${title}`, async (t) => { 9 | await t.throwsAsync(nodeVersionAlias(FULL_VERSION, opts)) 10 | }) 11 | }) 12 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@ehmicky/dev-tasks/tsconfig.json" 3 | } 4 | --------------------------------------------------------------------------------