easypost-node icon indicating copy to clipboard operation
easypost-node copied to clipboard

[Bug]: Cannot instantiate new EasyPostClient in Nextjs 13

Open oliviergabison opened this issue 11 months ago • 8 comments

Software Version

7.2.0

Language Version

20.10.0

Operating System

Mac OS 13.3.1

What happened?

  1. Imported easypost-node library
  2. Attempt to create new EasyPostClient via const client = new EasyPostClient(process.env.EASYPOST_API_KEY!);
  3. Upon building, error is thrown that .TypeError: a is not a function
  4. After commenting out the code, the app builds fine

What was expected?

To be able to build the app while using EasyPostClient

Sample Code

const client = new EasyPostClient(process.env.EASYPOST_API_KEY!);

Relevant logs

No response

oliviergabison avatar Mar 25 '24 23:03 oliviergabison

Hello @oliviergabison , thanks for reaching out and reporting this! We are currently investigating the issue, and will keep you updated.

nwithan8 avatar Mar 25 '24 23:03 nwithan8

Hello @oliviergabison , just wanted to update you on our findings so far.

We've been able to recreate your issue trying to import the library into a Next.js 13 project, and have discovered the issue lies in the easypost-node > superagent > formidable > hexoid dependency chain. Specifically, formidable does not seem to be importing hexoid properly.

In our research, we came across another library that seems to be facing a similar issue, if any of the conversation there might be able to help you get unblocked: https://github.com/pubnub/javascript/issues/352. In the meantime, we'll continue investigating if there's anything we need/can fix with our library specifically.

Thanks again for reporting this!

nwithan8 avatar Mar 28 '24 05:03 nwithan8

I'm running into this as well

jonknyc avatar Jul 10 '24 18:07 jonknyc

Ok, I think I finally found the issue. It is definitely a common issue with formidable + webpack

  • https://github.com/node-formidable/formidable/issues/960
  • https://github.com/pubnub/javascript/issues/352
  • https://github.com/amzn/selling-partner-api-samples/pull/45

That last pull request actually provides a solution though. In the diff, in the Next.js config, you can see they create a webpack override for it. I did that as well, and it fixed the issue for me.

  1. Install webpack, npm i --save-dev webpack
  2. Change you next.config.js to include this:
import webpack from "webpack";

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config) => {
    config.plugins.push(
      new webpack.NormalModuleReplacementPlugin(
        /^hexoid$/,
        "hexoid/dist/index.js"
      )
    );
    return config;
  },
};

export default nextConfig;

ralexmatthews avatar Jul 23 '24 16:07 ralexmatthews

@oliviergabison @jonknyc, did the override suggestion above help in your cases?

Justintime50 avatar Aug 27 '24 16:08 Justintime50

@oliviergabison @jonknyc, did the override suggestion above help in your cases?

I ended up just calling the API with fetch, instead of using the SDK.

e.g.

const response = await fetch("https://api.easypost.com/v2/trackers", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${process.env.EASYPOST_API_KEY}`,
    },
    body: JSON.stringify({
      tracker: {
        carrier,
        tracking_code: trackingCode,
      },
    }),
  })

jonknyc avatar Aug 27 '24 18:08 jonknyc