Question

Node script works with `node file.js` but fails on Vercel with 'require is not defined'

Solved · 7 views · asked by tomvibes · edited
ReferenceError: require is not defined in ES module scope

Bolt generated the utility with require. Locally fine.

What I’ve tried

Renamed to .cjs, then imports elsewhere broke.

Comment

1 answer

Marked as helpful by the asker
jonas_k · edited

Your package.json has "type": "module", so every .js file is ESM and require does not exist there. Pick one world and stay in it. For a modern project: ESM.

// before
const fs = require('fs');
// after
import fs from 'node:fs';

Tell Bolt: "this project is ESM, use import/export only". It remembers within a session.

Comment