title: NodeJS 14.x - Native AWS Lambda Import/Export Support
body: <p>I am looking to make use of the native <a href="https://nodejs.org/api/esm.html#esm_import_statements" rel="nofollow noreferrer">import/export</a> that comes with ES6.</p>
  <p>I am using Serverless Containers within AWS Lambda.</p>
  <p>I have my <code>Dockerfile</code> which looks like this:</p>
  <pre><code>FROM public.ecr.aws/lambda/nodejs:14
  
  COPY app ./
  
  RUN npm install
  
  CMD [ &quot;app.handler&quot; ]
  </code></pre>
  <p>I then have an <code>app</code> directory with my application code. The <code>app.js</code> code looks like this:</p>
  <pre><code>import { success } from './utils/log';
  
  exports.handler = async () =&gt; {
    success('lambda invoked');
    const response = 'Hello World';
    return {
      statusCode: 200,
      body: JSON.stringify(response),
      isBase64Encoded: false,
    };
  };
  </code></pre>
  <p>As you can see from this line <code>import { success } from './utils/log';</code> I am making use of native imports.</p>
  <p>In my package.json I specify this:</p>
  <pre><code>  &quot;type&quot;: &quot;module&quot;
  </code></pre>
  <p>As I need to tell my application this is a module and I would like imports natively. If I don't specify this, I get:</p>
  <pre><code>{
      &quot;errorType&quot;: &quot;Runtime.UserCodeSyntaxError&quot;,
      &quot;errorMessage&quot;: &quot;SyntaxError: Cannot use import statement outside a module&quot;,
      &quot;stack&quot;: [
          &quot;Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module&quot;,
          &quot;    at _loadUserApp (/var/runtime/UserFunction.js:98:13)&quot;,
          &quot;    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)&quot;,
          &quot;    at Object.&lt;anonymous&gt; (/var/runtime/index.js:43:30)&quot;,
          &quot;    at Module._compile (internal/modules/cjs/loader.js:1063:30)&quot;,
          &quot;    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)&quot;,
          &quot;    at Module.load (internal/modules/cjs/loader.js:928:32)&quot;,
          &quot;    at Function.Module._load (internal/modules/cjs/loader.js:769:14)&quot;,
          &quot;    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)&quot;,
          &quot;    at internal/main/run_main_module.js:17:47&quot;
      ]
  }
  </code></pre>
  <p>So, I specify it, telling Lambda this is a module. However, for the life of me I can't get it to work, I am seeing this error:</p>
  <pre><code>{
      &quot;errorType&quot;: &quot;Error&quot;,
      &quot;errorMessage&quot;: &quot;Must use import to load ES Module: /var/task/app.js\nrequire() of ES modules is not supported.\nrequire() of /var/task/app.js from /var/runtime/UserFunction.js is an ES module file as it is a .js file whose nearest parent package.json contains \&quot;type\&quot;: \&quot;module\&quot; which defines all .js files in that package scope as ES modules.\nInstead rename app.js to end in .cjs, change the requiring code to use import(), or remove \&quot;type\&quot;: \&quot;module\&quot; from /var/task/package.json.\n&quot;,
      &quot;code&quot;: &quot;ERR_REQUIRE_ESM&quot;,
      &quot;stack&quot;: [
          &quot;Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/app.js&quot;,
          &quot;require() of ES modules is not supported.&quot;,
          &quot;require() of /var/task/app.js from /var/runtime/UserFunction.js is an ES module file as it is a .js file whose nearest parent package.json contains \&quot;type\&quot;: \&quot;module\&quot; which defines all .js files in that package scope as ES modules.&quot;,
          &quot;Instead rename app.js to end in .cjs, change the requiring code to use import(), or remove \&quot;type\&quot;: \&quot;module\&quot; from /var/task/package.json.&quot;,
          &quot;&quot;,
          &quot;    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1080:13)&quot;,
          &quot;    at Module.load (internal/modules/cjs/loader.js:928:32)&quot;,
          &quot;    at Function.Module._load (internal/modules/cjs/loader.js:769:14)&quot;,
          &quot;    at Module.require (internal/modules/cjs/loader.js:952:19)&quot;,
          &quot;    at require (internal/modules/cjs/helpers.js:88:18)&quot;,
          &quot;    at _tryRequire (/var/runtime/UserFunction.js:75:12)&quot;,
          &quot;    at _loadUserApp (/var/runtime/UserFunction.js:95:12)&quot;,
          &quot;    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)&quot;,
          &quot;    at Object.&lt;anonymous&gt; (/var/runtime/index.js:43:30)&quot;,
          &quot;    at Module._compile (internal/modules/cjs/loader.js:1063:30)&quot;
      ]
  }
  </code></pre>
  <p>It looks like <code>/var/runtime/UserFunction.js</code> is calling my app handler as a require and a module. However, I have no control over <code>/var/runtime/UserFunction.js</code> (I don't believe?). In my <code>Dockerfile</code> I have specified <code>Node14</code>. I don't quite know where I have gone wrong?</p>
  <p>What I am looking to do is run the latest and greatest of Node14 (such as imports) without Babel/Transpiler that &quot;bloat&quot; my code. If someone could point me in the right direction of where I have gone wrong, it would be appreciated.</p>