Nextjs 在类型注释上“模块解析失败”(Yarn 工作区)

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

我正在尝试测试/更改项目从 Vite+React 到 Nextjs+React。
我在这里检查了此页面:https://nextjs.org/learn-pages-router/foundations/from-react-to-nextjs/getting-started-with-nextjs然后我相应地更改了项目。

但是,在运行下一个开发后,我在这里收到以下错误:

const customErrorMap: z.ZodErrorMap = (issue, ctx) => {
Module parse failed: Unexpected token (5:20)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders

webpack.config.js:
我目前没有 webpack.config 文件设置,但我之前尝试过设置它,然后在认为它可能与问题无关后将其删除,因为文档说您不需要它来将 typescript 与 nextjs 一起使用。
依赖版本:

{
  "typescript": "^5.1.6",
  "next": "^14.0.3",
  // "vite": "^3.2.0",
  "react": "^18.2.0",
  "react-dom": "^18.2.0"
}

更新

我发现只有从使用

yarn workspaces
添加的本地工作区导入时才会发生这种情况。

project-name
| - backend
| - frontend

前端/package.json

  dependencies: {
    "@project-name/backend": "1.0.0",
    ...
  }

yarn 工作空间是否存在兼容性问题?
我需要设置什么才能使其正常工作?

reactjs typescript next.js annotations
1个回答
0
投票

我通过将

transpilePackages
添加到
next.config.js

解决了该问题
/** @type {import('next').NextConfig} */
const nextConfig = {
    transpilePackages: ['@project-name/backend'],
}

module.exports = nextConfig
© www.soinside.com 2019 - 2024. All rights reserved.