Next.js Discord

Discord Forum

500 FUNCTION_INVOCATION_FAILED

Unanswered
Pyramid ant posted this in #help-forum
Open in Discord
Pyramid antOP
Hello, i am having this error when deploying my project to vercel, which does not happen on local development servers, i have tried everything but cant get it fixed. Thanks for anyone who might know how to fix this:

(node:4) Warning: Failed to load the ES module: /var/task/index.js. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.
(Use node --trace-warnings ... to show where the warning was created)
(node:4) Warning: Failed to load the ES module: /var/task/index.js. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.
/var/task/index.js:1
import "dotenv/config";
^^^^^^

SyntaxError: Cannot use import statement outside a module
at wrapSafe (node:internal/modules/cjs/loader:1861:18)
at Module.<anonymous> (node:internal/modules/cjs/loader:1903:20)
at L.m._compile (/opt/rust/bytecode.js:2:5035)
at Object..js (node:internal/modules/cjs/loader:2060:10)
at Module.load (node:internal/modules/cjs/loader:1651:32)
at Module.<anonymous> (node:internal/modules/cjs/loader:1443:12)
at /opt/rust/nodejs.js:2:14482
at Module.Eo (/opt/rust/nodejs.js:2:14860)
at e.<computed>.ut._load (/opt/rust/nodejs.js:2:14452)
at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
Node.js process exited with exit status: 1. The logs above can help with debugging the issue.

5 Replies

Thanks for providing the whole error. The solution is alr in your error:

(node:4) Warning: Failed to load the ES module: /var/task/index.js. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.

So add “type: module” in your package json. OR rename /var/task/index.js to /var/task/index.mjs
Pyramid antOP
I initialized the backend now again but with the vercel express template, and now it works just fine. I suppose there was something wrong with the package.json / tsconfig etc. Thanks for trying to help me though, this took way too long 💀
@Pyramid ant Hello, i am having this error when deploying my project to vercel, which does not happen on local development servers, i have tried everything but cant get it fixed. Thanks for anyone who might know how to fix this: (node:4) Warning: Failed to load the ES module: /var/task/index.js. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension. (Use `node --trace-warnings ...` to show where the warning was created) (node:4) Warning: Failed to load the ES module: /var/task/index.js. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension. /var/task/index.js:1 import "dotenv/config"; ^^^^^^ SyntaxError: Cannot use import statement outside a module at wrapSafe (node:internal/modules/cjs/loader:1861:18) at Module.<anonymous> (node:internal/modules/cjs/loader:1903:20) at L.m._compile (/opt/rust/bytecode.js:2:5035) at Object..js (node:internal/modules/cjs/loader:2060:10) at Module.load (node:internal/modules/cjs/loader:1651:32) at Module.<anonymous> (node:internal/modules/cjs/loader:1443:12) at /opt/rust/nodejs.js:2:14482 at Module.Eo (/opt/rust/nodejs.js:2:14860) at e.<computed>.ut._load (/opt/rust/nodejs.js:2:14452) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) Node.js process exited with exit status: 1. The logs above can help with debugging the issue.
Cynipid gall wasp
Basically, the error means Vercel is treating my index.js file as CommonJS, but my code is using ES module syntax with import.

So locally it works, but on Vercel it fails because the module type isn't configured correctly.

The simple fix is to add "type": "module" to the package.json, or rename the file from .js to .mjs.

Since the project already uses import and export, I would probably add "type": "module" to package.json.