serverless-bundle: imports not working in typescript project

/file_a.ts

export async function test() {
  console.log('test');
}

/file_b.ts

import { test } from './file_a.ts';
export async function handler(event: LambdaEvent): Promise<LambdaResponse> {
  await test();
}

Executing the handler of file_b (using sls offline or deploying to aws) leads to something like:

TypeError: file_a_1.test is not a function

Both eslint and typescript are not showing any errors. The code is compiling fine. I assume it has something to do with webpack or babel.

That is the issue I am facing right now in a nutshell. I will try to create a small repo to reproduce this error.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 32 (14 by maintainers)

Commits related to this issue

Most upvoted comments

Hey mate @oakgary , try using sourcemaps: false seems to work for me

Just pushed a patch to handle non standard JSON tsconfigs.

https://github.com/AnomalyInnovations/serverless-bundle/releases/tag/v4.0.1

Pushed a new release that shows a warning if commonjs is detected. Also added a note to the README.

https://github.com/AnomalyInnovations/serverless-bundle/releases/tag/v4.0.0

https://github.com/AnomalyInnovations/serverless-bundle#module-and-targets

Yeah I might give this a try tonight. Btw if some of you guys are wondering why it’s failing, the default tsconfig module is set to commonjs when you specify target: es3 (default) or es5. So maybe change that to es6 so it picks module: es6

image

Yeah the issue is that with commonjs set, the exports conflict with the export set by the babel-plugin-source-map-support plugin. You shouldn’t have to set it to commonjs in your tsconfig.json with serverless-bundle because we transpile it through Babel and ensure that the exports are set correctly.

If some of the other folks in this thread can confirm, then I’ll document this issue and possibly add a warning if commonjs is set.

@jayair actually a combination of caching: false and removal of "module": "commonjs" from tsconfig.json worked. Also, it works if I set "module": "es6" with caching: false

same issue, sourcemaps: false fixes it

Yes, in my case it’s placed outside the root of the service.

image

I need this kind of configuration because I’m running on a monorepo setup with a single tsconfig for all services

@oakgary Thanks for putting together the repo. I’ll have a look.