【问题标题】:Next.js - Is it possible to debug getServerSideProps?Next.js - 是否可以调试 getServerSideProps?
【发布时间】:2021-01-14 02:19:39
【问题描述】:

您好,根据文档getServerSideProps 是一个仅在服务器端执行的函数。

export async function getServerSideProps(context) {
  console.log("hello world");
  debugger;
  return {
    props: {
      age:55
    }, // will be passed to the page component as props
  }
}

因此,简单地删除调试器关键字是行不通的。

我试过了:

  • 在端口 9229 上附加 node.js 调试器
  • 正在运行node --inspect-brk ./node_modules/next/dist/bin/next
  • 正在运行cross-env NODE_OPTIONS='--inspect' next dev

但是 console.log()debugger 关键字似乎都没有任何效果。

但是我的 getServerSideProps 的 props 是正确传入的,这证明函数被调用了。

是否可以逐步执行 getServerSideProps 或至少让 console.log 工作?谢谢!

附言

getServerSideProps 之外的代码行是可调试的并且可以按预期工作。

【问题讨论】:

    标签: next.js


    【解决方案1】:

    为了使打开调试端口正常打开,您需要执行以下操作:

    // package.json scripts
    "dev": "NODE_OPTIONS='--inspect' next dev"
    // if you want custom debug port on a custom server
    "dev": "NODE_OPTIONS='--inspect=5009' node server.js",
    

    一旦调试端口是自己的,您应该能够使用您选择的inspector clients

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-17
      • 2010-12-29
      • 2021-11-14
      • 2023-01-12
      • 2020-05-03
      • 2014-12-08
      • 1970-01-01
      • 2020-10-14
      相关资源
      最近更新 更多