简体   繁体   中英

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

Just starting off with Shopify, and trying to get an order. Following the Shopify API documentation, here is my code:

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}`,
  });
}

I get the following error when I execute this code:

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

Can't seem to figure out what the issue is.

You need to use Object destructing to get the Shopify object or use default export like below.

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

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

OR

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

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

OR

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

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

This has to do with how ES6 modules are emulated in CommonJS and how you import the module. You can read about that here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM