使用纱线工作区时,tsc 在 apollo-server 中找不到 express 的声明文件
Posted
技术标签:
【中文标题】使用纱线工作区时,tsc 在 apollo-server 中找不到 express 的声明文件【英文标题】:tsc cannot find declaration files of express in apollo-server when using yarn workspaces 【发布时间】:2021-11-07 19:33:03 【问题描述】:我正在尝试在其中一个包中构建一个带有 yarn 工作区(版本 3.0.2)、typescript(4.4.3) 和 apollo-server (3.3.0) 的 monorepo。运行yarn workspace graphql tsc --build
时出现以下错误:
Could not find a declaration file for module 'express'. '...../.yarn/__virtual__/express-virtual-1169aebee1/0/cache/express-npm-4.17.1-6815ee6bf9-d964e9e17a.zip/node_modules/express/index.js' implicitly has an 'any' type.
If the 'express' package actually exposes this module, consider sending a pull request to amend 'https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/express'
2 import express from 'express';
~~~~~~~~~
然后我尝试安装@types/express
,但错误仍然相同。我该如何解决这个问题?
我的项目结构如下:
├── package.json
├── packages
│ └── graphql
│ ├── index.ts
│ ├── package.json
│ └── tsconfig.json
└── yarn.lock
我尝试编译的index.ts
文件:
import gql from 'apollo-server';
// A schema is a collection of type definitions (hence "typeDefs")
// that together define the "shape" of queries that are executed against
// your data.
const typeDefs = gql`
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
# This "Book" type defines the queryable fields for every book in our data source.
type Book
title: String
author: String
# The "Query" type is special: it lists all of the available queries that
# clients can execute, along with the return type for each. In this
# case, the "books" query returns an array of zero or more Books (defined above).
type Query
books: [Book]
`;
我的tsconfig.json
:
"compilerOptions":
"target": "ES2020",
"module": "commonjs",
"outDir": "./dist",
"strict": true,
"incremental": true,
"noUnusedLocals": true,
"esModuleInterop": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": "./src",
"skipLibCheck": false
,
"include": ["**/*.ts"],
"compileOnSave": true
我的根package.json
:
"name": "test-monorepo",
"packageManager": "yarn@3.0.2",
"private": true,
"workspaces": [
"packages/*"
]
我的package.json
graphql 包:
"name": "graphql",
"packageManager": "yarn@3.0.2",
"dependencies":
"@types/express": "^4.17.13",
"@types/node": "^16.9.1",
"apollo-server": "^3.3.0",
"graphql": "^15.5.3",
"typescript": "^4.4.3"
【问题讨论】:
【参考方案1】:在您的tsconfig.json
文件中,将skipLibCheck
更改为true
。这将跳过声明文件的类型检查。
【讨论】:
但是我不想仅仅为了这种情况而跳过所有库的类型检查。我想保持类型系统的准确性。【参考方案2】:包apollo-server
不包括@types/express
作为peerDependency
。解决方法是在.yarnrc.yaml
中告诉yarn包apollo-server
需要@types/express
:
packageExtensions:
apollo-server@*:
dependencies:
"@types/express": "*"
【讨论】:
以上是关于使用纱线工作区时,tsc 在 apollo-server 中找不到 express 的声明文件的主要内容,如果未能解决你的问题,请参考以下文章
Firebase 推送通知在纱线手表中工作,但在纱线构建中不工作