7

I'm working on a website created with Nextjs. But when I run it on my local it doesn't load anything on the console just show [ event ] build page: /_error. I tried to log the _error.js but nothing had been shown. The following are my configuration:

server/index.js

// Use dotenv Config Package
const dotenv = require('dotenv');
// Express Package and Next Package For Run server
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const next = require('next');
const nextI18NextMiddleware = require('next-i18next/middleware').default
dotenv.config();

const NextI18NextInstance = require('../i18n');
const port = parseInt(process.env.PORT, 10) || 3000; // Port Application Run Load From dotenv File
const dev = process.env.NODE_ENV !== 'production'; // Application Run Environment Load From dotenv File
const app = next({ dev }); // Add Loaded Config to Application
const handle = app.getRequestHandler();

const proxyOptions  = {
    target: process.env.API_PROXY_URL + ':' + process.env.API_PROXY_PORT,
    changeOrigin: true,
    ws: true,
};

const devProxy = createProxyMiddleware(proxyOptions);

app.prepare()
    .then(() => {
        // Run Express Server
        const server = express();

        server.use(nextI18NextMiddleware(NextI18NextInstance));

        server.use('/api', devProxy);




        // Handle All Server Requests And Responses
        server.all('*', (req, res) => {
            return handle(req, res);
        });

        // Server Listen Port Config
        server.listen(port, err => {
            if (err) throw err;
            console.log(`> Ready on http://localhost:${port}`);
        });

    })
    // Application Catch Error On Run Server
    .catch(ex => {
        console.error(ex.stack);
        process.exit(1);
    });

i18n.js

// Import Next i18n Package
const NextI18Next = require('next-i18next').default;

// Create and Export Default i18n Config Module use in Whole Project
const languages = ['fa-IR', 'en-US'];
const options = {
    defaultLanguage: 'fa', // Default Language
    otherLanguages: ['en'], // Other Languages Can Add In Array
    // browserLanguageDetection: true, // Detect Language From Browser Meta
    defaultNS: 'common', // Default File Load in Ns
    localeExtension: 'json', // Default language Files Extension
};

const NextI18NextInstance = new NextI18Next(options);

NextI18NextInstance.i18n.languages = languages;

module.exports = NextI18NextInstance;

pages/index.js

import React, { Component } from 'react';
import {SiteLayout} from '../components';
import { i18n,withTranslation } from '../i18n'

class MainPage extends Component {

    render() {
        return (
            <SiteLayout pageTitle={'صفحه اصلی'}>
                {this.props.t('mainPage')}
            </SiteLayout>
        );
    }
}

MainPage.getInitialProps = async () => ({
    namespacesRequired: ['MainPage'],
});

export default withTranslation('MainPage')(MainPage);

after the server is up and show > Ready on http://localhost:3000 I got the following output in terminal and browser stuck loading the page. if you need more info ask. is there any idea how to debug this or fix it? thanks in advance.

[HPM] Proxy created: /  -> https://myapi**.com:
> Ready on http://localhost:3000
[ event ] build page: /_error
Saeed Amin
  • 145
  • 1
  • 3
  • 8

7 Answers7

1

Solved.How?Firstly i tried to remove node_modules and reinstall them,no luck.After that I upgraded next.js version 11 with version 12 and compiling works well.You can look here in the docs upgrading process upgrade process

Goran_Ilic_Ilke
  • 812
  • 12
  • 16
1

try to have a look at this (Next.js dev server gets stuck after a while, requests just spin) issue, there are several approaches. I problem was solved by changing the port number, following this Solution.

1

For me, the issue was the older version of nodejs

For nextjs v13, min. node version is v16 But, I was using v14. Due to this, I just got the following logs on running next dev:

> started server on 0.0.0.0:3000, url: http://localhost:3000
>  Loaded env from /Users/deepak/my-web-app/.env

With this node version, the browser kept loading endlessly without any error on browser or terminal.

But, after switching to node v16, I finally got the following logs and the app worked fine after that:

> wait  - compiling / (client and server)...
> event - compiled client and server successfully in 3s (13135 modules)
Deepak
  • 2,487
  • 3
  • 21
  • 27
1

For me, the issue was really weird. When I ran lsof -i tcp:3000 there was a node instance running (probably NextJs) but when I ran npm run dev I didn't get an error. NextJs started just fine. I somehow got two NextJs instances running at the same time, so when I tried to reach 127.0.0.1:3000, it just wouldn't load. I killed that process and the issue was fixed.

0

In case that you use windows WSL. first check this

if didn't work do this as last option:

changing wsl2 to wsl1 solved this problem for me (I don't know why)!

you can do that with this code on PowerShell:

wsl --set-version Ubuntu 1
sarem eskandary
  • 504
  • 4
  • 16
0

try deleting the .next folder and run npm run dev it should work

Ons Jannet
  • 177
  • 2
  • 2
  • 10
-1

Today I also face this problem and it takes me hours to figure it out, my false is in the next.config.js file. Please check if it can help you.

const path = require("path");
const subFolder = 'src';
module.exports = {
    webpack: config => {
        config.node = {
            fs: "empty"
        };

        // You can see MANY IMPLEMENTED ALIASES when printing this:
        console.log(config.resolve.alias);

        // So if you want to add your own aliases, don't replace the old one:
        // PLEASE DON'T DO THIS >>>
        // config.resolve.alias = {
        //     "@components": path.join(__dirname, "components"),
        //     "@pages": path.join(__dirname, "pages"),
        //     "@redux": path.join(__dirname, "redux"),
        //     "@api": path.join(__dirname, "api")
        // };
        // <<< PLEASE DON'T DO THIS


        // DO THIS INSTEAD >>>
        config.resolve.alias = {
            ...config.resolve.alias // <<< ADD THIS LINE to extend the old one
            "@components": path.join(__dirname, "components"),
            "@pages": path.join(__dirname, "pages"),
            "@redux": path.join(__dirname, "redux"),
            "@api": path.join(__dirname, "api")
        };
        // <<< DO THIS INSTEAD
      return config;
    },
};
Ha Dinh Viet
  • 1
  • 1
  • 2