aws lambda - ES6模块错误:在ES模块作用域中未定义模块。

0

【以下的问题经过翻译处理】 基于以下资源:

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/

很明显,aws nodejs 14.x 现在支持ES6模块。

但是,当我运行带有ES6模块的nodejs应用程序时,我会遇到以下错误:

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)"
    ]
}

我已经在package.json中添加了 "type": "module"

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;
};

我也尝试了将"module.exports.handler"替换为 "exports.handler ",但也生效。错误信息是 "exports is not defined in ES module scope" 。 另外我是用zip文件来上传代码的。

我哪里做的有问题呢?

profile picture
EXPERTE
gefragt vor 5 Monaten45 Aufrufe
1 Antwort
0

【以下的回答经过翻译处理】 请将以下这句代码

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

替换为:

export const handler = async (event) => {

你可以使用以下代码将模块导入handler之外,并在handler中使用:

import {StringMessage} from  './StringMessage.mjs';

profile picture
EXPERTE
beantwortet vor 5 Monaten

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