You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NextJS中@aws-sdk/signature-v4-crt包使用异常问题

CloudFront KeyValueStore 在Next.js应用路由中报错的解决方法

问题场景

  • 普通Node.js环境下,以下操作CloudFront KeyValueStore的代码可正常完成查询与写入:
// index.js
const {
  CloudFrontKeyValueStoreClient,
  DescribeKeyValueStoreCommand, PutKeyCommand, ListKeysCommand
} = require("@aws-sdk/client-cloudfront-keyvaluestore");

require("@aws-sdk/signature-v4-crt");

(async () => {
  try {
    // Initialize the CloudFront KeyValueStore Client
    const client = new CloudFrontKeyValueStoreClient({
      region: 'eu-west-1',
      credentials: {
        accessKeyId: 'XXX',
        secretAccessKey: 'XXX',
      },
    });

    const input = {
      KvsARN: "arn:aws:cloudfront::xxx:key-value-store/xxx-xxx-xxx",
    };

    // Create a new command
    const command = new DescribeKeyValueStoreCommand(input);

    // Send the command to the client
    const response = await client.send(command);

    // Log the response
    console.log(response);


    const input1 = { // PutKeyRequest
      Key: "sports", // required
      Value: "true", // required
      KvsARN: "arn:aws:cloudfront::xxx:key-value-store/xxx-xxx-xxx", // required
      IfMatch: response.ETag // required
    };
    const command1 = new PutKeyCommand(input1);
    const response1 = await client.send(command1);
    console.log('response 1: ', response1);

  } catch (e) {
    // Catch and log any errors
    console.error("Error:", e);
  }
})();
  • 将逻辑迁移至Next.js应用路由的API路由后,代码无法正常运行:
// index.js
const {
  CloudFrontKeyValueStoreClient,
  DescribeKeyValueStoreCommand, PutKeyCommand, ListKeysCommand
} = require("@aws-sdk/client-cloudfront-keyvaluestore");

require("@aws-sdk/signature-v4-crt");

//export const dynamic = 'force-dynamic'; // Opt out of static generation

export async function GET() {
  try {
    // Initialize the CloudFront KeyValueStore Client
    const client = new CloudFrontKeyValueStoreClient({
        region: 'eu-west-1',
        credentials: {
        accessKeyId: 'XXX',
        secretAccessKey: 'XXX',
        },
    });

    // Input for the DescribeKeyValueStoreCommand
    const input = {
        KvsARN: "arn:aws:cloudfront::xxx:key-value-store/xxx-xxx-xxx", // Replace with your own ARN
    };

    // Create a new command
    const command = new DescribeKeyValueStoreCommand(input);

    // Send the command to the client
    const response = await client.send(command);

    // Log the response
    console.log(response);

    const input1 = { // PutKeyRequest
        Key: "sports", // required
        Value: "true", // required
        KvsARN: "arn:aws:cloudfront::xxx:key-value-store/xxx-xxx-xxx", // required
        IfMatch: response.ETag // required
    };
    const command1 = new PutKeyCommand(input1);
    const response1 = await client.send(command1);
    console.log('response 1: ', response1);

    return Response.json(response1 );
  } catch (error) {
    console.error(error);
    return Response.json({ error: 'An error occurred' }, { status: 500 });
  }
}

报错信息:

Please check whether you have installed the
"@aws-sdk/signature-v4-crt" package explicitly. You must also
register the package by calling
[require("@aws-sdk/signature-v4-crt");] or an ESM equivalent such as
[import "@aws-sdk/signature-v4-crt";]. For more information please go
to
https://github.com/aws/aws-sdk-js-v3#functionality-requiring-aws-common-runtime-crt

注:已确认安装@aws-sdk/signature-v4-crt包,且Node.js环境运行正常,仅Next.js环境报错。

解决方法

1. 切换为ESM导入方式

Next.js App Router默认采用ESM模块系统,CommonJS的require可能无法正确触发签名包的注册逻辑,改用import语法:

import {
  CloudFrontKeyValueStoreClient,
  DescribeKeyValueStoreCommand,
  PutKeyCommand,
  ListKeysCommand
} from "@aws-sdk/client-cloudfront-keyvaluestore";
import "@aws-sdk/signature-v4-crt";

2. 强制路由动态运行

取消注释路由文件中的动态配置,避免Next.js对该路由进行静态优化,确保模块在请求时才加载:

export const dynamic = 'force-dynamic'; // 强制动态运行,禁用静态生成

3. 重置依赖缓存

删除现有依赖和锁文件后重新安装,排除依赖缓存导致的加载异常:

rm -rf node_modules package-lock.json
npm install

4. 配置Webpack处理外部依赖(可选)

如果以上方法无效,在next.config.js中添加Webpack配置,将签名包标记为外部依赖,避免Next.js的打包逻辑干扰其加载:

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config) => {
    config.externals.push('@aws-sdk/signature-v4-crt');
    return config;
  },
};

module.exports = nextConfig;

内容的提问来源于stack exchange,提问作者asanas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 18:24:51