Next.js - Error: only absolute urls are supported

后端 未结 6 1186
感动是毒
感动是毒 2020-12-29 10:05

I\'m using express as my custom server for next.js. Everything is fine, when I click the products to the list of products

Step 1: I click the produc

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-29 10:29

    In the NextJS 9.5, we can also use process.cwd().
    process.cwd() will give you the directory where Next.js is being executed.

    import path from 'path'
    import fs from "fs";
    
    export const getStaticProps: GetStaticProps = async () => {
        const dataFilePath = path.join(process.cwd(), "jsonFiles", "data.json");
        console.log(dataFilePath);     // will be YourProject/jsonFiles/data.json
    
        const fileContents = fs.readFileSync(dataFilePath, "utf8");
        const data: TypeOfData[] = JSON.parse(fileContents);
        return { props: { data } };
    };
    

    Ref: https://nextjs.org/docs/basic-features/data-fetching#reading-files-use-processcwd

提交回复
热议问题