Skip to main content

How to Deploy Docusaurus Site to Cloudflare Pages - Detailed Guide

· 8 min read

In this blog post we'll setup a Docusaurus site locally, host the source code on Github private repository and finally publish the site using Cloudflare Pages.

Docusaurus is a static-site generator. Cloudflare Pages is a JAMstack platform that we'll use to host the Docusaurus site.

Let's get started.

Install Docusaurus Locally

Follow the Official Installation Guide to install Docusaurus.

Run locally to verify:

Upload the Local Docusaurus Project to Github

Create a new repository on Github.

Copy the Github repository url from Github.

Stop the Docusaurus local server (if still running) and run the following commands in the terminal (or command prompt on windows).

git init
git add .
git commit -m "initial commit"
git branch -M main
git remote add origin https://github.com/YOUR_USERNAME/docusaurus-on-cloudflare-pages.git
git push -u origin main

Reload the Github page and you should see the local project source code is synced to Github repository.

Deploy the Docusaurus Site to Cloudflare Pages

Go to Cloudflare Pages website and sign in to your Cloudflare account (or sign up for a new account).

Once signed in, click on Pages on the left sidebar.

Click on Create a project.

Connect your Github account and select the repository and click on Save (on Github).

You'll be redirected back to Cloudflare Pages. Select the Github repository and click on Begin Setup.

Under Build settings, change the Framework preset to Docusaurus. Click on Save and Deploy.

Build Failed

If you have followed all the previous steps, you'll end up with a Build failed error screen.

The error log:

12:11:09.800    Initializing build environment. This may take up to a few minutes to complete
12:11:11.685 Success: Finished initializing build environment
12:11:11.685 Cloning repository...
12:11:13.380 Success: Finished cloning repository files
12:11:13.719 Installing dependencies
12:11:13.731 Python version set to 2.7
12:11:17.651 v12.18.0 is already installed.
12:11:19.045 Now using node v12.18.0 (npm v6.14.4)
12:11:19.387 Started restoring cached build plugins
12:11:19.404 Finished restoring cached build plugins
12:11:20.110 Attempting ruby version 2.7.1, read from environment
12:11:25.960 Using ruby version 2.7.1
12:11:26.326 Using PHP version 5.6
12:11:26.514 5.2 is already installed.
12:11:26.540 Using Swift version 5.2
12:11:26.542 Started restoring cached node modules
12:11:26.564 Finished restoring cached node modules
12:11:26.802 Installing NPM modules using NPM version 6.14.4
12:11:27.736 npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. I'll try to do my best with it!
12:11:44.663
12:11:44.664 > core-js@3.22.4 postinstall /opt/buildhome/repo/node_modules/core-js
12:11:44.664 > node -e "try{require('./postinstall')}catch(e){}"
12:11:44.664
12:11:44.766 Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!
12:11:44.767
12:11:44.767 The project needs your help! Please consider supporting of core-js:
12:11:44.767 > https://opencollective.com/core-js 
12:11:44.767 > https://patreon.com/zloirock 
12:11:44.767 > bitcoin: bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz 
12:11:44.767
12:11:44.767 Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)
12:11:44.768
12:11:44.779
12:11:44.779 > core-js-pure@3.22.4 postinstall /opt/buildhome/repo/node_modules/core-js-pure
12:11:44.779 > node -e "try{require('./postinstall')}catch(e){}"
12:11:44.779
12:11:45.760 npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules/fsevents):
12:11:45.761 npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
12:11:45.761
12:11:45.765 added 1146 packages from 732 contributors and audited 1150 packages in 18.071s
12:11:46.302
12:11:46.302 191 packages are looking for funding
12:11:46.303 run `npm fund` for details
12:11:46.303
12:11:46.303 found 11 vulnerabilities (5 moderate, 6 high)
12:11:46.303 run `npm audit fix` to fix them, or `npm audit` for details
12:11:46.332 NPM modules installed
12:11:46.709 Installing Hugo 0.54.0
12:11:47.576 Hugo Static Site Generator v0.54.0-B1A82C61A/extended linux/amd64 BuildDate: 2019-02-01T10:04:38Z
12:11:47.580 Started restoring cached go cache
12:11:47.603 Finished restoring cached go cache
12:11:47.762 go version go1.14.4 linux/amd64
12:11:47.783 go version go1.14.4 linux/amd64
12:11:47.787 Installing missing commands
12:11:47.787 Verify run directory
12:11:47.788 Executing user command: npm run build
12:11:48.137
12:11:48.138 > docusaurus-on-cloudflare-pages@0.0.0 build /opt/buildhome/repo
12:11:48.138 > docusaurus build
12:11:48.138
12:11:48.215 (node:1355) ExperimentalWarning: The ESM module loader is experimental.
12:11:48.222 file:///opt/buildhome/repo/node_modules/@docusaurus/core/bin/docusaurus.mjs:28
12:11:48.222 await beforeCli();
12:11:48.222 ^^^^^
12:11:48.222
12:11:48.222 SyntaxError: Unexpected reserved word
12:11:48.222 at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18)
12:11:48.222 at async link (internal/modules/esm/module_job.js:37:21)
12:11:48.227 npm ERR! code ELIFECYCLE
12:11:48.228 npm ERR! errno 1
12:11:48.228 npm ERR! docusaurus-on-cloudflare-pages@0.0.0 build: `docusaurus build`
12:11:48.228 npm ERR! Exit status 1
12:11:48.231 npm ERR!
12:11:48.231 npm ERR! Failed at the docusaurus-on-cloudflare-pages@0.0.0 build script.
12:11:48.231 npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
12:11:48.237
12:11:48.237 npm ERR! A complete log of this run can be found in:
12:11:48.237 npm ERR! /opt/buildhome/.npm/_logs/2022-05-03T06_41_48_232Z-debug.log
12:11:48.248 Failed: build command exited with code: 1

Error Log Analysis

To fix the issue, we first need to know what the error is.

Going through the error log, we can see that the error occurred at file:///opt/buildhome/repo/node_modules/@docusaurus/core/bin/docusaurus.mjs on line 28, and the error message is SyntaxError: Unexpected reserved word at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18).

The error is giving a hint that the issue might be a Docusaurus internal issue.

12:11:48.215    (node:1355) ExperimentalWarning: The ESM module loader is experimental.
12:11:48.222 file:///opt/buildhome/repo/node_modules/@docusaurus/core/bin/docusaurus.mjs:28
12:11:48.222 await beforeCli();
12:11:48.222 ^^^^^
12:11:48.222
12:11:48.222 SyntaxError: Unexpected reserved word
12:11:48.222 at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18)
12:11:48.222 at async link (internal/modules/esm/module_job.js:37:21)

But it was working fine when we tested locally. Second hint, something is wrong in Cloudflare Pages but it's very unlikely.

Let's do a quick google search on SyntaxError: Unexpected reserved word at Loader.moduleStrategy (internal/modules/esm/translators.js:81:18), and you'll end up on StackOverflow to find out that this error occurs when Node version is lower than 14.8.0.

But, is Cloudflare Pages using an older version of NodeJS ? Let's find out.

Going through the Cloudflare Pages docs, we could find the list of languages supported and the default versions of their build tools.

info

The default version for Node.js on Cloudflare Pages is 12.18.0.

This is also visible on the error logs:

12:11:19.045    Now using node v12.18.0 (npm v6.14.4)

But, how are we supposed to know which version of NodeJS is required for Docusaurus ?

It's mentioned on the Docusaurus installation guide. See Requirements section.

Resolving the Issue

Now that we know that NodeJS >= 14 is required, let's see how to configure Cloudflare Pages to use a recent version of NodeJS.

On the Cloudflare Pages Build Configuration page, you'll find the supported NodeJS versions as Any version up to 17.x.

I'll use the same version that's installed on my local machine. Run the following command on the terminal (or command prompt) to find out:

node -v

How to Use NodeJS v14 or v16 or v17 on Cloudflare Pages ?

To use a different but supported version of NodeJS on Cloudflare Pages, you need to create an Environment Variable scoped to the project.

From the Cloudflare dashboard, click on Pages and select the pages project.

Go to Settings tab and click on Environment variables. Click on Add variables.

Enter NODE_VERSION as the Variable name and 16.14.0 as the Value. Click on Save.

Question: How do I know that an Environment Variable needs to be created in this case ?

It's mentioned on the official documentation

If you need to use a specific version of a language, (for example, Node.js or Ruby) you can specify it by providing an associated environment variable in your build configuration, or setting the relevant file in your source code.

Hence, always read the documentation.

Question: Why is the Environment Variable Value written as 16.14.0 and not as v16.14.0 ?

It's because the Default version on the documentation is written as 12.18.0 and not as v12.18.0. Hence, using the same format makes sense.

Retry Deployment

Switch to the Deployments tab and click on Retry deployment.

Finally, the deployment is successful and the Docusaurus site is deployed to Cloudflare Pages.

What's Next ?

Your Docusaurus site is live now. Go ahead and update your site. When done, just commit your changes to Github and that's it. Cloudflare Pages will rebuild your site whenever you push new commits to Github.

That's all for now!

In this blog post, we've deployed a Docusaurus site to Cloudflare Pages. The idea for this blog post is not just to provide a solution for a problem but to provide an understanding of how you should approach to solve a problem.