Next.js注释函数后仍出现Module Not Found错误原因咨询
问题
跟随Next.js官方教程学习getStaticProps时,将getSortedPostsData()注释或改为字符串后,明明没使用这个模块,却依然出现无法从'fs'导入的错误。相关代码如下:
import Head from 'next/head'; import styles from '../styles/Home.module.css'; import Link from 'next/link'; import { getSortedPostsData } from '../lib/posts'; import Layout from '../components/layout' import utilStyles from '../styles/utils.module.css'; export async function getStaticProps() { console.log("get static props runs") const allPostsData = 'getSortedPostsData();' return { props: { allPostsData, }, }; } export default function Home({ allPostsData }) { return ( <Layout home> <div className={styles.container}> <Head> <title>Create Next App</title> <link rel="icon" href="/favicon.ico" /> </Head> <main> <h1 className={styles.title}> Read <Link href="/posts/first-post">this page!</Link> </h1> <p className={styles.description}> Get started by editing <code>pages/index.js</code> </p> <div className={styles.grid}> <a href="https://nextjs.org/docs" className={styles.card}> <h3>Documentation →</h3> <p>Find in-depth information about Next.js features and API.</p> </a> <a href="https://nextjs.org/learn" className={styles.card}> <h3>Learn →</h3> <p>Learn about Next.js in an interactive course with quizzes!</p> </a> <a href="https://github.com/vercel/next.js/tree/master/examples" className={styles.card} > <h3>Examples →</h3> <p>Discover and deploy boilerplate example Next.js projects.</p> </a> <a href="https://vercel.com/import?filter=next.js&utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" className={styles.card} > <h3>Deploy →</h3> <p> Instantly deploy your Next.js site to a public URL with Vercel. </p> </a> </div> </main> <footer> <a href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" target="_blank" rel="noopener noreferrer" > Powered by{' '} <img src="/vercel.svg" alt="Vercel" className={styles.logo} /> </a> </footer> <style jsx>{` main { padding: 5rem 0; flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; } footer { width: 100%; height: 100px; border-top: 1px solid #eaeaea; display: flex; justify-content: center; align-items: center; } footer img { margin-left: 0.5rem; } footer a { display: flex; justify-content: center; align-items: center; text-decoration: none; color: inherit; } code { background: #fafafa; border-radius: 5px; padding: 0.75rem; font-size: 1.1rem; font-family: Menlo, Monaco, Lucida Console, Liberation Mono, DejaVu Sans Mono, Bitstream Vera Sans Mono, Courier New, monospace; } `}</style> <style jsx global>{` html, body { padding: 0; margin: 0; font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif; } * { box-sizing: border-box; } `}</style> </div> <section className={`${utilStyles.headingMd} ${utilStyles.padding1px}`}> <h2 className={utilStyles.headingLg}>Blog</h2> <ul className={utilStyles.list}> {/* {allPostsData .map(({ id, date, title }) => ( <li className={utilStyles.listItem} key={id}> {title} <br /> {id} <br /> {date} </li> ))} */} {console.log(`info: ${allPostsData}`)} </ul> </section> </Layout> ) }
原以为注释后未使用该模块就不会产生错误,请问这是为什么?
回答
问题出在你依然保留了import { getSortedPostsData } from '../lib/posts';这行导入代码。
哪怕你没有调用getSortedPostsData函数,只要导入了../lib/posts模块,Node.js/Next.js就会加载并解析这个模块的全部内容——包括模块内部对fs模块的导入(官方教程里的posts.js正是用fs读取本地markdown文件的)。
fs是Node.js的核心模块,只能在服务器端环境使用。如果Next.js在不支持fs的环境下尝试加载该模块,就会抛出导入错误;即使是在服务器端执行的getStaticProps,只要模块被导入就会触发依赖解析,一旦posts.js里的fs导入在模块顶级作用域,就会被执行。
解决方法很简单:把那行导入getSortedPostsData的代码注释或删除,这样就不会加载包含fs依赖的posts模块,错误自然会消失。
内容的提问来源于stack exchange,提问作者PythonPadawan
相关产品推荐
相关产品推荐

