0

I am new to Next JS. I can't build my application locally. When I enter the npm run build command I get the following errors :

...
_currentUrl: 'http://localhost:3003/data/menus.json',
    [Symbol(kCapture)]: false
  },
  response: undefined,
  isAxiosError: true,
  toJSON: [Function: toJSON]
}
Error: connect ECONNREFUSED 127.0.0.1:3003
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1148:16) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 3003,
  config: {
    url: 'http://localhost:3003/data/menus.json',
    method: 'get',
    headers: {
      Accept: 'application/json, text/plain, */*',
      'User-Agent': 'axios/0.21.4'
    },
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    adapter: [Function: httpAdapter],
...

Et à la fin du log

...
info  - Generating static pages (771/771)

> Build error occurred
Error: Export encountered errors on following paths:
        /ar
        /ar/classic
        /ar/minimal
        /ar/standard
        /ar/vintage
        /de
        /de/classic
        /de/minimal
        /de/standard
        /de/vintage
        /en
        /en/classic
        /en/minimal
        /en/standard
        /en/vintage
        /es
        /es/classic
        /es/minimal
        /es/standard
        /es/vintage
        /fr
        /fr/classic
        /fr/minimal
        /fr/standard
        /fr/vintage
        /he
        /he/classic
        /he/minimal
        /he/standard
        /he/vintage
        /zh
        /zh/classic
        /zh/minimal
        /zh/standard
        /zh/vintage
    at C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\export\index.js:487:19
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Span.traceAsyncFn (C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\telemetry\trace\trace.js:60:20)
    at async C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\build\index.js:833:17
    at async Span.traceAsyncFn (C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\telemetry\trace\trace.js:60:20)
    at async C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\build\index.js:707:13
    at async Span.traceAsyncFn (C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\telemetry\trace\trace.js:60:20)
    at async Object.build [as default] (C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\build\index.js:77:25)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1
Command: C:\Program Files\nodejs\node.exe
Arguments: C:\Users\PC\.node\corepack\yarn\1.22.15\lib\cli.js build
Directory: C:\Users\PC\OneDrive\Documents\lab\samara\laravel\shop
Output:

info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.
error Command failed with exit code 1.

The npm run dev command works without any problem. So locally the application has no problem. But when I go to production, I want to build I get these errors above.

Here is my server.js file:

// server.js
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = 3003;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  createServer(async (req, res) => {
    try {
      // Be sure to pass `true` as the second argument to `url.parse`.
      // This tells it to parse the query portion of the URL.
      const parsedUrl = parse(req.url, true);
      const { pathname, query } = parsedUrl;

      if (pathname === '/a') {
        await app.render(req, res, '/a', query);
      } else if (pathname === '/b') {
        await app.render(req, res, '/b', query);
      } else {
        await handle(req, res, parsedUrl);
      }
    } catch (err) {
      console.error('Error occurred handling', req.url, err);
      res.statusCode = 500;
      res.end('internal server error');
    }
  }).listen(port, (err) => {
    if (err) throw err;
    console.log(`> Ready on http://${hostname}:${port}`);
  });
});

And my package.json file

{
  "name": "@samara/shop",
  "version": "1.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev -p 3003",
    "build": "next build",
    "start": "NODE_ENV=production node server.js"

If you need another file to check let me know.

  • 1
    Welcome to Stackoverflow - Is it possible that you are requesting `http://localhost:3003/data/menus.json` before the server is up? Where is this being called? – Ramakay Feb 16 '22 at 22:31
  • Please provide the code for one of the failing pages. – juliomalves Feb 18 '22 at 20:02

2 Answers2

3

I don't know if it's the best way but in my case I run npm run dev then run npm run build and the error stops happening then stop the dev and run npm run start

you can also add a try catch in the api call http://localhost:3003/data/menus.json if you are calling in a getStaticProps function, you can in the catch set the revalidity to 1 second if you need to revalidate

Thales
  • 31
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 10 '22 at 07:05
  • This doesn't really help - npm run build only succeeds because the dev server is running in the background. As soon as you exit the dev server then the same calls will fail when doing npm run start as well – Dirk R Jan 26 '23 at 13:56
0

(i know this was posted a long time ago) Try using the port thats given inside of the error message. For example, for the error you would experiencing the port would be 3003 (the port: 3003, line tells you it). I had this error too and I just changed the port in my browser to the port given.

  • Post some code that shows how to do this. – sanitizedUser Jul 10 '23 at 08:45
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 10 '23 at 08:45
  • Theres no code to add. You just change the port in your browser. – DeveloLongScript Jul 22 '23 at 19:44