繁体   English   中英

npm 运行构建错误,无法在next.js 中运行构建命令

[英]npm run build error , unable to run build command in next.js

我正在尝试运行 npm run build 但我总是收到错误消息。即使所有页面都运行良好且没有错误。 我已经使用 getServerSidePorps 添加了用于 api 调用的代码。

错误:

Error: connect ECONNREFUSED 127.0.0.1:3000
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
  type: 'Error',
  config: {
    url: 'http://localhost:3000/api/admin/user/users/list',
    method: 'get',
    headers: {
      Accept: 'application/json, text/plain, */*',
      'User-Agent': 'axios/0.21.4'
    },
    transformRequest: [ null ],
    transformResponse: [ null ],
    timeout: 0,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    }
  },
  code: 'ECONNREFUSED'
}

API 调用

export const getServerSideProps = async () => {
  const additionalData = {
    InitialSkip: 0,
    initialLimit: 3,
  };
  const response = await axios.post(
    "http://localhost:3000/api/admin/user/users/list",
    additionalData,
    {
      headers: {
        "Content-Type": "application/json",
      },
    }
  );

  return {
    props: {
      data: response.data,
    },
  };
};

问题 -

npm build 因连接错误而失败。

回答 -

您不应fetch()getServerSideProps()的 API 路由。

注意:您不应该使用 fetch() 在 getServerSideProps 中调用 API 路由。 相反,直接导入 API 路由中使用的逻辑。 您可能需要为这种方法稍微重构您的代码。

从外部 API 获取是很好的!

查看此链接了解更多信息 -

这里的问题是您调用的是 API 路由而不是外部 API。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM