17

I have created a lambda function to get the list of items stored in dynamodb. Then i executed npm run build command after which i got .js files. I zipped the dist contents along with node modules, webpack.config.js and package.json and uploaded it. When i try to test it, am getting the following error. But there is no error in the code as far as i checked.

{
  "errorType": "Runtime.UserCodeSyntaxError",
  "errorMessage": "SyntaxError: Cannot use import statement outside a module",
  "trace": [
    "Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module",
    "    at _loadUserApp (/var/runtime/UserFunction.js:98:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:956:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)",
    "    at Module.load (internal/modules/cjs/loader.js:812:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:724:14)",
    "    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)",
    "    at internal/main/run_main_module.js:17:11"
  ]
}
1
  • Find all instances of "import" in your code, see if any look dodgy. Commented Dec 4, 2019 at 10:50

4 Answers 4

14

I had the same error with node uuid module. Importing and using it as below fixed my issue:

'use strict';
const uuid = require('uuid');   // <-- how to import

exports.hello = async (event) => {
   // ...

   uniqueId = uuid.v4();  // <-- how to use it

   // ...
};
Sign up to request clarification or add additional context in comments.

Comments

0

I was ok with using require() for the longest time BUT certain packages like netlify cannot be loaded in lambda via require().

But AWS improved this ES vs commonJS import statement support early 2022... See here. Sadly not well described.

The following steps are maybe not perfect but worked for me:

  1. Switch to NodeJs v14 on your AWS function if you didn't already
  2. Pack the packages that you want to use once more and don't just have a nodejs folder but also a node14 folder. The node14 folder is mentioned here and I think essential. In case you forgot how to create lambda packages follow the docs here.
  3. Create a lib.mjs file and load the package via import from /opt/nodejs/node14/node_modules/[my-package]/[maybe-some-subfolder]/index.js.
  4. You can export what you need as object from lib.mjs and load it from index.js

lib.mjs:

// lib.mjs
import { NetlifyAPI } from "/opt/nodejs/node14/node_modules/netlify/src/index.js";
import { Route53Client } from "/opt/nodejs/node14/node_modules/@aws-sdk/client-route-53/dist-cjs/index.js";

export function loadPackages() {
    return { NetlifyAPI, Route53Client };
}

Index.js:

//index.js:
import { loadPackages } from './lib.mjs';

export async function handler(event) {
  const { NetlifyAPI, Route53Client } = loadPackages();
  //TODO: business logic
}

This took me a long time to research and my approach is likely not perfect. I was forced to find a solution to load netflify and it only worked via import statement. I'm happy to get feedback in the comments and I am willing to adjust my tutorial accordingly.

Currently I'd generally still recommend to stick with require() if you can. If not I hope I could help.

PS: webpack is not needed in this approach.

Comments

0

This might be pretty late but i fixed mine by using require instead of import when importing modules

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2

You have a problem with your import. Review your code (imports parts) and see if your IDE detects any errors. For my case, it was :

import Jimp from 'jimp/es'

I changed it to

import Jimp from 'jimp'

And now everything works.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.