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

Cannot Find Module #56

Closed
nathanaelphilip opened this issue Nov 30, 2021 · 2 comments
Closed

Cannot Find Module #56

nathanaelphilip opened this issue Nov 30, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@nathanaelphilip
Copy link

Describe the bug
Installed the latest version ^2.0.0 installed via Node 16.7.0 in a NextJS project and get this error when importing:

Import statement:
import { payments } from "@square/web-sdk";

Error:

Error: Cannot find module '/Volumes/Neue/Developer/Sites/thecuriousfoxco-v2/node_modules/@square/web-sdk/dist/payments' imported from /Volumes/Neue/Developer/Sites/thecuriousfoxco-v2/node_modules/@square/web-sdk/dist/index.js

To Reproduce
Steps to reproduce the behavior:

  1. Install NextJS 12 Project
  2. Install @square/web-sdk
  3. Try to import { payments }
  4. See error

Expected behavior
I would like to be able to use this package to build the Payment Forms and Buttons

Screenshots
CleanShot 2021-11-30 at 15 36 47@2x
CleanShot 2021-11-30 at 15 36 58@2x
CleanShot 2021-11-30 at 15 37 06@2x

Desktop (please complete the following information):

  • OS: MacOS
  • Browser Safari
  • Version 15.1
@nathanaelphilip nathanaelphilip added the bug Something isn't working label Nov 30, 2021
@maxbeatty
Copy link
Contributor

I have a project using Next v11 and used next-transpile-module with this configuration to account for @square/web-sdk being ESM. I'm not sure if Next v12 handles it differently.

// next.config.js
const withTM = require('next-transpile-modules')(['@square/web-sdk']);

module.exports = withTM({
  // more config
})

Our usage is almost identical to what you posted:

import { payments } from '@square/web-sdk';
import type { Payments } from '@square/web-sdk';
import { useEffect, useState } from 'react';

type MaybeError = Error | undefined;
type MaybePayments = Payments | undefined;

interface SqPaymentsState {
  error: MaybeError;
  payments: MaybePayments;
}

export function useSqPayments(): SqPaymentsState {
  const [error, setError] = useState<MaybeError>();
  const [paymentsInstance, setPayments] = useState<MaybePayments>();

  useEffect(() => {
    async function load(): Promise<void> {
      try {
        const p = await payments(applicationId, locationId, overrides);

        if (p === null) {
          throw new Error('Square Payments failed to load');
        }

        setPayments(p as Payments);
      } catch (error_: unknown) {
        setError(error_ as Error);
      }
    }

    load();
  }, [setPayments, setError]);

  return { error, payments: paymentsInstance };
}

Hope this helps!

@nathanaelphilip
Copy link
Author

@maxbeatty that worked! thank you for sharing :). I thought Next12 was supposed to handle that natively, but maybe this is a different issue: https://nextjs.org/blog/next-11-1#es-modules-support

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants