fetch icon indicating copy to clipboard operation
fetch copied to clipboard

Can't resolve 'dns'

Open okaysuper opened this issue 3 years ago • 2 comments

Hello,

I'm trying to use @vercel/fetch in nextjs on the serverside Whatever I try I always have the same error:

./node_modules/@zeit/dns-cached-resolve/lib/dns-resolve.js:6:0 Module not found: Can't resolve 'dns'

Am I doing something wrong?

Thanks for your help.

okaysuper avatar Mar 15 '21 18:03 okaysuper

@okaysuper I had the same problem trying to use this in nextjs. I think the problem is that dns is built into node, but isn't available in the browser. You need to make sure you're using this server side only.

I have a file that is both server and client side so my solution is something like:

function getFetch() {
  if (typeof window === 'undefined') {
    const createFetch = require('@vercel/fetch');
    return createFetch();
  }
  return window.fetch;
}

Alternatively if you're in nextjs and doing something in getStaticProps or getServerSideProps if you only use this package there nextjs will take care of not including this in the client bundle and causing errors.

nsmith7989 avatar Apr 30 '21 16:04 nsmith7989

@nsmith7989 thanks for your answer. As far as I know I'm using it only on the server side but you are probably right. @vercel/fetch somehow ended up in the client bundle otherwise I wouldn't get this error. I'll investigate further or try your solution if I need it on the client. Thanks!

okaysuper avatar Apr 30 '21 17:04 okaysuper

Thank you for the report. We are archiving this project; you can read more details here: https://github.com/vercel/fetch/issues/83

Ethan-Arrowood avatar Nov 09 '23 22:11 Ethan-Arrowood