从 Next.js 基本示例演练中获取错误

oka*_*p12 4 next.js

运行 Next JS 入门演练中的代码时出现此错误:


    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1174:13)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.remark (C:\*******\***********\.next\server\pages\index.js:3249:18)
    at __webpack_require__ (C:\*******\***********\.next\server\webpack-runtime.js:25:42)
    at Object../lib/posts.js (C:\*******\***********\.next\server\pages\index.js:132:64)
    at __webpack_require__ (C:\*******\***********\.next\server\webpack-runtime.js:25:42)
    at Object../pages/index.js (C:\*******\***********\.next\server\pages\index.js:2933:68) {
  code: 'ERR_REQUIRE_ESM'
}
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\**********\********\node_modules\remark\index.js
require() of ES modules is not supported.
require() of C:\*******\***********\node_modules\remark\index.js from C:\*******\***********\.next\server\pages\index.js is an ES module file as 
it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename C:\***********\********\node_modules\remark\index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\*******\***********\node_modules\remark\package.json.
Run Code Online (Sandbox Code Playgroud)

以下是本教程该部分重点关注的文件 ([id].js):

import { getAllPostIds, getPostData } from '../../lib/posts'

export async function getStaticProps({ params }) {
  const postData = await getPostData(params.id)
  return {
    props: {
      postData
    }
  }
}

export async function getStaticPaths() {
  const paths = getAllPostIds()
  return {
    paths,
    fallback: false
  }
}

export default function Post({ postData }) {
    return (
        <html>
            {postData.title}
            <br />
            {postData.id}
            <br />
            {postData.date}
            <br />
            <div dangerouslySetInnerHTML={{ __html: postData.contentHtml }} />
        </html>
    )
  }
Run Code Online (Sandbox Code Playgroud)

这是 package.json 文件:

{
  "name": "abhayrajio",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "fs": "0.0.1-security",
    "gray-matter": "^4.0.3",
    "next": "11.0.1",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "remark": "^14.0.1",
    "remark-html": "^13.0.1"
  },
  "devDependencies": {
    "eslint": "7.32.0",
    "eslint-config-next": "11.0.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

我对此很陌生,只是想了解代码是如何工作的。出了什么问题以及我应该采取什么措施来纠正它?

这是演练链接:https://nextjs.org/learn/basics/create-nextjs-app

小智 9

您可以使用以前版本的remark和remark-html

   "remark": "^13.0.0",
   "remark-html": "^13.0.1"
Run Code Online (Sandbox Code Playgroud)

  • 我还必须像这样替换导入:`import remark from 'remark'` (2认同)