By Prince7hakur
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
My project is built as a modern ES Module.
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!
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.