I get this error when accessing an API endpoint in a Vercel deployment of a React app with a top level api/
folder containing serverless endpoints. Interestingly, this API endpoint works fine locally using vercel dev
. Even more interestingly, a similar API endpoint in the same API folder which also uses ES6 module imports works fine.
[POST] /api/checkout
20:49:23:62
2021-02-20T04:49:23.818Z undefined ERROR /var/task/api/checkout.js:1
import { firebaseAdmin, stripe } from "./_firebase_stripe";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at /var/task/___vc_launcher.js:26:28
at Object.<anonymous> (/var/task/___vc_launcher.js:74:7)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
RequestId: 0e27de3f-fdd1-401a-a5e2-030327a15a31 Error: Runtime exited with error: exit status 1
Runtime.ExitError
This is the code which fails on the server once deployed but works fine locally.
import { firebaseAdmin, stripe } from "./_firebase_stripe";
import { getConfig } from "./_config";
import {
verifiedUser,
getCustomerCheckoutDetails,
readyToGo,
} from "./_helpers";
export default async function Checkout(req, res) {
const { url } = getConfig();
let sessionId;
This is the content of api/hello.js
which works fine both locally and on deployed version when accessing /api/hello
.
import { firebaseAdmin } from "./_firebase_stripe";
import { getConfig } from "./_config";
export default function (_req, res) {
const version = firebaseAdmin.SDK_VERSION;
return res.send({ ...getConfig(), version });
}