2

When trying to deploy website I get get the error invalid url error from @project-serum/anchor. Is there an alternative anchor package I could use?

TypeError [ERR_INVALID_URL]: Invalid URL
    at new NodeError (node:internal/errors:399:5)
    at new URL (node:internal/url:560:13)
    at new Connection (/vercel/path0/back-alley-solana/node_modules/@project-serum/anchor/node_modules/@solana/web3.js/lib/index.cjs.js:6036:15)
    at /vercel/path0/back-alley-solana/.next/server/chunks/163.js:461:20 {
  input: 'undefined',
  code: 'ERR_INVALID_URL'
}
> Build error occurred
Error: Failed to collect page data for /
    at /vercel/path0/back-alley-solana/node_modules/next/dist/build/utils.js:1158:15 {
  type: 'Error'
}
2
  • are you passing a proper URL while defining the RPC connection? a proper URL would look something like this - rpc-provider.com. you might have used environment variables while defining the RPC connection and forgot to add the environment variable in vercel.
    – 0xMukesh
    Jul 16, 2023 at 5:03
  • It is hard to tell what is exactly causing your problem here without seeing the code. Can you share some of the code for how you are creating your connection? I suspect that you are incorrectly setting the cluster url
    – nickfrosty
    Jul 17, 2023 at 13:35

2 Answers 2

2

It is difficult to troubleshoot your error with seeing any of your code, but it looks like you are incorrectly setting an RPC url (or maybe even not setting one at all) when you initiate your Connection to your Solana RPC provider.

Using the @solana/web3.js package, you can create a new connection to any RPC via its url. Including using one of the public RPC listed in the Solana Docs.

import { Connection, clusterApiUrl } from "@solana/web3.js"

// connect to 'devnet' via the public endpoint
const connection = new Connection(clusterApiUrl("devnet"));

// connect to 'mainnet' via the public endpoint
const connection = new Connection(clusterApiUrl("mainnet-beta"));

// connect to any other RPC provider's url (this url will be provided by them)
const connection = new Connection("https://yourprovider.tld")

Note: clusterApiUrl is a helper function inside the web3.js package for more easily connecting to the public RPC endpoints, but should be not used for production apps since you will hit rate limits.

Now that you should have a valid connection, you can tell Anchor to use it when your create your provider:

const provider = new anchor.AnchorProvider(connection, wallet, {
  commitment: "processed",
});

// note: the `wallet` variable is not included in this code snippet

Not specifically part of your initial question, but worth noting:

I also see you are using the deprecated @project-serum/anchor Anchor package. This package is not maintained and should NOT be used anymore.

You should instead install the official Anchor package @coral-xyz/anchor from the creators and maintainers of Anchor (their company name is Coral):

yarn remove @project-serum/anchor
yarn add @coral-xyz/anchor

FYI: You might also need to delete your node modules directory and reinstall all packages if you run into issues with this too.

3

@project-serum/anchor is retired. You should update your packages to use the new @coral-xyz/anchor. Clean the node modules and try again.

Also, your error message looks like you're trying to initiate a Connection with incorrect RPC url format. Make sure you provided the correct url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.