Skip to content
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

Open
yonish3 opened this issue Jun 8, 2020 · 10 comments · May be fixed by #129
Open

TypeError: WooCommerceRestApi is not a constructor #66

yonish3 opened this issue Jun 8, 2020 · 10 comments · May be fixed by #129

Comments

@yonish3
Copy link

yonish3 commented Jun 8, 2020

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?

@tomjn
Copy link

tomjn commented Jun 11, 2020

Does this work?

import { WooCommerceRestApi } from "@woocommerce/woocommerce-rest-api";

@yonish3
Copy link
Author

yonish3 commented Jun 11, 2020

nop, but thanks anyway.
i gave up, will keep the old version

@jor3l
Copy link

jor3l commented Jul 18, 2020

workaround:

import wcapi from '@woocommerce/woocommerce-rest-api';
const { default: WooCommerceRestApi } = wcapi;

Not sure why it doesn't work but calling it like this does

@lukewarlow
Copy link

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"

@robindelaater
Copy link

Wondering if there is a fix for this yet.. None of the workarounds seem to work for me.

@lukewarlow
Copy link

lukewarlow commented Oct 5, 2021

https://www.npmjs.com/package/@reformosoftware/woocommerce-rest-api this package includes the fix from my PR for the issue.

@notmike101
Copy link

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

@tarpagad
Copy link

Didn't try all the solutions suggested here but here's another that worked for me:

import wc from "@woocommerce/woocommerce-rest-api";
const WooCommerceRestApi = wc.default;

@danaoairuike
Copy link

It's been 3 years. This issue is still not resolved.

@lukewarlow
Copy link

It's pretty clear this repository is effectively abandoned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants