Karthi
Karthi

Reputation: 3489

Next.js returns 500: internal server error in Production

Created a next.js full stack application. After production build when I run next start it returns 500 : internal server. I'm using environment varibles for hitting api.

env.development file

BASE_URL=http://localhost:3000

It was working fine in development service.ts

import axios from 'axios';
const axiosDefaultConfig = {
  baseURL: process.env.BASE_URL, // is this line reason for error?
  headers: {
    'Access-Control-Allow-Origin': '*'
  }
};
const axio = axios.create(axiosDefaultConfig);

export class Steam {
  static getGames = async () => {
    return await axio.get('/api/getAppList');
  };
}

Upvotes: 3

Views: 7735

Answers (2)

ddon-90
ddon-90

Reputation: 2976

Do you have a next.config.js file?

To add runtime configuration to your app open next.config.js and add the publicRuntimeConfig and serverRuntimeConfig configs:

module.exports = {
  serverRuntimeConfig: {
    // Will only be available on the server side
    mySecret: 'secret',
    secondSecret: process.env.SECOND_SECRET, // Pass through env variables
  },
  publicRuntimeConfig: {
    // Will be available on both server and client
    staticFolder: '/static',
  },
}

To get access to the runtime configs in your app use next/config, like so:

import getConfig from 'next/config'

// Only holds serverRuntimeConfig and publicRuntimeConfig
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig()
// Will only be available on the server-side
console.log(serverRuntimeConfig.mySecret)
// Will be available on both server-side and client-side
console.log(publicRuntimeConfig.staticFolder)

function MyImage() {
  return (
    <div>
      <img src={`${publicRuntimeConfig.staticFolder}/logo.png`} alt="logo" />
    </div>
  )
}

export default MyImage

I hope this helps.

Upvotes: 3

Anas M.I
Anas M.I

Reputation: 582

I dont think you have setup env.

You need to configure it for it to work. Try it without it and it should work fine!

Upvotes: 1

Related Questions