简体   繁体   English

如何解决 NextJs Error: Failed to load / at loadComponents

[英]How to solve NextJs Error: Failed to load / at loadComponents

I initialized a NextJs app(v10.2.0) via npx create-next-app and replcaced below code in index.js file of the pages folder .我通过 npx npx create-next-app初始化了 NextJs 应用程序(v10.2.0),并在pages folderindex.js文件中替换了下面的代码。 I am trying to fetch data from a json file and then pass it as props to the page component.我正在尝试从 json 文件中获取数据,然后将其作为道具传递给页面组件。

import fs from 'fs/promises';
import path from 'path';

function Home(props) {
  const { products } = props;

  return (
    <ul>
      {products.map((product) => {
        return <li key={product.id}>{product.title}</li>;
      })}
    </ul>
  );
}


export async function getStaticProps() {

  const filePath = path.join(process.cwd(), 'data', 'products-data.json');
  const jsonData = await fs.readFile(filePath);
  const data = JSON.parse(jsonData);

  console.log(data);

  return {
    props: {
      products: data.products
    }
  };
}

export default Home;

And the products-data.json file is: products-data.json文件为:

{
    "products": [{
            "id": "p1",
            "title": "Product 1"
        }, {
            "id": "p2",
            "title": "Product 2"
        },
        {
            "id": "p3",
            "title": "Product 3"
        }
    ]
}

But when i run the npm run dev , i get this error:但是当我运行npm run dev时,我得到了这个错误:

Error: Failed to load /
    at loadComponents (/home/ali/Desktop/NextJs3/nextjs3/node_modules/next/dist/next-server/server/load-components.js:1:1554)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async DevServer.findPageComponents (/home/ali/Desktop/NextJs3/nextjs3/node_modules/next/dist/next-server/server/next-server.js:76:257)
    at async DevServer.renderToHTML (/home/ali/Desktop/NextJs3/nextjs3/node_modules/next/dist/next-server/server/next-server.js:137:542)
    at async DevServer.renderToHTML (/home/ali/Desktop/NextJs3/nextjs3/node_modules/next/dist/server/next-dev-server.js:36:578)
    at async DevServer.render (/home/ali/Desktop/NextJs3/nextjs3/node_modules/next/dist/next-server/server/next-server.js:74:255)
    at async Object.fn (/home/ali/Desktop/NextJs3/nextjs3/node_modules/next/dist/next-server/server/next-server.js:58:672)
    at async Router.execute (/home/ali/Desktop/NextJs3/nextjs3/node_modules/next/dist/next-server/server/router.js:25:67)
    at async DevServer.run (/home/ali/Desktop/NextJs3/nextjs3/node_modules/next/dist/next-server/server/next-server.js:68:1042)
    at async DevServer.handleRequest (/home/ali/Desktop/NextJs3/nextjs3/node_modules/next/dist/next-server/server/next-server.js:32:504)

What is the problem with this code?这段代码有什么问题? is it because of importing core node modules?是因为导入了核心节点模块吗?

Once I changed the promise based version of readFile() to readFileSync() , it worked:) Don't know why, but it works!一旦我将基于 promise 的readFile()版本更改为readFileSync() ,它就起作用了:) 不知道为什么,但它起作用了!

Parts of the code edited:部分代码已编辑:

import fs from 'fs';
.
.
.
const jsonData = fs.readFileSync(filePath);
.
.
.

But couldn't figure out how to use the asynchronous version of readFile in this case.但是在这种情况下无法弄清楚如何使用readFile异步版本。

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

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