aws lambda - ES6 module error : module is not defined in ES module scope

0

Based on these resources :

https://aws.amazon.com/about-aws/whats-new/2022/01/aws-lambda-es-modules-top-level-await-node-js-14/ https://aws.amazon.com/blogs/compute/using-node-js-es-modules-and-top-level-await-in-aws-lambda/

it is clear that aws nodejs 14.x now supports ES6 module.

However, when I to run a nodejs app with ES6 module, I get this error

undefined   ERROR   Uncaught Exception  
{
    "errorType": "ReferenceError",
    "errorMessage": "module is not defined in ES module scope\nThis file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.",
    "stack": [
        "ReferenceError: module is not defined in ES module scope",
        "This file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.",
        "    at file:///var/task/index.js:20:1",
        "    at ModuleJob.run (internal/modules/esm/module_job.js:183:25)",
        "    at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:60:5)",
        "    at /var/runtime/deasync.js:23:15",
        "    at _tryAwaitImport (/var/runtime/UserFunction.js:74:12)",
        "    at _tryRequire (/var/runtime/UserFunction.js:162:21)",
        "    at _loadUserApp (/var/runtime/UserFunction.js:197:12)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:242:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:1085:14)"
    ]
}

I have already added "type": "module" in package.json

package.json

{
  "name": "autoprocess",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@aws-sdk/client-sqs": "^3.41.0",
    "aws-sdk": "^2.1030.0",
    "check-if-word": "^1.2.1",
    "express": "^4.17.1",
    "franc": "^6.0.0",
    "is-html": "^3.0.0",
    "nodemon": "^2.0.15"
  }
}

index.json

'use strict';

import StringMessage from  './StringMessage.js';

module.exports.handler = async (event) => {
   var data = JSON.parse(event.body);
    //other code goes here

   let response = {
      statusCode: 200,
      headers: {          
      },
      body: ""
  };
  console.log("response: " + JSON.stringify(response))
  return response;
};

I have also tried replacing "module.exports.handler" with "exports.handler ". this does not work either. error message shows "exports is not defined in ES module scope"

What am I doing wrong?

additional info: I am uploading the function code via zip file

az-gi
gefragt vor 2 Jahren16184 Aufrufe
1 Antwort
2
Akzeptierte Antwort

Hi, try replacing the line:

module.exports.handler = async (event) => {

with

export const handler = async (event) => {

You can import you modules outside of the handler with the command below and then use within the handler:

import {StringMessage} from  './StringMessage.mjs';
AWS
beantwortet vor 2 Jahren
profile picture
EXPERTE
überprüft vor 6 Monaten
  • changing to : export const handler = async (event) => { worked. Thank you

  • Mine still doesn't work

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen