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

使用Yarn Berry创建NestJS项目遇导入及Buffer类型错误求助

使用Yarn Berry创建NestJS项目启动失败的问题

操作步骤

  • 执行 nest new yarnProject,选择Yarn作为包管理器,提示创建成功
  • 执行 yarn install 和 yarn start 启动项目

错误情况

  1. Node.js v20.17.0 下的Buffer类型错误
node:buffer:587
      throw new ERR_INVALID_ARG_TYPE(
            ^

TypeError [ERR_INVALID_ARG_TYPE]: The "list[2]" argument must be an instance of Buffer or Uint8Array. Received type string ('app.controller.d.ts')
    at Function.concat (node:buffer:587:13)
    at node:internal/fs/rimraf:144:32
    at Array.forEach (<anonymous>)
    at node:internal/fs/rimraf:143:5
    at /Users/shin/TodaysProject/yarn-project/.pnp.cjs:12946:13 {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v20.17.0
  1. 切换到Node.js v18.20.4 后模块找不到错误
src/app.controller.ts:1:33 - error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.

1 import { Controller, Get } from '@nestjs/common';
                                  ~~~~~~~~~~~~~~~~
src/app.module.ts:1:24 - error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.

1 import { Module } from '@nestjs/common';
                         ~~~~~~~~~~~~~~~~
src/app.service.ts:1:28 - error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.

1 import { Injectable } from '@nestjs/common';
                             ~~~~~~~~~~~~~~~~
src/main.ts:1:29 - error TS2307: Cannot find module '@nestjs/core' or its corresponding type declarations.

1 import { NestFactory } from '@nestjs/core';
                              ~~~~~~~~~~~~~~

Found 4 error(s).

解决方案

方案1:升级Yarn Berry并配置TypeScript支持PnP

  • 进入项目根目录,执行命令升级到最新稳定版Yarn Berry:
    yarn set version stable
    
  • 生成TypeScript SDK(适配编辑器识别模块):
    yarn dlx @yarnpkg/sdks vscode
    
  • 打开tsconfig.json,确保compilerOptions中包含Yarn PnP插件配置(若SDK未自动添加):
    "compilerOptions": {
      // ...其他原有配置
      "plugins": [{ "name": "@yarnpkg/esbuild-plugin-pnp" }]
    }
    
  • 重新安装依赖并启动:
    yarn install
    yarn start
    

方案2:改用nodeModules linker(放弃PnP,使用传统node_modules结构)

  • 在项目根目录创建.yarnrc.yml文件,添加配置:
    nodeLinker: node-modules
    
  • 删除项目中的.pnp.cjs和.pnp.loader.mjs文件
  • 重新安装依赖并启动:
    yarn install
    yarn start
    

额外建议

  • 确保使用最新版Nest CLI,避免兼容性问题:
    npm install -g @nestjs/cli@latest
    

内容的提问来源于stack exchange,提问作者신동혁

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 12:45:12