TypeScript编译未排除node_modules,Yarn Workspace构建报错求助
node_modules in Yarn Workspace The Problem
I'm working with a Yarn Workspace setup, and every time I run my build command (rimraf ./dist && tsc), I hit TypeScript syntax errors from the graphql-toolkit package in node_modules. Even though I've set exclude: ["node_modules"] and skipLibCheck: true in all my tsconfig.json files, the errors keep popping up.
Error Output
$ rimraf ./dist && tsc ../../node_modules/graphql-toolkit/dist/esnext/utils/validate-documents.d.ts:7:115 - error TS1005: ',' expected. 7 export declare const validateGraphQlDocuments: (schema: GraphQLSchema, documentFiles: DocumentFile[]) => readonly LoadDocumentError[]; ~~~~~~~~~~~~~~~~~ ../../node_modules/graphql-toolkit/dist/esnext/utils/validate-documents.d.ts:7:132 - error TS1005: ',' expected. 7 export declare const validateGraphQlDocuments: (schema: GraphQLSchema, documentFiles: DocumentFile[]) => readonly LoadDocumentError[]; error Command failed with exit code 2. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. error Command failed with exit code 2. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
My tsconfig.json
{ "compilerOptions": { "target": "es6", "module": "commonjs", "lib": ["dom", "es6", "es2017", "esnext", "esnext.asynciterable"], "sourceMap": true, "outDir": "./dist", "moduleResolution": "node", "removeComments": true, "noImplicitAny": true, "strictNullChecks": true, "strictFunctionTypes": true, "noImplicitThis": true, "noUnusedLocals": true, "noUnusedParameters": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, "allowSyntheticDefaultImports": false, "emitDecoratorMetadata": true, "experimentalDecorators": true, "skipLibCheck": true }, "exclude": ["node_modules"], "include": ["./src/**/*.tsx", "./src/**/*.ts"] }
I need to get rid of these errors and compile cleanly, ensuring node_modules .d.ts files are properly excluded.
Why This Happens
Let's break down the likely issues:
- Outdated TypeScript Version: The
readonly LoadDocumentError[]syntax in the error is a feature introduced in TypeScript 3.4. If your project is using an older TS version, it won't recognize this syntax, leading to the comma error. - Yarn Workspace Dependency Conflicts: Yarn's dependency hoisting might have pulled in multiple versions of TypeScript (or a
graphql-toolkitversion that expects a newer TS version) across your workspace packages. skipLibCheckNot Being Respected: In a workspace setup, it's possible that child packages aren't properly inheriting theskipLibChecksetting, or another configuration is overriding it.
Step-by-Step Fixes
1. Upgrade TypeScript to a Compatible Version
First, check your current TS version:
yarn tsc --version
If it's below 3.4.x, upgrade to the latest stable version (install at the workspace root to ensure consistency across all packages):
yarn add typescript@latest -D -W
2. Enforce skipLibCheck Across the Workspace
Create a base TypeScript config at your workspace root (tsconfig.base.json) to share settings across all packages:
{ "compilerOptions": { "skipLibCheck": true, // Add other shared compiler options here }, "exclude": ["node_modules"] }
Then update each child package's tsconfig.json to extend this base config:
{ "extends": "../tsconfig.base.json", "compilerOptions": { // Add package-specific options here (like outDir, target, etc.) }, "include": ["./src/**/*.tsx", "./src/**/*.ts"] }
This ensures the skipLibCheck setting is applied consistently everywhere in your workspace.
3. Clean Cache and Reinstall Dependencies
Sometimes cached or duplicated dependencies cause weird issues. Let's start fresh:
# Clear Yarn's cache yarn cache clean # Remove all node_modules and lock files rimraf node_modules */node_modules yarn.lock # Reinstall dependencies yarn install
4. Exclude the Problematic Package's Types Directly
If the above steps don't work, you can explicitly exclude the graphql-toolkit type files in your tsconfig.json:
{ "compilerOptions": { // Your existing options }, "exclude": [ "node_modules", "node_modules/graphql-toolkit/dist/esnext/**/*.d.ts" ] }
内容的提问来源于stack exchange,提问作者Sathish Shajahan

