powertools-lambda-typescript: Bug: unable to build with `tsc` when not using `@middy/core`
Bug description
EDIT: see this comment for updated info after triage.
The import statement in Tracer.ts for TracerInterface is importing all the modules from the root. https://github.com/awslabs/aws-lambda-powertools-typescript/blob/main/packages/tracer/src/Tracer.ts#L3.
I am trying to use tracer module without the need to install middy.
Expected Behavior
Should be able to use Tracer without the need to import middy when its not used.
Current Behavior
This behavior is forcing to import middy middleware
Possible Solution
import { TracerInterface } from ‘./TracerInterface’;
Steps to Reproduce
- Install tracer npm module
npm i @aws-lambda-powertools/tracer - Import tracer
import { Tracer } from "@aws-lambda-powertools/tracer/lib/Tracer"; - Add tracer annotation and build fails with
error TS2307: Cannot find module '@middy/core' or its corresponding type declarations.
Environment
- Powertools version used: 1.1.1
- Packaging format (Layers, npm): npm
- AWS Lambda function runtime: Node 16
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 18 (12 by maintainers)
Commits related to this issue
- Bug (Tracer): Tracer imports all the components via TracerInterface #1068 — committed to rreddypally/aws-lambda-powertools-typescript by rreddypally 2 years ago
Hi @ValeryShvyndzikau, that’s great to hear you’ll be adopting the utilities. I just want to acknowledge that I read your message, I will be talking about this issue with @dreamorosi this week and we’ll get back to you with a more concrete answer by the end of the week.
To build on Sara’s previous comment, currently the packages are exported like so:
File
packages/tracer/src/index.tswhich once built and packaged allows consumers to import like:
or, if not using the Middy middleware:
As reported by the discussion in this thread, when bundling with
tscthe compiler throws an error because it expects@middy/coreas dependency. This doesn’t happen when usingesbuild.In order to fix this, it seems that we need to change the main entry point like so:
This would allow everyone to still import the main
Tracerclass like before:but, most notably, it’ll require those who use the middleware to change their imports to:
Now, this would solve the issue in question, but it would also constitute a breaking change, which will require us to do a major release to
v2.0.0. We need to discuss how this fits with our roadmap.Thanks again @rreddypally for reporting this bug and providing a repo to reproduce the issue.
Upon a bit of investigation playing around with the repo
I found out why the compiler is complaining when folks are not importing the middleware file in their codebasesI came to a similar conclusion reached in a PR that was opened before creating this issue.This happens because in the Tracer’s declaration file located in
./node_modules/@aws-lambda-powertools/tracer/lib/Tracer.d.tswe are importing theTracerInterfacevia a wildcard import, instead of importing theTracerInterfacefile specifically. As a consequence, all the content of./node_modules/@aws-lambda-powertools/tracer/lib/index.d.tsis loaded and checked by the compiler:Notice the 4th line, which is causing an error.
Steps to reproduce:
head -2 ./node_modules/@aws-lambda-powertools/tracer/lib/Tracer.d.tsand notice theTracerInterfaceimportnpm run buildand notice the error thrown./node_modules/@aws-lambda-powertools/tracer/lib/Tracer.d.tsfrom this:import { TracerInterface } from '.';to this:import { TracerInterface } from './TracerInterface';npm run buildand notice the lack of errors;The fix for this issue is make sure that the middy middleware file is not imported elsewhere like other folders and files, by removing it from example from the index file imports. Applicable to all other packages too.