Did you know you can access your package without installing it?
Yes, you can, with the help of popular CDNs like unpkg and jsDelivr!
What is this?
Unpkg and jsDelivr are CDNs that host public npm packages. They allow browser-based apps to quickly access packages globally without needing a package manager or bundler.
How to Access?
-
Unpkg: https://unpkg.com/package-name
-
jsDelivr: https://cdn.jsdelivr.net/npm/package-name
How Does It Work?
-
You Publish to npm:
Your package is uploaded to the npm public registry when you run npm publish.
-
CDNs Fetch from npm:
- They detect new versions in the npm registry.
- Fetch the package tarball and extract it.
- Serve files based on fields like main, unpkg, or jsdelivr in package.json.
-
Custom Fields:
Fields like unpkg and jsdelivr in package.json specify which file the CDN should serve. These fields are ignored by other tools unless explicitly supported.
Example: @monaco-editor/react
{
"name": "@monaco-editor/react",
"version": "4.4.6",
"main": "lib/cjs/index.js",
"unpkg": "lib/umd/monaco-react.min.js",
"jsdelivr": "lib/umd/monaco-react.min.js"
}
unpkg and jsdelivr are custom fields not a standard fields and these can be ignored by other tools unless they explicitly recognize them. It is used to determine which file to serve when the package is requested via the CDN unpkg / jsdelivr.
Use Cases
1. Browser-Based Applications
2. Fast, Global Delivery
-
Use Case: Ensure your package is served quickly and reliably worldwide.
-
Example:
- A website using your library benefits from jsDelivr or Unpkg’s globally distributed edge servers, which reduce latency.
-
Relevance:
- Ideal for high-traffic applications or when performance is critical.
3. Avoiding Build Steps
-
Use Case: Provide a ready-to-use version of your library for users who don't want to deal with transpilation or bundling.
-
Example:
- Your package provides a pre-bundled UMD or IIFE build. Developers can include it directly without setup:
{
"name": "@monaco-editor/react",
"version": "4.4.6",
"main": "lib/cjs/index.js",
"unpkg": "lib/umd/monaco-react.min.js",
"jsdelivr": "lib/umd/monaco-react.min.js"
}
-
Relevance:
- Great for rapid development environments or non-Node.js ecosystems.
4. Embedding Libraries in Static Sites
5. Legacy Environments
-
Use Case: Enable users working in environments without modern build tools or Node.js.
-
Example:
- A developer maintaining a legacy application can use your library via CDN links rather than modifying their outdated setup.
-
Relevance:
- Supports legacy apps or non-modern JavaScript environments.
6. Demos and Sandboxes
7. Version-Specific Loading
-
Use Case: Allow users to load specific versions of your library without worrying about npm install commands.
-
Example:
- A user wants version 2.3.0:
{
"name": "@monaco-editor/react",
"version": "4.4.6",
"main": "lib/cjs/index.js",
"unpkg": "lib/umd/monaco-react.min.js",
"jsdelivr": "lib/umd/monaco-react.min.js"
}
-
Relevance:
- Helps developers test or lock their projects to a specific version without bundling tools.
8. Sharing Packages in Multi-Framework Projects
9. Backup or Alternative to npm
-
Use Case: Provide an alternative way to access your package if npm registry issues arise.
-
Example:
- jsDelivr can serve packages even if npm is temporarily down.
-
Relevance:
- Adds redundancy and reliability.
When to Avoid CDN Hosting
-
Modern Applications:
- If most of your users use Node.js or modern bundlers (Webpack, Rollup, etc.), they likely don’t need a CDN.
-
Package Size:
- Large libraries served via CDN may increase browser load times.
-
Version Conflicts:
- If multiple versions of your library might load simultaneously, it could lead to unexpected behavior.
Summary of Use Cases
Use Case |
Ideal For |
Example Usage |
Browser Inclusion |
Simplicity |
|
Fast Delivery |
High-traffic apps |
Use of jsDelivr or Unpkg for caching |
Avoiding Build Steps |
Prototypes or small projects |
UMD or IIFE pre-bundled files |
Embedding in Static Sites |
Blogs, lightweight sites |
Markdown renderer, chart libraries |
Demos and Sandboxes |
Quick testing |
Platforms like CodePen or JSFiddle |
Sharing Across Frameworks |
Multi-framework apps |
Shared libraries or design systems |
CDN hosting is a great complement to npm distribution, particularly for web-focused libraries. If you have specific requirements, feel free to ask!
The above is the detailed content of Access package without Installing it.. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn