npm-to-cdn
Version:
A CLI tool to get CDN links for any npm package.
155 lines (116 loc) • 4.11 kB
Markdown
# <img src="logo.png" alt="npm-to-cdn logo" width="80" align="left" /> npm-to-cdn
[](https://triallord.github.io/npm-to-cdn/)
[](https://github.com/TrialLord/npm-to-cdn)
[](https://github.com/TrialLord/npm-to-cdn)
A professional open-source Node.js CLI tool to instantly get CDN links for any npm package from major providers.
## Features
- **Auto-discovers UMD files** from CDN file listings (unpkg, jsDelivr)
- Supports **unpkg, jsDelivr, esm.sh, skypack**
- **Minified/dev toggle**: `--min` (default) or `--dev`
- **Batch mode**: Query multiple packages at once
- **Clipboard**: Copy first CDN link with `--clipboard`
- **JSON output**: `--json` for machine-readable output
- **UMD-only**: `--umd` for just unpkg/jsDelivr UMD links
- **Show all versions**: `--versions` flag
- **Verbose/silent**: `--verbose` for debug, `--silent` for CI
- **Graceful error handling**
## Installation
```sh
npm install -g npm-to-cdn
```
## Usage
```sh
n2cdn react
n2cdn lodash@4.17.21
n2cdn axios --json
n2cdn vue --clipboard
n2cdn react@17 lodash@4.17.21 --json --silent
n2cdn react --versions
n2cdn react@17 --umd --verbose
n2cdn vue@2 --dev --umd
n2cdn react --cdnjs
n2cdn react --statically user/repo/tag/dist/file.js
n2cdn react --bowercdn
n2cdn react --template "https://mycdn.com/{name}@{version}/{file}"
```
### Flags
- `--json` Output as JSON
- `--clipboard` Copy first CDN link to clipboard
- `--umd` Show only unpkg/jsDelivr UMD links
- `--min` Prefer minified builds (default)
- `--dev` Prefer development (non-minified) builds
- `--versions` List all available versions for the package
- `--verbose` Show all attempted URLs and debug info
- `--silent` Suppress banner and only output links
- `--help` Show help
> Only valid CDN links are shown. UMD guessing is version-aware and auto-discovers from CDN file listings.
## Supported CDN Providers
- **unpkg**
- **jsDelivr**
- **esm.sh**
- **skypack**
- **cdnjs**
- **Statically** (GitHub, GitLab, Bitbucket)
- **BowerCDN**
- **Custom templates**
## Usage Examples
```sh
n2cdn react --cdnjs
n2cdn react --statically user/repo/tag/dist/file.js
n2cdn react --bowercdn
n2cdn react --template "https://mycdn.com/{name}@{version}/{file}"
```
## CLI Improvements
- All provider flags are now grouped and clearly documented
- Input validation for statically flag
- Consistent flag naming and help output
## Example Output
```
$ n2cdn react@17 lodash@4.17.21 --json --silent
[
{
"pkg": "react",
"version": "17",
"cdns": {
"unpkg": "https://unpkg.com/react@17/umd/react.production.min.js",
"jsDelivr": "https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js",
"esmSh": "https://esm.sh/react@17",
"skypack": "https://cdn.skypack.dev/react@17"
}
},
{
"pkg": "lodash",
"version": "4.17.21",
"cdns": {
"esmSh": "https://esm.sh/lodash@4.17.21",
"skypack": "https://cdn.skypack.dev/lodash@4.17.21"
}
}
]
```
## Global Installation
This CLI is globally installable. After running:
```sh
npm install -g npm-to-cdn
```
You can use the `n2cdn` command anywhere.
## Links
- **Website:** [https://triallord.github.io/npm-to-cdn/](https://triallord.github.io/npm-to-cdn/)
- **GitHub:** [https://github.com/TrialLord/npm-to-cdn](https://github.com/TrialLord/npm-to-cdn)
## License
MIT
## Planned Features
- Support for more CDN providers
- Custom CDN URL templates via CLI flag
## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md) for how to get started.
## Code of Conduct
This project follows the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md).