I have successfuly set up a netlify function, but I’m getting an error when executing the function, firstly it worked locally perfeclty and I was able to deploy the funtion however when calling the function I’ve came accross some errors:
Jul 24, 03:15:34 PM: INIT_START Runtime Version: nodejs:18.v9 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:7d5f06b69c951da8a48b926ce280a9daf2e8bb1a74fc4a2672580c787d608206Jul 24, 03:15:34 PM: 2023-07-24T14:15:34.501Z undefined ERROR (node:8) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)Jul 24, 03:15:34 PM: 2023-07-24T14:15:34.502Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (file:///var/runtime/index.mjs:994:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1029:21)"," at async start (file:///var/runtime/index.mjs:1192:23)"," at async file:///var/runtime/index.mjs:1198:1"]}Jul 24, 03:15:34 PM: 2023-07-24T14:15:34.520Z undefined ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"SyntaxError: Cannot use import statement outside a module","reason":{"errorType":"SyntaxError","errorMessage":"Cannot use import statement outside a module","stack":["/var/task/netlify/edge-functions/index.js:2"," import {createRequire as ___nfyCreateRequire} from \"module\";"," ^^^^^^","","SyntaxError: Cannot use import statement outside a module"," at internalCompileFunction (node:internal/vm:73:18)"," at wrapSafe (node:internal/modules/cjs/loader:1178:20)"," at Module._compile (node:internal/modules/cjs/loader:1220:27)"," at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)"," at Module.load (node:internal/modules/cjs/loader:1119:32)"," at Module._load (node:internal/modules/cjs/loader:960:12)"," at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)"," at ModuleJob.run (node:internal/modules/esm/module_job:194:25)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: SyntaxError: Cannot use import statement outside a module"," at process.<anonymous> (file:///var/runtime/index.mjs:1186:17)"," at process.emit (node:events:513:28)"," at emit (node:internal/process/promises:149:20)"," at processPromiseRejections (node:internal/process/promises:283:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}Jul 24, 03:15:35 PM: 2023-07-24T14:15:35.380Z undefined ERROR (node:8) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)Jul 24, 03:15:35 PM: 2023-07-24T14:15:35.381Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Cannot use import statement outside a module","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Cannot use import statement outside a module"," at _loadUserApp (file:///var/runtime/index.mjs:994:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1029:21)"," at async start (file:///var/runtime/index.mjs:1192:23)"," at async file:///var/runtime/index.mjs:1198:1"]}Jul 24, 03:15:35 PM: 2023-07-24T14:15:35.398Z undefined ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"SyntaxError: Cannot use import statement outside a module","reason":{"errorType":"SyntaxError","errorMessage":"Cannot use import statement outside a module","stack":["/var/task/netlify/edge-functions/index.js:2"," import {createRequire as ___nfyCreateRequire} from \"module\";"," ^^^^^^","","SyntaxError: Cannot use import statement outside a module"," at internalCompileFunction (node:internal/vm:73:18)"," at wrapSafe (node:internal/modules/cjs/loader:1178:20)"," at Module._compile (node:internal/modules/cjs/loader:1220:27)"," at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)"," at Module.load (node:internal/modules/cjs/loader:1119:32)"," at Module._load (node:internal/modules/cjs/loader:960:12)"," at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)"," at ModuleJob.run (node:internal/modules/esm/module_job:194:25)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: SyntaxError: Cannot use import statement outside a module"," at process.<anonymous> (file:///var/runtime/index.mjs:1186:17)"," at process.emit (node:events:513:28)"," at emit (node:internal/process/promises:149:20)"," at processPromiseRejections (node:internal/process/promises:283:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}Jul 24, 03:15:35 PM: Unknown application error occurred
my function implementation code is:
// Import Axios at the top of your file
import axios from "axios";
export default async (req, res) => {
try {
if (req.method !== "POST") {
return new Response(JSON.stringify({ error: "Method not allowed" }), {
status: 405,
headers: { "Content-Type": "application/json" },
});
}
// Parse the request body as JSON
const body = await req.json();
const { messageContent } = body;
if (!messageContent) {
return new Response(
JSON.stringify({ error: "Invalid message content" }),
{
status: 400,
headers: { "Content-Type": "application/json" },
}
);
}
const apiKey = process.env.OPEN_AI_KEY;
const model = "gpt-3.5-turbo";
// Use Axios instead of fetch
const response = await axios.post(
"https://api.openai.com/v1/chat/completions",
{
model,
messages: [
{
role: "user",
content: messageContent,
},
],
},
{
headers: {
Authorization: `Bearer ${apiKey}`,
"User-Agent": "Mozilla/5.0 (compatible; MyCoolApp/1.0)",
"Content-Type": "application/json",
},
}
);
// Access the response data using response.data
const responseData = response.data;
if (
response.status === 200 &&
responseData.choices &&
responseData.choices.length > 0
) {
const firstChoice = responseData.choices[0];
if (firstChoice.message && firstChoice.message.content) {
const content = firstChoice.message.content;
return new Response(JSON.stringify({ content }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
}
return new Response(
JSON.stringify({ error: "No response content available" }),
{
status: 500,
headers: { "Content-Type": "application/json" },
}
);
} catch (error) {
console.error(error);
return new Response(
JSON.stringify({ error: `An error occurred, ${error.message}` }),
{
status: 500,
headers: { "Content-Type": "application/json" },
}
);
}
};
export const config = {
path: "/api/openai-api",
};
These are some additional screenshots: