title: Typescript bundling error - cant find imports
body: <p>I have a node application that compiles typescript files to a dist folder and then serves these files as lambda resolvers via aws cdk. Here is an example of my setup:</p>
  <h2>The code</h2>
  <h4>register.ts</h4>
  <pre><code>import ValidateUserFields from '../utils/forms';
  
  export async function main(event: any, context: any) {
      return {
        statusCode: 200,
      };
  }
  </code></pre>
  <h4>register-lambda-config.ts</h4>
  <pre><code>import { Construct } from 'constructs';
  import * as apigateway from 'aws-cdk-lib/aws-apigateway';
  import * as lambda from 'aws-cdk-lib/aws-lambda';
  import * as s3 from 'aws-cdk-lib/aws-s3';
  
  export class FrontendService extends Construct {
    constructor(scope: Construct, id: string) {
      super(scope, id);
  
      const api = new apigateway.RestApi(this, 'frontend-api', {
        restApiName: 'Frontend Service',
        description: 'This service serves the frontend.',
      });
  
      const functionName = 'register';
  
      const handler = new lambda.Function(this, functionName, {
        functionName,
        runtime: lambda.Runtime.NODEJS_14_X,
        code: lambda.Code.fromAsset('dist/src/lambda'),
        handler: 'register.main',
      });
  
      const registerIntegration = new apigateway.LambdaIntegration(handler, {
        requestTemplates: { 'application/json': '{ &quot;statusCode&quot;: &quot;200&quot; }' },
      });
  
      const registerResource = api.root.addResource('register');
      registerResource.addMethod('POST', registerIntegration);
    }
  }
  </code></pre>
  <h4>tsconfig.json</h4>
  <pre><code>{
    &quot;compilerOptions&quot;: {
      &quot;target&quot;: &quot;ES2018&quot;,
      &quot;module&quot;: &quot;commonjs&quot;,
      &quot;moduleResolution&quot;: &quot;node&quot;,
      &quot;lib&quot;: [&quot;es2018&quot;],
      &quot;declaration&quot;: true,
      &quot;strict&quot;: true,
      &quot;noImplicitAny&quot;: false,
      &quot;strictNullChecks&quot;: true,
      &quot;noImplicitThis&quot;: true,
      &quot;alwaysStrict&quot;: true,
      &quot;esModuleInterop&quot;: true,
      &quot;noUnusedLocals&quot;: false,
      &quot;noUnusedParameters&quot;: false,
      &quot;noImplicitReturns&quot;: true,
      &quot;noFallthroughCasesInSwitch&quot;: false,
      &quot;inlineSourceMap&quot;: true,
      &quot;inlineSources&quot;: true,
      &quot;experimentalDecorators&quot;: true,
      &quot;strictPropertyInitialization&quot;: false,
      &quot;outDir&quot;: &quot;dist&quot;,
      &quot;typeRoots&quot;: [&quot;./node_modules/@types&quot;]
    },
    &quot;exclude&quot;: [&quot;node_modules&quot;, &quot;cdk.out&quot;, &quot;./dist/**/*&quot;]
  }
  </code></pre>
  <h2>The problem</h2>
  <p>When I run npm run dev it compiles my typescript files to the dist folder in the same structure as what I have in my src folder (where all my typescript files live). I however run into the following error if I have any imports in my register.ts file:</p>
  <blockquote>
  <p>{&quot;errorType&quot;:&quot;Runtime.ImportModuleError&quot;,&quot;errorMessage&quot;:&quot;Error: Cannot
  find module '../utils/forms'\nRequire stack:\n-
  /var/task/register.js\n- /var/runtime/UserFunction.js\n-
  /var/runtime/index.js&quot;,&quot;stack&quot;:[&quot;Runtime.ImportModuleError: Error:
  Cannot find module '../utils/forms'&quot;,&quot;Require stack:&quot;,&quot;-
  /var/task/register.js&quot;,&quot;- /var/runtime/UserFunction.js&quot;,&quot;-
  /var/runtime/index.js&quot;,&quot; at _loadUserApp
  (/var/runtime/UserFunction.js:202:13)&quot;,&quot; at Object.module.exports.load
  (/var/runtime/UserFunction.js:242:17)&quot;,&quot; at Object.
  (/var/runtime/index.js:43:30)&quot;,&quot; at Module._compile
  (internal/modules/cjs/loader.js:1085:14)&quot;,&quot; at
  Object.Module._extensions..js
  (internal/modules/cjs/loader.js:1114:10)&quot;,&quot; at Module.load
  (internal/modules/cjs/loader.js:950:32)&quot;,&quot; at Function.Module._load
  (internal/modules/cjs/loader.js:790:12)&quot;,&quot; at
  Function.executeUserEntryPoint [as runMain]
  (internal/modules/run_main.js:75:12)&quot;,&quot; at
  internal/main/run_main_module.js:17:47&quot;]}</p>
  </blockquote>
  <p>This happens for imports from relative local files (like '../utils/forms' as shown in the code above) but also for imports from node_modules. When I look into the compiled register.js file in the dist folder I see that it has made an attempt to parse the import:</p>
  <pre><code>&quot;use strict&quot;;
  var __importDefault = (this &amp;&amp; this.__importDefault) || function (mod) {
      return (mod &amp;&amp; mod.__esModule) ? mod : { &quot;default&quot;: mod };
  };
  Object.defineProperty(exports, &quot;__esModule&quot;, { value: true });
  const forms_1 = __importDefault(require(&quot;../utils/forms&quot;));
  const bucketName = process.env.BUCKET;
  
  exports.main = async function (event, context) { ...
  </code></pre>
  <p>however it shows the error message above. I have tried changing the compiler options in tsconfig to &quot;module&quot;: &quot;esnext&quot; but this introduces other errors...</p>
  <p>Here is a link to the repo: <a href="https://easyupload.io/ulnmo4" rel="nofollow noreferrer">https://easyupload.io/ulnmo4</a></p>
  <p>just do <code>npm i</code> then <code>npm run dev</code>. Then import the following template into Postman to reproduce error: <a href="https://pastebin.com/vr3MhR5Q" rel="nofollow noreferrer">https://pastebin.com/vr3MhR5Q</a></p>
  <p>Any help would be greatly appreciated.</p>