Back

NodeJS function throws error `Error [ERR_REQUIRE_ESM]: require() of ES Module...`

  • 1
  • Self Hosted
  • Functions
Rovolt
9 Sep, 2023, 15:33

@Meldiron @loks0n Some of my nodejs functions throw the following error on execution:

TypeScript
Error [ERR_REQUIRE_ESM]: require() of ES Module /usr/code-start/src/index.js from /usr/local/src/server.js not supported.
Instead change the require of index.js in /usr/local/src/server.js to a dynamic import() which is available in all CommonJS modules.
    at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
    at /usr/local/src/server.js:55:28
TL;DR
The user is experiencing an error when running a NodeJS function. The error is `Error [ERR_REQUIRE_ESM]: require() of ES Module...`. The user has tried updating the code and dependencies and deploying multiple times, but the error persists. A possible solution is to change the `require` statement in the `index.js` file to a dynamic `import()`. Another solution is to change the file name and entry point in the `appwrite.json` file to `src/main.js` instead of `src/index.js`. Note: The `appwrite.json` and `main.js` files provided are examples that work
Rovolt
9 Sep, 2023, 15:33

Example that works: appwrite.json

TypeScript
{
            "$id": "cleanup_kas_sessions",
            "name": "cleanup_kas_sessions",
            "runtime": "node-18.0",
            "execute": [],
            "events": [],
            "schedule": "",
            "timeout": 15,
            "enabled": true,
            "logging": true,
            "entrypoint": "src/main.js",
            "commands": "npm install",
            "ignore": [
                "node_modules",
                ".npm"
            ],
            "path": "functions/cleanup_kas_sessions"
        }

main.js (function file)

TypeScript
import { Client, Databases, Query } from 'node-appwrite';

export default async ({ req, res, log, error }) => {
  log('run function cleanup_kas_sessions');
  // do some stuff
  ...

Example that doesn't work: appwrite.json

TypeScript
{
            "$id": "authenticate_kas",
            "name": "authenticate_kas",
            "runtime": "node-18.0",
            "path": "functions/authenticate_kas",
            "entrypoint": "src/index.js",
            "commands": "npm install",
            "ignore": [
                "node_modules",
                ".npm"
            ],
            "execute": [],
            "events": [],
            "schedule": "",
            "timeout": 15,
            "enabled": true,
            "logging": true
        },

index.js (function file)

TypeScript
import { Client, Databases } from "node-appwrite";
import { v4 as uuidv4 } from "uuid";
import * as superagent from "superagent"; // even if I comment this out it doesn't work

export default async ({ req, res, log, error }) => {
  log("run function authenticate_kas"); // this is never being called
  // do some stuff

In both functions package.json I have the entry "type": "module",.

Tried to put appwrite container down and up. Deployed several times.

Drake
9 Sep, 2023, 15:37

What if you change the file name and the entrypoint in the appwrite.json to src/main.js?

Rovolt
9 Sep, 2023, 15:44

@Steven I just tried that. Didn't help.

Rovolt
9 Sep, 2023, 15:46

I also see this in the settings menu of the function where the error occurs. I updated the code regarding this list https://appwrite.io/docs/functions-develop#upgrade I don't know what else could be "not up to date".

Rovolt
9 Sep, 2023, 15:52

lol, I created a new function nodetest and copied the complete codefile and the dependencies to the new function, deployed and it works πŸ˜„

Rovolt
9 Sep, 2023, 15:52

So whats going on? πŸ™‚

Drake
9 Sep, 2023, 15:53

Ohhhh ya. Your old function must stay in the old format...

Rovolt
9 Sep, 2023, 15:53

what do you mean by format?

Drake
9 Sep, 2023, 15:54

function main(req, res)

Drake
9 Sep, 2023, 15:54

Can you create an issue for clarifying how to upgrade functions?

Rovolt
9 Sep, 2023, 15:55

Yea of course. Here or in github?

Drake
9 Sep, 2023, 15:56

GitHub please

Rovolt
9 Sep, 2023, 15:56

ok

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more