0

I am writing an nodejs Typescript application where i want to use an enum that is defined somewhere. The interesting section in my code is : category: Category.DIGITAL_GOODS, and import { Category } from '@paypal/checkout-server-sdk/lib/orders/lib';

My Editor is Visual Studio Code and if I press CTRL and click on the category: part in my code, I get redirected to the node_modules\@types\paypal__checkout-server-sdk\lib\orders\lib.d.ts file to line 482 there the definition of the enum stands:

export enum Category {
    DIGITAL_GOODS = 'DIGITAL_GOODS',
    PHYSICAL_GOODS = 'PHYSICAL_GOODS',
    DONATION = 'DONATION',
}

In the following you se my code where I try to import the Category enum but this does not work as Martin Braun write in this post: Be sure to have this within a *.ts. *.d.ts files will not work. A pitfall I ran into.

QUESTION:but how can i now import this Category enum to my code??

import paypal from '@paypal/checkout-server-sdk';
import { Category } from '@paypal/checkout-server-sdk/lib/orders/lib';

const convertNumberToString = (num: number): string => {
  return num.toFixed(2).toString();
};

const createOrderSDK = async (
  ticketPriceCentBrutto: number,
  eventID: string
) => {
  console.log('in paypal.createOrderSDK');

  const ticketPriceString = convertNumberToString(ticketPriceCentBrutto);
  const quantity = '1';
  const request = new paypal.orders.OrdersCreateRequest();
  request.prefer('return=representation'); //what does this do?
  request.requestBody({
    intent: 'CAPTURE',
    purchase_units: [
      {
        amount: {
          currency_code: 'EUR',
          value: ticketPriceString,
        },
        items: [
          {
            name: 'Ticket',
            category: Category.DIGITAL_GOODS,
            unit_amount: {
              currency_code: 'EUR',
              value: ticketPriceString,
            },
            quantity: quantity,
          },
        ],
      },
    ],
  });

  return;
};

My code compiles with no errors. Only at execution I get this error:

node:internal/process/esm_loader:97
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\joshua\Documents\ticketing\node_modules\@paypal\checkout-server-sdk\lib\orders\lib' imported from C:\Users\joshua\Documents\ticketing\dist\paypal\paypal.js
Did you mean to import @paypal/checkout-server-sdk/lib/orders/lib.js?
    at new NodeError (node:internal/errors:393:5)
    at finalizeResolution (node:internal/modules/esm/resolve:305:11)
    at moduleResolve (node:internal/modules/esm/resolve:866:10)
    at defaultResolve (node:internal/modules/esm/resolve:1074:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:425:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Node.js v18.9.0
3
  • Something that you need to understand. Anything in a *.d.ts will not do anything helpful at runtime (running in a production environment). In this case it appears to me that the library types are being provided (authored) by somebody other than paypal (which often isn't an issue, but sometime may indicate that somebody is not in sync with the library as it changes (interfaces are changed by paypal and the maintainer of the types hasn't kept up). The constant values you reference are not found in the paypal code, so it won't ever work.Your best bet if needed is to copy the enums in your code Commented Oct 9, 2022 at 22:06
  • Thanks for the answer. I added the 'enum Category' with all its definition to my source file and it works. In case the librarys are in sync and everything is fine, where can i than search for the file which i need to import?? Commented Oct 9, 2022 at 22:28
  • So 90% of the time you'd import from the package itself, much like you did on line 1, from the 'package root'. Sometimes I've seen something like you have on line 2. This will depend on how the author wrote the library. In the case of this library - you'll find that it has been deprecated by Palpay, so I don't think it will every be fixed in this case. github.com/paypal/Checkout-NodeJS-SDK Commented Oct 10, 2022 at 0:04

0

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.