You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js 16 + Yarn 4.10.3 工作区根目录推断错误求助

问题描述

使用npx create-next-app@latest my-app创建Next.js 16.0.0项目,进入项目目录执行yarn安装依赖后,运行yarn dev时出现Turbopack报错,错误信息如下:

yarn dev                                                                  ─╯
   ▲ Next.js 16.0.0 (Turbopack)
   - Local:        http://localhost:3000
   - Network:      http://192.168.1.69:3000

 ✓ Starting...
Error: Turbopack build failed with 1 errors:
./app
Error: Next.js inferred your workspace root, but it may not be correct.
    We couldn't find the Next.js package (next/package.json) from the project directory: /home/person/Projectz/my-app/app
     To fix this, set turbopack.root in your Next.js config, or ensure the Next.js package is resolvable from this directory.
    Note: For security and performance reasons, files outside of the project directory will not be compiled.
    See https://nextjs.org/docs/app/api-reference/config/next-config-js/turbopack#root-directory for more information.

    at ignore-listed frames

已尝试以下两种方案但均无效:

  • 更新Yarn至4.10.3版本(执行yarn set version stable后重新执行yarn);
  • 在next.config.js中设置turbopack.root:
import type { NextConfig } from "next";

import * as path from 'path';

const nextConfig: NextConfig = {
    turbopack: {
        root: path.join(__dirname, '') // package.json is on the same level as next config
    }
};

export default nextConfig;

使用npm或bun启动项目则完全正常,怀疑是Yarn 4.10.3与Next.js 16的兼容性问题,求可行的解决方法。

解决方法

1. 修正turbopack.root配置写法

之前的路径处理可能未生效,尝试直接指向项目根目录:

import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  turbopack: {
    root: process.cwd() // 直接指向package.json所在的项目根目录
  }
};

export default nextConfig;

或用path.resolve确保路径准确性:

import type { NextConfig } from "next";
import path from 'path';

const nextConfig: NextConfig = {
  turbopack: {
    root: path.resolve(__dirname) // __dirname即为next.config.js所在的根目录
  }
};

export default nextConfig;

2. 切换Yarn的模块链接模式

Yarn 4默认启用的PnP模式可能与Turbopack解析逻辑冲突,切换到node_modules模式:
在项目根目录的.yarnrc.yml中添加或修改:

nodeLinker: node-modules

删除node_modules和.yarn/cache目录,重新执行yarn install后启动yarn dev。

3. 临时禁用Turbopack

如果不需要Turbopack的特性,可强制使用Webpack构建:

yarn dev --no-turbopack

4. 降级Yarn版本

若确认是Yarn 4.x与Next.js 16的兼容性问题,尝试降级到Yarn 3.x稳定版:

yarn set version 3.6.4
yarn install
yarn dev

内容的提问来源于stack exchange,提问作者juztcode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 04:12:34