56

So I have this logs in my server.js file:

  console.info("LOOGGGGGSSSS")
  console.warn("LOOGGGGGSSSS")
  console.log("LOOGGGGGSSSS")
  console.error("LOOGGGGGSSSS")

My package.json has script:

  "scripts": {
    "dev": "next",
    "start": "next start"
  }

When I run server with npm run start all works correctly and I can see logs but with npm run dev no console.log() or console.error() works at all.

I tried with option quiet true and false but still not working:

const nextJsApp = next({dev, quiet: false}); // no logs in terminal

and

const nextJsApp = next({dev, quiet: true}); // still no logs in terminal

My next.config.js

require("dotenv").config();
const withCSS = require('@zeit/next-css');
const webpack = require('webpack');

const apiKey =  JSON.stringify(process.env.SHOPIFY_API_KEY);

module.exports = withCSS({
    webpack: (config) => {
        const env = { SHOPIFY_API_KEY: apiKey };
        config.plugins.push(new webpack.DefinePlugin(env));
        return config;
    },
});
1

9 Answers 9

53

Go to the terminal where you ran npm run dev. You should be able to see your console log there.

Sign up to request clarification or add additional context in comments.

2 Comments

but i want to see it in chrome console
@Getzel The whole point of a server side component / server side rendering (getStaticProps) is that any JavaScript logic is performed on, well, the server, before it gets to the client. So if you want to see it in your browser, you have to call console.log in your client side logic.
12

If you want to see your server logs in local development mode, you do not need to use your own server. You can edit the dev Script in your package.json like this:

"dev": "NODE_OPTIONS='--inspect' next dev",

and when you run your dev (npm run dev) you can go in Chrome to: chrome://inspect.

At the bottom under remote target, click on inspect.

3 Comments

not working for me: 'NODE_OPTIONS' is not recognized as an internal or external command, operable program or batch file.
This didn't work
In Windows you need to use the cross-env package to be able to set the NODE_OPTIONS variable, as seen in the answer by waliby.
9

I used the package cross-env to avoid some issue when working with different OS.

"dev": "cross-env NODE_OPTIONS='--inspect' next dev"

Works just fine, I can now see all my logs mixed with Next logs.

See this in the official Next.js docs.

1 Comment

This seems to break the hot reloading feature of the app
3

I think it's cause you have to use "use client"

if your using next.js 13, every component is a server side component by default which means the component won't run on the browser, if you want to use client side components write "use client" on the top of your file.

but I won't recommend putting "use client" on top just because you want the information to display in the browser you can see this link to learn more about client side and server side components https://nextjs.org/docs/getting-started/react-essentials

Comments

2

The console.log info is on the terminal.

Open your OS terminal, look at your terminal, you will see the console.log data.

1 Comment

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.
1

I had to remove this from my next.config.js. I figured it only applied for production, but maybe not.

    removeConsole: {
      exclude: ["error"],
    },

I will add this to tsconfig.json instead:

"removeComments": true,

1 Comment

thanks lol that was my issue
0

In Next.js, console messages don't show up in the browser by default because pages are server-rendered. To fix this, add "use client" at the top of your file to make console.log messages visible in the browser console. Happy coding! 👍

Comments

-1

I encountered the same issue. Later I found that all the console.log outputs of UI component (frontend) were visible in the browser console and non UI methods or backend logs in terminals.

Example:


// on browser console
export default function Detail({ id, data, notFound }) {
  console.log(" data ",notFound) 
  
  useEffect(() => {
    console.log("Component mounted or updated"); // on browser console
  }, []);
  
  return (
    <> ... </>
  );
}

// on IDE terminal
export async function getStaticPaths() {
  console.log("Generating static paths"); 
  return {
    paths: [],
    fallback: 'blocking',
  };
}

// on IDE terminal
export async function getStaticProps({ params }) {
  const { id } = params;
  console.log('Fetching data for id:', params?.id);
  
  try {
    const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${id}`);
    if (!res.ok) {
      return { props: { notFound: true } };
    }
    const data = await res.json();
    console.log('Fetched data:', data);
    return { 
      props: { data },
      revalidate: 60
    };
  } catch (error) {
    console.error('Error fetching data:', error.message);
    return { props: { notFound: true } };
  }
}

1 Comment

Edited answer for correction and added example for more clarity.
-2

As @AryanJ-NYC mentioned, when using a custom server in Next.js you'll need to update the scripts in package.json to run the server.js file.

"scripts": {
    "dev": "node server.js",
    "build": "next build",
    "start": "NODE_ENV=production node server.js"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.