Skip to content

Latest commit

 

History

History
117 lines (86 loc) · 3.19 KB

HOSTING.md

File metadata and controls

117 lines (86 loc) · 3.19 KB

Self-Hosting

esm.sh provides a fast, global content delivery network publicly which powered by Cloudflare. You may also want to host esm.sh by yourself.

To serve esm.sh, You will need Go 1.18+ to run and compile the server. The server will install Node.js runtime automatically if it's not found on your host machine.

Recommended Host Machine (Single Server)

  • Linux system with git installed
  • 4x CPU cores or more
  • 8GB RAM or more
  • 100GB disk space or more

Clone code

git clone https://github.com/esm-dev/esm.sh
cd esm.sh

Configration

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.)

Run the Sever Locally

go run main.go --config=config.json --dev

Then you can import React from http://localhost:8080/react

Deploy to Single Machine with the Quick Deploy Script

Please ensure the supervisor has been installed on your host machine.

./scripts/deploy.sh --init

Deploy with Docker

esm.sh provides an official 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

Then 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.
  • SERVER_AUTH_SECRET: The server auth secret, default is no auth.

You can also create your own Dockerfile with ghcr.io/esm-dev/esm.sh:

FROM ghcr.io/esm-dev/esm.sh
ADD ./config.json /etc/esmd/config.json
CMD ["esmd", "--config", "/etc/esmd/config.json"]

Deploy with Cloudflare Workers

We use Cloudflare Workers as the CDN layer to handle and cache esm.sh requests at edge(earth). We open sourced the code, you can use it to build your own esm CDN without deploying the server easily.

More details check esm-worker.

Deploy with Deno

We also provide a server for Deno which is powered by the esm-worker.

deno run -A https://esm.sh/v135/server --port=8080