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
- Add typescript support — committed to AnomalyInnovations/serverless-bundle by tume 4 years ago
Hey mate @oakgary , try using
sourcemaps: false
seems to work for meJust 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 tocommonjs
when you specifytarget: es3 (default) or es5
. So maybe change that toes6
so it picksmodule: es6
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 tocommonjs
in yourtsconfig.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"
withcaching: false
same issue,
sourcemaps: false
fixes itYes, in my case it’s placed outside the root of the service.
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.