6

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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.