dev*_*ato 16 amazon-web-services node.js node-modules aws-lambda
我正在尝试使用 JavaScript 模块执行以下代码。我知道 NodeJS 默认是 CommonJS。我的代码在本地工作,但是当我想将其作为 lambda 中的模块运行时,我遇到了以下问题:
错误:
{
"errorType": "ReferenceError",
"errorMessage": "exports 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.",
"trace": [
"ReferenceError: exports 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:2:1",
" at ModuleJob.run (node:internal/modules/esm/module_job:195:25)",
" at async Promise.all (index 0)",
" at async ESMLoader.import (node:internal/modules/esm/loader:337:24)",
" at async _tryAwaitImport (file:///var/runtime/index.mjs:660:16)",
" at async _tryRequire (file:///var/runtime/index.mjs:709:37)",
" at async _loadUserApp (file:///var/runtime/index.mjs:721:16)",
" at async Object.module.exports.load (file:///var/runtime/index.mjs:741:21)",
" at async file:///var/runtime/index.mjs:781:15",
" at async file:///var/runtime/index.mjs:4:1"
]
}
Run Code Online (Sandbox Code Playgroud)
我已经删除了
type: module并使用require了import,但我仍然遇到同样的问题。
Lambda 文件夹结构(节点 V16.X):
+SendPushNotification(root)
+node_modules
-index.js
-package.json
-package.json.lock
Run Code Online (Sandbox Code Playgroud)
索引.JS
import * as OneSignal from '@onesignal/node-onesignal';
exports.handler = async (event) => {
const response = "hey";
console.log("testing")
return response;
};
Run Code Online (Sandbox Code Playgroud)
包.JSON
{
"name": "onesignal-nodejs-client-sample",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module"
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@onesignal/node-onesignal": "^1.0.0-beta4"
}
}
Run Code Online (Sandbox Code Playgroud)
Pis*_*eni 33
由于您有“type”:“module”,因此启用了ES6模块。你应该将index.js更改为
import * as OneSignal from '@onesignal/node-onesignal';
export const handler = async (event) => {
const response = "hey";
console.log("testing")
return response;
};
Run Code Online (Sandbox Code Playgroud)
并且,如果您想要默认导出,请使用:
import * as OneSignal from '@onesignal/node-onesignal';
const handler = async (event) => {
const response = "hey";
console.log("testing")
return response;
};
export default handler
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33734 次 |
| 最近记录: |