title: AWS Lambda Layers - Typescript - Problem with specifying imports in index handler since, in the layer, then need to be in form /opt/nodejs/
body: <p>I'm using Typescript to write AWS Lambda code (both handlers and shared code) and then I transpile the code, add node_modules to the dir where transpiled code exist and publish the Lambda. This works correctly.</p>
  <p>Now, if I want to add shared code as part of AWS Lambda layer, this is where it gets tricky with Typescript.
  Consider following code structure that I have:</p>
  <pre><code>.
  ├── index.ts
  ├── lib
  │   └── userData.ts
  ├── node_modules
  ├── package-lock.json
  ├── package.json
  └── tsconfig.json
  </code></pre>
  <p>I would like to have a layer which contains:</p>
  <pre><code>node_modules
  userData.ts
  </code></pre>
  <p>I'm aware of the structure that needs to exist when publishing the layer and also how to reference it in the index.ts (handler method) code.</p>
  <p>Before layers, my index.ts code looks like this:</p>
  <pre><code>import { UserData } from &quot;./lib/userData&quot;;
  
  export const handler = async (event: any = {}): Promise&lt;any&gt; =&gt; {
     # some code here
  }
  </code></pre>
  <p>Because I want to import UserData from the layer, import would need to look like this:</p>
  <pre><code>import { UserData } from &quot;./opt/nodejs/userData&quot;;
  </code></pre>
  <p>However, if I specify this, and try to transpile code (before creating a aws lambda package), I will get an obvious error:</p>
  <pre><code>index.ts:1:26 - error TS2307: Cannot find module '/opt/nodejs/userData' or its corresponding type declarations.
  
  1 import { UserData } from &quot;/opt/nodejs/userData&quot;;
                             ~~~~~~~~~~~~~~~~~~~~~~
  </code></pre>
  <p>There are some ways to avoid this:</p>
  <ol>
  <li>Adding <code>// @ts-ignore</code> before the actual import will silent this error</li>
  <li>Dirty/hacky way would be to edit this import on transpiled code which is not what I want to do.</li>
  </ol>
  <p>Now, this seems like a chicken and egg problem and I'm new to the AWS Lambdas, so any advice on how to handle this situation without these hacky ways to avoid it?</p>