How to run a Node.js app on Cloudflare so that all node modules work fully?

I installed a “Wrangler” and created a “Worker” in a project (the project itself is on Nest.js and Express.js), when I try to deploy a worker on Cloudflare, I get an error - “Could not resolve “worker_threads””.

I already have the following in my project settings:

  1. node_compat=true
  2. –platform=node
  3. –external:node:*

// My wrangler.toml file

name = "my-worker"
compatibility_date = "2023-07-05"
account_id = "0000000111111111111111111"
node_compat = true

crons = [ "* * * * *" ]

command="npm run build"

// My package.json file

   "name": "my-worker",
   "version": "0.0.0",
   "main": "build/worker.js",
   "node": "18.11.18",
   "devDependencies": {
     "@cloudflare/workers-types": "^4.20230518.0",
     "rollup-plugin-node-polyfills": "^0.2.1",
     "wrangler": "3.1.1"
   "private": true
   scripts: {
     "start": "wranglerdev",
     "build": "npx esbuild ./src/index.ts --bundle --platform=node --external:node:* --outfile=build/worker.js",
     "deploy": "wrangler deploy"
   "dependencies": {
     "@rollup/plugin-inject": "^5.0.3",
     "@types/node": "^18.11.18",
     "ts-node": "^10.0.0"

Looking at the package.json there seems like there are no dependencies that would get bundled, or would require bundling in the worker.

What Node.js-specific pieces of code are you trying to implement?

1 Like

I can link the Node.js compatibility docs for Workers

It is important to know that workers run on a different runtime built by Cloudflare (not Node.js). I don’t expect compatibility for worker_threads to be arriving aytime soon.

If you really need to deploy a Node.js app I would search more for a classic serverless option. If you can split your app into small components and don’t need to rely heavily on Node.js, Cloudflare Workers are an amazing option and there are routing libraries for them as well (ie: to replace express).

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.