Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nextjs not logging console.log or console.error when running with "next"

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;
    },
});
like image 202
CommonSenseCode Avatar asked Apr 15 '20 23:04

CommonSenseCode


3 Answers

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

like image 128
gignu Avatar answered Nov 18 '22 00:11

gignu


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.

like image 27
waliby Avatar answered Nov 18 '22 02:11

waliby


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.

like image 3
Mika Avatar answered Nov 18 '22 02:11

Mika