How I fixed @aws-crypto build error
I've been getting the following error when building my Next.js app:
Failed to compile.
./node_modules/.pnpm/@aws-crypto+sha256-js@5.2.0/node_modules/@aws-crypto/sha256-js/build/module/index.js + 12 modules Cannot get final name for export 'fromUtf8' of ./node_modules/.pnpm/@smithy+util-utf8@2.0.2/node_modules/@smithy/util-utf8/dist-es/index.js
I narrowed the source down to the following piece of code:
import { createServerRunner } from "@aws-amplify/adapter-nextjs";
import { AWS_AMPLIFY_CONFIG } from "./utils";
import { cookies } from "next/headers";
import { getCurrentUser } from "aws-amplify/auth/server";
export const { runWithAmplifyServerContext } = createServerRunner({
config: AWS_AMPLIFY_CONFIG,
});
export const getAuthUser = async () =>
runWithAmplifyServerContext({
nextServerContext: { cookies },
operation: (contextSpec) => getCurrentUser(contextSpec),
});
I found these two tickets describing relatively similar issues: https://github.com/FredKSchott/snowpack/issues/3819 https://github.com/aws/aws-sdk-js-crypto-helpers/issues/274
I decided to try switching to require instead of import and that fixed the issue. Here is my code after the changes, error-free.
import { AWS_AMPLIFY_CONFIG } from "./utils";
import { cookies } from "next/headers";
import { getCurrentUser } from "aws-amplify/auth/server";
const createServerRunner =
require("@aws-amplify/adapter-nextjs").createServerRunner;
export const { runWithAmplifyServerContext } = createServerRunner({
config: AWS_AMPLIFY_CONFIG,
});
export const getAuthUser = async () =>
runWithAmplifyServerContext({
nextServerContext: { cookies },
// @ts-ignore
operation: (contextSpec) => getCurrentUser(contextSpec),
});