Skip to content

zirkelc/serverless-esm-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

Jiti v1

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

Jiti v2

// serverless.cjs
const { createJiti } = require('jiti');
const jiti = createJiti(null, { interopDefault: true });

module.exports = jiti.import(`${__dirname}/serverless.ts`, { default: true });

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

sls deploy --config serverless.cjs

Alternative

Since Serverless v3 has been deprecated, the community has started a fork to continue maintaining a Serverless Framework CLI v3 alternative. I added support for ESM and TypeScript via this PR and it has since been released as v3.49.0. That means running sls deploy with a serverless.ts or serverless.mts config should work out of the box.

About

Serverless with ESM and TypeScript

Resources

Stars

Watchers

Forks