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

本地Express开发Shopify应用遇Digital Ocean Spaces无效URL错误

本地开发Shopify应用时出现TypeError [ERR_INVALID_URL]错误,部署到服务器正常运行的问题

问题背景

本地用Express开发Shopify应用时触发TypeError [ERR_INVALID_URL]: Invalid URL错误,但相同代码部署到服务器环境可正常运行,无法定位本地环境故障原因。

技术栈

  • Digital Ocean Spaces
  • Node.js v17.0.1
  • @aws-sdk/client-s3 v3.171.0

相关代码

import { S3 } from "@aws-sdk/client-s3";
import dotenv from "dotenv";

dotenv.config();

const s3Client = new S3({
    endpoint: process.env.SPACES_ENDPOINT,
    region: process.env.SPACES_REGION,
    credentials: {
        accessKeyId: process.env.SPACES_KEY,
        secretAccessKey: process.env.SPACES_SECRET,
    },
});

export default s3Client;

已在本地.env文件中配置有效环境变量。

错误日志

burhan@burhan:/shopifynode$ shopify app serve
✓ ngrok tunnel running at https://ebb6-2401-4900-1c02-5a90-982c-9e83-1b5e-b9ee.ngrok.io, with account testmail@mail.com
✓ .env saved to project root

⭑ To install and start using your app, open this URL in your browser:
https://ebb6-2401-4900-1c02-5a90-982c-9e83-1b5e-b9ee.ngrok.io/login?shop=my-app-staging.myshopify.com

Running server…

> dev
> cross-env NODE_ENV=development nodemon server/index.js --watch ./server

[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): server/**/*

[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server/index.js`
node:internal/errors:464
   ErrorCaptureStackTrace(err);
   ^

TypeError [ERR_INVALID_URL]: Invalid URL
   at new NodeError (node:internal/errors:371:5)
   at onParseError (node:internal/url:552:9)
   at new URL (node:internal/url:632:5)
   at parseUrl (/shopifynode/node_modules/@aws-sdk/url-parser/dist-cjs/index.js:7:38)
   at resolveEndpointsConfig (/shopifynode/node_modules/@aws-sdk/config-resolver/dist-cjs/endpointsConfig/resolveEndpointsConfig.js:14:87)
   at new S3Client (/shopifynode/node_modules/@aws-sdk/client-s3/dist-cjs/S3Client.js:22:72)
   at new S3 (/shopifynode/node_modules/@aws-sdk/client-s3/dist-cjs/S3.js:98:1)
   at file:///shopifynode/server/helpers/s3-client.js:6:18
   at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
   at async Promise.all (index 0) {
 input: '"https://fra1.digitaloceanspaces.com"',
 code: 'ERR_INVALID_URL'
}

Node.js v17.0.1
[nodemon] app crashed - waiting for file changes before starting...

问题分析与解决

从错误日志的input: '"https://fra1.digitaloceanspaces.com"'可以看出,环境变量SPACES_ENDPOINT的值被包裹了多余的双引号,这是URL解析失败的直接原因。本地.env文件中可能错误地给变量值加了双引号,而服务器环境的变量配置没有这个问题。

解决方法

  1. 修正本地.env文件
    打开本地.env文件,将SPACES_ENDPOINT的双引号移除,示例:

    SPACES_ENDPOINT=https://fra1.digitaloceanspaces.com
    

    重启本地开发服务器,让新配置生效。

  2. 代码层面兼容处理(可选)
    若担心环境变量配置可能存在引号问题,可在代码中添加去除首尾引号的逻辑:

    const s3Client = new S3({
        endpoint: process.env.SPACES_ENDPOINT?.replace(/^"|"$/g, ''),
        region: process.env.SPACES_REGION?.replace(/^"|"$/g, ''),
        credentials: {
            accessKeyId: process.env.SPACES_KEY?.replace(/^"|"$/g, ''),
            secretAccessKey: process.env.SPACES_SECRET?.replace(/^"|"$/g, ''),
        },
    });
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 02:51:00