wallet-adapter: Incompatibility with NextJS (11.0.0)

When using the latest version of NextJS (with TS), I am unable to use the @solana/wallet-adapter packages.

***/node_modules/@solana/wallet-adapter-react/lib/index.js:1
export * from './ConnectionProvider';
^^^^^^

SyntaxError: Unexpected token 'export'

I’m quite positive it has to do with how the packages are transpiled, and wouldve hoped adding them to the list of transpiled code in my nextjs project (using next-transpile-modules) would work, but unfortunately I was not able to get them working (edit: see update bellow). Any tips? If I get it working I’d be happy to submit a PR for either a readme update or change.

tsconfig:

{
  "compilerOptions": {
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": false,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

nextjs config:

module.exports = {
  webpack5: true
}

Update:

I was finally able to get it working taking quite a detour, I’ve had to change my nextjs config to the following:

/* eslint-disable @typescript-eslint/no-var-requires */
const withTM = require('next-transpile-modules')([
  '@solana/wallet-adapter-base',
  '@solana/wallet-adapter-bitpie',
  '@solana/wallet-adapter-coin98',
  '@solana/wallet-adapter-ledger',
  '@solana/wallet-adapter-mathwallet',
  '@solana/wallet-adapter-phantom',
  '@solana/wallet-adapter-react',
  '@solana/wallet-adapter-solflare',
  '@solana/wallet-adapter-sollet',
  '@solana/wallet-adapter-solong',
  '@solana/wallet-adapter-torus',
  '@solana/wallet-adapter-wallets',
  '@project-serum/sol-wallet-adapter',
])

module.exports = withTM({
  webpack5: true
})

That allowed me to use the dependencies, but then the next error popped up:

window is not defined

which means of the packages has a dependency on window (not possible on the server) so I had to dynamically load the wallet provider like so:

ClientWalletProvider.tsx:

import type { WalletProviderProps } from '@solana/wallet-adapter-react'
import { WalletProvider } from '@solana/wallet-adapter-react'
import {
  getLedgerWallet,
  getMathWallet,
  getPhantomWallet,
  getSolflareWallet,
  getSolletWallet,
  getSolongWallet,
} from '@solana/wallet-adapter-wallets'
import type { ReactNode } from 'react'
import { useMemo } from 'react'

export function ClientWalletProvider(
  props: Omit<WalletProviderProps, 'wallets'> & { children: ReactNode }
): JSX.Element {
  const wallets = useMemo(
    () => [
      getPhantomWallet(),
      getSolflareWallet(),
      getLedgerWallet(),
      getSolongWallet(),
      getMathWallet(),
      getSolletWallet(),
    ],
    []
  )

  return <WalletProvider wallets={wallets} {...props} />
}

and in my _App.tsx:

import type { AppProps } from 'next/app'
import dynamic from 'next/dynamic'

const WalletProvider = dynamic(() => import('../ClientWalletProvider'), {
  ssr: false,
})

export default function App({ Component, pageProps }: AppProps): JSX.Element {
  return (
    <WalletProvider autoConnect>
      <Component {...pageProps} />
    </WalletProvider>
  )
}

This works for now, but it is quite the detour. Any other ideas?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 9
  • Comments: 25 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Nice, good catch with shx and thanks for the addition to the docs. I’ve merged and will close this, but please reopen if other issues are found.

Sure thing! I’ll have a looksy tomorrow.

Hey @jordansexton! Sorry I didn’t help out with the PR more. You got to it quick! Thanks for doing all of that. I tested it locally and only ran into a few snags that I thought someone else could run into and be confused about. shx wasn’t a dev dep and I think it’s worth including it if you’re going to be using that as part of the clean script otherwise it errors out.

And then simply adding the instructions to install the deps. 🤷🏻‍♂️ Just some thoughts. Other than that everything works as expected!! Good job and thank you for being so on top of everything!

My PR with those additions: https://github.com/solana-labs/wallet-adapter/pull/58/files

@Hossman333 @grumpyoldman-io would anyone be willing to PR an addition to the readme for Next.js config? Would love to help other users coming to this and be able to close this issue.

I created a throwaway branch using esbuild: https://github.com/solana-labs/wallet-adapter/pull/50

I’m not happy with the result though. There are various issues with the webpack starter projects using it, and from what I can tell we’d lose tree shaking for the wallets package, which is kind of a dealbreaker IMO.

perhaps the solution to this problem can be in the form of documentation

Let’s go with this approach. I would be happy to accept any PRs to document use with different frameworks and build systems.

Just to keep the discussion centralized, this is a huge step forward but I think there will always be a need for transpiling the dependencies, perhaps the solution to this problem can be in the form of documentation, as bundling third party libraries might have a negative effect on the final bundle size and could be a bit too opinionated.

Thanks for the report and documenting the workaround. I’m a big fan of Next.js! I’ll test next week and see if we can provide a starter project using it, and try to rework the window checks so that they won’t hard fail with SSR.

My good sir…I might be stupid. Hahah. I moved out the ClientWalletProvider.tsx outside of the pages directory and it worked that time. 🤦🏻‍♂️

Thank you for being my rubber ducky and I’m sorry about the spam. This workaround will work great for now. Thank you

@Hossman333 can you perhaps share your error? I think it boils down to adding some third party dependencies to the next.config.js until it works. There is stuff in the works to make the wallet adapters more compatible with other frameworks like NextJS