Skip to content

Latest commit

 

History

History
28 lines (20 loc) · 1.22 KB

README.md

File metadata and controls

28 lines (20 loc) · 1.22 KB

Serverless + ESM + TypeScript

This is a working example of a Serverless Framework project using ESM and TypeScript. The TypeScript types for Serverless are provided by @serverless/typescript.

Issue

Serverless Framework doesn't support ESM and TypeScript for serverless.mts. If you run sls deploy in a project that has "type": "module" in its package.json, you will likely see an error like this:

$ sls deploy

Error:
Cannot load "serverless.ts": Initialization error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/zirkelc/serverless.ts
require() of ES modules is not supported.

This issue is described in more detail in this pull request and this issue.

Workaround

Create a serverless.cjs file and use jiti to compile the serverless.ts on the fly.

module.exports = require('jiti')(null, { interopDefault: true })(`${__dirname}/serverless.ts`);

Then run sls deploy with the --config flag to specify the serverless.cjs file.

sls deploy --config serverless.cjs