【以下的问题经过翻译处理】 基于以下资源:
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文件来上传代码的。
我哪里做的有问题呢?