Introduction to esm.sh
esm.sh is an innovative content delivery network (CDN) designed to enhance modern web development by providing easy, fast, and efficient access to JavaScript ES modules. This powerful service simplifies the importing of modules from popular sources like NPM, GitHub, and JSR, eliminating the need for traditional installation or build steps.
How to Use esm.sh
Utilizing esm.sh is straightforward. Developers can import modules via a simple URL format:
import * as mod from "https://esm.sh/PKG[@SEMVER][/PATH]";
With the additional support of import maps, developers can employ bare import specifiers instead of lengthy URLs, which greatly simplifies code management.
<script type="importmap">
{
"imports": {
"react": "https://esm.sh/[email protected]"
}
}
</script>
<script type="module">
import React from "react"; // → https://esm.sh/[email protected]
</script>
Supported Registries
esm.sh supports multiple registries, including:
- NPM: Access the latest or specific versions of modules, including sub-modules.
- JSR: Offers access with
/jsr/
as the URL prefix. - GitHub: Modules can be directly linked using the
/gh/
prefix.
Specifying and Aliasing Dependencies
esm.sh allows specification of dependencies with a query parameter, thus customizing module versions to suit project needs. Moreover, dependencies can be aliased to other comparable modules, offering greater flexibility.
Tree Shaking
One of the advanced features esm.sh supports is tree shaking, which allows developers to import only specific module exports, thereby optimizing the bundle size.
import { __await, __rest } from "https://esm.sh/tslib?exports=__await,__rest"; // smaller bundle
Bundling Strategy
The default setting in esm.sh is to bundle sub-modules to reduce network requests. However, developers have control over this and can disable it entirely or bundle everything including external dependencies into a single file.
Development Mode and ESBuild Options
Developers can specify environments like development mode by using designated queries, allowing for different behaviors in development versus production environments. Also customizable are the build targets such as ES standards, Deno, or Node environments using ESBuild options.
Web Worker Capabilities
esm.sh is capable of loading modules as web workers, a vital feature for developers working with asynchronous threads. This can be achieved through the ?worker
query, which facilitates operations in separate threads without blocking the main thread.
Advanced Features
esm.sh supports several other advanced features:
- Import Maps: Facilitates bare import specifiers by keeping dependencies externally resolvable.
- Raw Source Files: On special requests, source files can be fetched in their raw form.
- WASM Module Importing: WebAssembly modules can be directly imported in JS projects.
- CSS Handling: Allows direct importing of module-related CSS files.
Deno and Node.js Compatibility
esm.sh is highly compatible with Deno, automatically resolving Node's built-in modules, and it includes support for types using the X-TypeScript-Types
header. While Node.js and Bun do not yet support HTTP modules natively, esm.sh recommends using Reejs as a runtime environment supporting both platforms.
Infrastructure and Self-Hosting
Global service of esm.sh is robustly supported by Cloudflare's extensive CDN network, ensuring fast and reliable delivery worldwide. For enterprises or individuals interested in self-hosting, comprehensive guidelines and documentation are provided to facilitate deployment.
esm.sh is not just a tool but a bridge connecting modern web development practices with the future of module management, offering a new horizon of possibilities for developers across the globe.