【问题标题】:Nextjs not logging console.log or console.error when running with "next"使用“next”运行时,Nextjs 未记录 console.log 或 console.error
【发布时间】:2020-07-29 02:35:42
【问题描述】:

所以我的server.js 文件中有这个日志:

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

我的package.json 有脚本:

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

当我使用 npm run start 运行服务器时,一切正常,我可以看到日志,但 npm run dev 没有 console.log()console.error() 工作。

我尝试了选项 quiet true 和 false 但仍然无法正常工作:

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

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

我的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;
    },
});

【问题讨论】:

标签: webpack next.js


【解决方案1】:

转到您运行npm run dev 的终端。 您应该可以在那里看到您的控制台日志。

【讨论】:

    【解决方案2】:

    如果你想在本地开发模式下查看你的服务器日志,你不需要使用你自己的服务器。您可以像这样在 package.json 中编辑开发脚本:

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

    当您运行您的开发 (npm run dev) 时,您可以在 Chrome 中访问:chrome://inspect

    在远程目标下方,点击inspect

    【讨论】:

    • 不适合我:'NODE_OPTIONS' 未被识别为内部或外部命令、可运行程序或批处理文件。
    • 这不起作用
    【解决方案3】:

    我使用包 cross-env 来避免在使用不同的操作系统时出现一些问题。

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

    工作正常,我现在可以看到我的所有日​​志与 Next 日志混合在一起。

    【讨论】:

      【解决方案4】:

      作为@AryanJ-NYC mentioned,在Next.js 中使用custom server 时,您需要更新package.json 中的脚本以运行server.js 文件。

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

      【讨论】:

        猜你喜欢
        • 2018-12-07
        • 2020-10-21
        • 1970-01-01
        • 2019-06-06
        • 1970-01-01
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 2022-12-21
        相关资源
        最近更新 更多