Closed anni1236012 closed 1 month ago
What repository structure do you have? Why aren't the dependencies specified in the root package.json?
package.json has the dependencies as shown above in the code and it is in the amplify directory. Here is the directory structure.
Why are they not defined in the root package.json? During build npm is being run from the root, and afaik npm does not automatically resolve nested package.json
Might to be the thing causing the issue, but that stood out to me.
root package.json is only for frontend build but amplify has it's own build process to build the backend and it will look for amplify/package.json
Not sure that is true, but perhaps it works none the less. I don't have any dependencies in mine, using a monorepo setup, and my lambdas are able to pull in any dependencies from my root package.json.
Could it perhaps be that the library requires some lambda layers or be incompatible with esbuild?
You could try deploying a NodeJsFunction construct to rule out amplify as the culprit.
I tried with NodeConstruct but still getting the same error, require.ensure is not a function
.
Below is my backend.ts . is the correct way of deploying NodeConstruct?
In GEN1, same package is working fine. I use require content in javascript and it has the node run 18. I don't know how it works in GEN1 because lambda is running in the same environment.
import { defineBackend, Backend } from "@aws-amplify/backend";
import { auth } from "./auth/resource";
import { data } from "./data/resource";
import path from "path";
import * as iam from "aws-cdk-lib/aws-iam";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import * as lambda from "aws-cdk-lib/aws-lambda";
import { storage } from "./storage/resource";
import { Duration } from "aws-cdk-lib/core";
import { MyStack } from "./functions/CreateFilebasedChatbot/myStack";
import { CreateFileChatbotPermissions } from "./functions/CreateFilebasedChatbot/permissions";
import { CreateFileChatbotHandler } from "./functions/CreateFilebasedChatbot/resource";
import { fileURLToPath } from "url";
// ES module alternative to __dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
*/
const backend = defineBackend({
auth,
data,
storage,
});
// Create a new stack for the Lambda function
const lambdaStack = backend.createStack("LambdaStack");
// Define the Lambda function
const createFileChatbotLambda = new NodejsFunction(
lambdaStack,
"CreateFileChatbotLambda",
{
entry: path.join(
__dirname,
"./functions/CreateFilebasedChatbot/handler.ts"
),
handler: "handler",
runtime: lambda.Runtime.NODEJS_18_X,
bundling: {
externalModules: [
"aws-sdk",
],
},
}
);
// Define the ARN for the function
const createFileChatbotLambdaARN = createFileChatbotLambda.functionArn;
// Create the IAM policy statement
const selfInvokePolicy = new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ["lambda:InvokeFunction"],
resources: [createFileChatbotLambdaARN],
});
// Create an inline policy and attach it to the Lambda role
const inlinePolicy = new iam.Policy(lambdaStack, "SelfInvokePolicy", {
statements: [selfInvokePolicy],
});
if (createFileChatbotLambda.role) {
createFileChatbotLambda.role.attachInlinePolicy(inlinePolicy);
}
// Grant self-invoke permissions
createFileChatbotLambda.addPermission("SelfInvokePermission", {
principal: new iam.ServicePrincipal("lambda.amazonaws.com"),
action: "lambda:InvokeFunction",
sourceArn: createFileChatbotLambdaARN,
});
I haven't looked closely at this yet, but just wanted to clarify:
but amplify has it's own build process to build the backend and it will look for amplify/package.json
This isn't entirely true. The amplify/package.json
file only tells Node to treat that folder as a module, it does not contain any information on dependencies. Dependencies should still be installed in the root package.json of the project.
Hey @anni1236012, following up on this issue. Were you able to resolve this issue?
Closing the issue due to inactivity. Do reach out to us if you are still experiencing this issue
Environment information
Description
Looks like a build error. I've narrow down the issue to a pdf-parse package. Runtime error occurs at this step
javascript const loader = new PDFLoader(pdfBlob);
. Please see the complete code below.langchain documentation for reference https://js.langchain.com/v0.2/docs/integrations/document_loaders/file_loaders/pdf/
Runtime Lambda Error
handler.ts
package.json
tsconfig.json