为什么我在 TypeScript 中收到“文件不是模块”错误

问题描述 投票:0回答:1

我盯着这段代码太久了,我不明白为什么我会收到一个错误,表明这不是一个模块。

文件名:page.tsx

import Link from "next/link";
import SessionList from "./sessionList";

export default function Page() {

    return (
        <article>
            <div className="flex justify-between items-center mb-3">
                <h2 className="p-0 m-0">Session List</h2>
                <Link href="/sessions/edit" className="bg-rr-green px-3 py-1 rounded-lg text-white">Add New</Link>
            </div>
            <SessionList />
        </article>
    )
}

这是一个运行 Tailwind、Auth0 和 MongoDB 的 NextJS 项目。我使用 Auth0 进行身份验证,在姊妹网站上没有任何问题。唯一的区别是我将 Auth0 作为服务器组件加载到layout.tsx 中。我可以运行

npm run dev
并且应用程序运行良好。但是当我尝试构建它时,我得到以下信息:

.next/types/app/sessions/edit/page.ts:2:24
Type error: File 'C:/Users/shane/workspaces/riverrail-admin/app/sessions/edit/page.tsx' is not a module.

  1 | // File: C:\Users\shane\workspaces\riverrail-admin\app\sessions\edit\page.tsx
> 2 | import * as entry from '../../../../../app/sessions/edit/page.js'
    |                        ^
  3 | import type { ResolvingMetadata, ResolvingViewport } from 'next/dist/lib/metadata/types/metadata-interface.js'
  4 |
  5 | type TEntry = typeof import('../../../../../app/sessions/edit/page.js')

我错过了什么?

我尝试删除 .next 文件夹,删除所有节点模块并使用

npm install
重新安装。我这样没有看到任何错误。

reactjs mongodb next.js auth0
1个回答
0
投票
当 package.json 文件中的类型为 commonJs(默认值)并且您尝试使用 import 语句而不是 require 方法来使用模块时,会出现

file is not a module 错误。 因此,您需要将 package.json 文件中的类型更改为 module only。

© www.soinside.com 2019 - 2024. All rights reserved.