Using Module Format for Workers

Hello, I wanted to start using DurableObjects and a prerequisite to that is to use ESM. There isn’t one central place with docs on how to migrate, but here are the steps I’ve taken:

# wrangler.toml
type="javascript"
[build]
command = "npm install && npm run build"
upload_exclude = ["*.map"]
upload_format = "modules"

// package.json
"module": "./dist/index.js"

 // index.js
export default {
  async fetch(request, env) {
    ...
  }
}

I verified my build results in the bundled index.js file ends up at dist/index.js.

I still am seeing the error on startup of: “The main key in your package.json file is required; please specify the entry point of your Worker.”

Any other suggestions on things I have to change to make this work? For instance, do I need to use the .mjs extension?

Thanks for the help in advance! :smiley:

So I feel real bad that I somehow missed this–but if I can, I’m sure there are others. Currently, you need to install the beta version of wrangler to use module syntax and DurableObjects. If you are using npm it is as simple as:

npm install -g @cloudflare/[email protected]

Oh, and one final note: It appears using module in package.json isn’t enough–one also needs to make sure the file extension is mjs . :woman_shrugging:

Hope this saves someone else some time! :slight_smile:

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