5

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.

Uzair
  • 197
  • 1
  • 2
  • 9

1 Answers1

10

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.

Bilal Akbar
  • 4,659
  • 1
  • 18
  • 29
  • 2
    It's incredible how difficult is to find this information on official Shopify docs. There is almost no mention of this line: `const { Shopify } = require('@shopify/shopify-api');` – Tajs May 17 '22 at 09:57