Report this

What is the reason for this report?

ReferenceError: module is not defined in ES module scope

Posted on October 9, 2025

Hello everyone, I’m building a serverless cron job on DigitalOcean Functions (using Node.js 18) and I’m stuck on a persistent ReferenceError: module is not defined in ES module scope.

One of my functions (userDetails) works perfectly, but my cron job function (notifications) consistently fails with this error. The key difference is that the notifications function has a more complex dependency graph.

although the code is working fine at local enviroment just failing at do-functions

The Root Cause As I Understand It

  1. My project is built as a modern ES Module.

  2. The bundler (esbuild) gets confused by this chain (ESM -> CJS -> CJS) and produces a broken bundle that crashes in the strict ESM environment on DigitalOcean.

this is my folder structure src functions notifications.ts

import { notifications } from "../services/notifications.service";

export async function main(args: any) {
  await notifications();
  return {
    statusCode: 200,
    body: JSON.stringify({ message: "Job completed successfully" }),
  };
}`

Project.yml

packages:
  - name: pi-service
    functions:
      - name: cron-job-for-daily-notifications
        runtime: nodejs:18
        main: main
        file: dist/notifications.js
        triggers:
          - name: run-daily
            sourceType: scheduler
            sourceDetails:
              cron: "*/5 * * * *"

My Question is how do i make it work for DO-Functions

package.json

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "tsc",
    "bundle": "esbuild src/functions/*.ts --bundle --platform=node --target=node18 --external:uglify-js --outdir=dist",
    "start:local": "npm run build && cross-env dotenv_config_path=./env/.env.dev node -r dotenv/config dist/local-dev.js",
    "deploy:prod": "npm run build && npx serverless deploy --stage prod",
    "deploy": "npm run bundle && doctl serverless deploy ."
  },

tsconfig.json

"target": "es2022",
    "sourceMap": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./dist",
    "rootDir": "src",
    "strict": true,
    "esModuleInterop": true,
    "baseUrl": ".",


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

This error usually happens when the deployed code mixes CommonJS and ES modules. DigitalOcean Functions runs Node.js 18 in a strict ES module environment, so if your bundle is outputting CommonJS (module: "commonjs"), it can cause the ReferenceError: module is not defined in ES module scope.

You could try updating your tsconfig.json to use "module": "esnext" and adjust your build command to output ESM format (--format=esm). That should resolve the issue.

If it still does not work as expected, probably worth reaching out to support: https://do.co/support

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.