-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError: WooCommerceRestApi is not a constructor #66
Comments
Does this work? import { WooCommerceRestApi } from "@woocommerce/woocommerce-rest-api"; |
nop, but thanks anyway. |
workaround:
Not sure why it doesn't work but calling it like this does |
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.
|
Wondering if there is a fix for this yet.. None of the workarounds seem to work for me. |
https://www.npmjs.com/package/@reformosoftware/woocommerce-rest-api this package includes the fix from my PR for the issue. |
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 const woocommerce = new WooCommerceRestApi({
url: 'https://localhost',
consumerKey: '',
consumerSecret: '',
version: 'wc/v3'
}); |
Didn't try all the solutions suggested here but here's another that worked for me:
|
It's been 3 years. This issue is still not resolved. |
It's pretty clear this repository is effectively abandoned. |
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?
The text was updated successfully, but these errors were encountered: