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

AWS SDK模块解析错误求助:已装依赖仍无法导入

问题解决步骤

1. 修正AWS SDK导入路径

你当前的导入语句存在错误,@aws-sdk是命名空间,Secrets Manager的客户端和命令实际位于@aws-sdk/client-secrets-manager子包中。修改导入代码:

import { SecretsManagerClient, GetSecretValueCommand } from "@aws-sdk/client-secrets-manager";

2. 确保安装正确依赖

确认你安装的是对应子包,而非错误的@aws-sdk包:

npm install @aws-sdk/client-secrets-manager

3. 修复await使用问题

await必须包裹在async函数内,将获取密钥的逻辑封装到异步函数中:

async function fetchSecret() {
  const secret_name = "MySecret";
  const client = new SecretsManagerClient({ region: "us-west-1" });

  try {
    const response = await client.send(
      new GetSecretValueCommand({
        SecretId: secret_name,
        VersionStage: "AWSCURRENT"
      })
    );
    return response.SecretString;
  } catch (error) {
    console.error("获取密钥失败:", error);
    throw error;
  }
}

// 调用异步函数并处理后续逻辑
fetchSecret().then(secret => {
  const apiKey = secret;
  const apiUrl = 'https://api.openai.com/v1/';
  
  // 后续DOM操作和事件监听逻辑
  const form = document.querySelector('form');
  const input = document.querySelector('input');
  const submitButton = document.querySelector('#submit-button');
  const instructions = document.querySelector('.instructions');

  form.addEventListener('submit', (event) => {
    event.preventDefault();
    const inputText = input.value.trim();
    console.log(inputText)

    if (inputText !== '') {
      submitButton.disabled = true;
      fetch(apiUrl + 'completions', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${apiKey}`,
        },
        body: JSON.stringify({
          model: 'text-davinci-003',
          prompt: `Generate a title based on: "${inputText}" make sure its SEO optimized and catchy.`,
          max_tokens: 100,
          top_p: 1,
          n: 1,
        }),
      })
      .then((response) => response.json())
      .then((data) => {
        const title = data.choices[0].text.trim();
        const result = document.createElement('p');
        result.textContent = `Title: ${title}`;
        form.parentNode.insertBefore(result, form.nextSibling);
        if (title !== '') {
          instructions.textContent = '';
        }
        submitButton.disabled = false;
        console.log(data.choices[0].text);
      })
      .catch((error) => {
        console.error(error);
        submitButton.disabled = false;
      });
    }
  });
}).catch(err => console.error(err));

4. 关键安全提示

禁止在前端代码中直接调用AWS Secrets Manager:前端代码会暴露你的AWS访问凭证,引发严重安全风险。正确做法是:

  • 搭建后端服务(如Node.js、Python)
  • 在后端中调用Secrets Manager获取API密钥
  • 前端通过调用你的后端API获取密钥,避免直接接触AWS服务

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 12:15:27