Skip to main content

Setting Up Cloudflare on Replit

Setting Up Cloudflare on Replit

  • You can use the Cloudflare template provided by Replit, but before running it, make the following changes
  1. In .config/_setup.sh, change the wrangler@2 to just wrangler on lines 44 and 51 (remove the @2), so you get the latest wrangler version
  2. In replit.nix file, change the node version to 18
  3. In the .replit file add the following
[[ports]]
localPort = 8787
externalPort = 80
exposeLocalhost = true
  • After you do the above, you can run the template (just choose Y for initial setup). It will create all the files in the subdirectory. I am not sure how to get it to initialize in the main directory for now. So after you run it do the following:
  1. Drag the files from the subdirectory the template created to the main directory.
  2. After all files are dragged, delete the subdirectory folder.
  3. In the package.json, after "test", add the following: "replit-run-command": "npm run start"
  4. In the wrangler.toml - change the name of the project to the name you want.
  5. Then just hit run, and you should be fine.
  • Note: Remember to connect your cloudflare account! To use Cloudflare Features, like, AI, features you need to connect your Cloudflare Account to your Replit. To do that, follow the instructions in the Deploy Worker to Cloudflare in the ReadMe file that comes with the template. Remember to: Add a Replit secret with the name WRANGLER_SEND_METRICS=false

  • Note: For some functionality, like AI, you need to make sure the API token you have set up in Cloudflare that you use to connect in the Replit secrets, has access to that resource. Login into Cloudflare -> My Profile (upper right) -> API Tokens (left side) -> Click on ... next to API token, Edit -> Give it access to something, like Workers AI

  • Note: You can use an existing repo for your cloudflare project. Just download the files from git and then upload them to replit to override the current files. run npm install. remember to also create a .dev.vars for your env variables that can run locally in replit. connect the repo to replit in github and then you can push/pull. Make sure you only develop on a dev branch in replit!

  • IMPORTANT: Before working on Replit for an existing repo, create a new branch on Github, e.g. dev, and pull that to Replit and work off that branch. Then follow the directions.

  • IMPORTANT: After you do the above, create a new branch on Github and pull that to Replit. Then follow the directions to create Github Actions. Do NOT develop without Github actions or else you are asking for a nightmare with regards to maintenance.

Note: Though not recommended, I believe that you can run the set up again, but editing the .replit file and changing the run command to: run = ".config/setup.sh"