Shopify API Node/Express 无法读取未定义的属性(读取 'Rest')

Shopify API Node/Express Cannot read properties of undefined (reading 'Rest')

刚开始使用 Shopify,并尝试获得订单。根据 Shopify API 文档,这是我的代码:

const Shopify = require('@shopify/shopify-api');

const client = new Shopify.Clients.Rest('my-store.myshopify.com', 
process.env.SHOPIFY_KEY);

module.exports.getShopifyOrderById = async (orderId) => {
return await client.get({ 
    path: `orders/${orderId}`,
  });
}

执行这段代码时出现以下错误:

TypeError: Cannot read properties of undefined (reading 'Rest')

似乎无法弄清楚问题所在。

您需要使用对象析构来获取 Shopify 对象或使用如下所示的默认导出。

const { Shopify } = require('@shopify/shopify-api');

const client = new Shopify.Clients.Rest('my-store.myshopify.com', 
process.env.SHOPIFY_KEY);

const Shopify = require('@shopify/shopify-api').default;

const client = new Shopify.Clients.Rest('my-store.myshopify.com', 
process.env.SHOPIFY_KEY);

const ShopifyLib = require('@shopify/shopify-api');

const client = new ShopifyLib.Shopify.Clients.Rest('my-store.myshopify.com', 
process.env.SHOPIFY_KEY);

这与 ES6 模块在 CommonJS 中的模拟方式以及导入模块的方式有关。你可以 .