esm.sh provides a global fast CDN publicly which is powered by Cloudflare. You can also host esm.sh service by yourself.
git clone https://github.com/esm-dev/esm.sh
cd esm.sh
To configure the server, create a config.json
file then pass it to the server bootstrap command. For example:
// config.json
{
"port": 8080,
"workDir": "/var/www/esmd",
"storage": "local:/var/www/esmd/storage",
"origin": "https://esm.sh",
"npmRegistry": "https://registry.npmjs.org/",
"npmToken": "xxxxxx"
}
You can find all the server options in config.exmaple.jsonc. (Note: the
config.example.jsonc
is not a valid JSON file, it's a JSONC file.)
You will need Go 1.18+ to compile the server.
go run main.go --config=config.json --dev
Then you can import React
from http://localhost:8080/react.
Ensure the supervisor has been installed on your host machine.
./scripts/deploy.sh --init
Recommended host machine requirements:
- Linux system with
git
andsupervisor
installed - 4x CPU cores or more
- 8GB RAM or more
- 100GB disk space or more
esm.sh provides a Docker image for deployment. You can pull the container image from https://ghcr.io/esm-dev/esm.sh.
docker pull ghcr.io/esm-dev/esm.sh # latest version
docker pull ghcr.io/esm-dev/esm.sh:v135 # specific version
Run the container:
docker run -p 8080:8080 \
-e NPM_REGISTRY=https://registry.npmjs.org/ \
-e NPM_TOKEN=xxxxxx \
ghcr.io/esm-dev/esm.sh:latest
Available environment variables:
CDN_ORIGIN
: The origin of CDN, default is using the origin of the request.CDN_BASE_PATH
: The base path of CDN, default is "/".NPM_REGISTRY
: The NPM registry, default is "https://registry.npmjs.org/".NPM_TOKEN
: The NPM token for private packages.NPM_REGISTRY_SCOPE
: The NPM registry scope, default is no scope.NPM_USER
: The NPM user for private packages.NPM_PASSWORD
: The NPM password for private packages.AUTH_SECRET
: The server auth secret, default is no authrization check.DISABLE_COMPRESSION
: Disable compression, default is false.
You can also create your own Dockerfile based on ghcr.io/esm-dev/esm.sh
:
FROM ghcr.io/esm-dev/esm.sh:v135
ADD ./config.json /etc/esmd/config.json
CMD ["esmd", "--config", "/etc/esmd/config.json"]
We use Cloudflare Workers as the front layer to handle and cache esm.sh requests at edge(earth). And we open sourced the code, you can use it to build your own esm.sh CDN that's running globally.
For more details check esm-worker.