title: ECONNREFUSED during 'next build'. Works fine with 'next dev'
body: <p>I have a very simple NextJS 9.3.5 project.
  For now, it has a single pages/users and a single <code>pages/api/users</code> that retrieves all users from a local MongoDB table</p>
  
  <p><strong>It builds fine locally using 'next dev'
  But, it fails on 'next build' with ECONNREFUSED error</strong></p>
  
  <p>page/users</p>
  
  <pre><code>import fetch from "node-fetch"
  import Link from "next/link"
  
  export async function getStaticProps({ params }) {
    const res = await fetch(`http://${process.env.VERCEL_URL}/api/users`)
    const users = await res.json()
    return { props: { users } }
  }
  
  export default function Users({ users }) {
    return (
      &lt;ul&gt;
        {users.map(user =&gt; (
          &lt;li key={user.id}&gt;
            &lt;Link href="/user/[id]" as={`/user/${user._id}`}&gt;
              &lt;a&gt;{user.name}&lt;/a&gt;
            &lt;/Link&gt;
          &lt;/li&gt;
        ))}
      &lt;/ul&gt;
    );
  }
  </code></pre>
  
  <p>pages/api/users</p>
  
  <pre><code>import mongoMiddleware from "../../lib/api/mongo-middleware";
  import apiHandler from "../../lib/api/api-handler";
  
  export default mongoMiddleware(async (req, res, connection, models) =&gt; {
    const {
      method
    } = req
  
    apiHandler(res, method, {
      GET: (response) =&gt; {
        models.User.find({}, (error, users) =&gt; {
          if (error) {
            connection.close();
            response.status(500).json({ error });
          } else {
            connection.close();
            response.status(200).json(users);
          }
        })
      }
    });
  })
  </code></pre>
  
  <p>yarn build</p>
  
  <pre><code>yarn run v1.22.4
  $ next build
  Browserslist: caniuse-lite is outdated. Please run next command `yarn upgrade`
  &gt; Info: Loaded env from .env
  Creating an optimized production build
  
  Compiled successfully.
  
  &gt; Info: Loaded env from .env
  Automatically optimizing pages ..
  Error occurred prerendering page "/users". Read more: https://err.sh/next.js/prerender-error:
  FetchError: request to http://localhost:3000/api/users failed, reason: connect ECONNREFUSED 127.0.0.1:3000
  </code></pre>
  
  <p>Any ideas what is going wrong ? particularly when it works fine with 'next dev' ?</p>
  
  <p>Thank you.</p>