question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: WooCommerceRestApi is not a constructor

See original GitHub issue

I was using NPM @woocommerce/woocommerce-rest-api successfully to manage API requests to Woocommerce/ WP website.

Was using babel and CJS version: ` const WooCommerceRestApi = require(“@woocommerce/woocommerce-rest-api”).default;

const api = new WooCommerceRestApi({ url: “http://example.com”, consumerKey: “ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”, consumerSecret: “cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”, version: “wc/v3” });`

But since Node 14 is offering a simple way to use ESM I have added the following configuration to the package.json, so I can use the import statement: “type”: “module”

So I should have been able to use this format:

`import WooCommerceRestApi from “@woocommerce/woocommerce-rest-api”;

const api = new WooCommerceRestApi({ url: “http://example.com”, consumerKey: “ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”, consumerSecret: “cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”, version: “wc/v3” });`

But now I get this error:

`file:///xxxxxx/test.js:5 const api = new WooCommerceRestApi({ ^

TypeError: WooCommerceRestApi is not a constructor at file:///xxxxxxxx/test.js:5:13 at ModuleJob.run (internal/modules/esm/module_job.js:138:23) at async Loader.import (internal/modules/esm/loader.js:178:24)`

Why would that happen?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:8

github_iconTop GitHub Comments

4reactions
lukewarlowcommented, Oct 20, 2020

The reason it doesn’t work is because of a missing section in package.json telling node which file to import, so it chooses the CJS code. As a temporary fix change the import to explicitly choose the index.mjs file like below.

import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api/index.mjs"

2reactions
notmike101commented, Dec 10, 2021

For anyone who comes here in the future looking for an solution to this error when using the ESM version, here’s a solution that worked for me:

import { default as WCAPI} from '@woocommerce/woocommerce-rest-api';

const { default: WooCommerceRestApi } = WCAPI;

From there, you can just use WooCommerceRestApi as described in the ESM example.

const woocommerce = new WooCommerceRestApi({
  url: 'https://localhost',
  consumerKey: '',
  consumerSecret: '',
  version: 'wc/v3'
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: WooCommerceRestApi is not a constructor
This worked for me: import pkg from '@woocommerce/woocommerce-rest-api'; const WooCommerceRestApi = pkg.default ...
Read more >
TypeError: WooCommerceRestApi is not a constructor
I was using NPM @woocommerce/woocommerce-rest-api successfully to manage API requests to Woocommerce/ ... TypeError: WooCommerceRestApi is not a constructor.
Read more >
TypeError: "x" is not a constructor - JavaScript - MDN Web Docs
The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor,...
Read more >
Retrieve data from WooCommerce REST API - Ionic Forum
I try to retrieve the data from the WooCommerce REST API with this code: ... promise): TypeError: process.version.split is not a function ....
Read more >
June 8, 2020, 11:35 pm - WordPress.org Forums » All Topics
file:///xxxxxx/test.js:5 const api = new WooCommerceRestApi({ ^ TypeError: WooCommerceRestApi is not a constructor at ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found