【发布时间】:2020-10-24 16:42:21
【问题描述】:
我正在尝试使用 NodeJS 和使用 TypeScript 的 React 构建一个基于工作区的应用程序。我确实在根目录上配置了package.json,并使用工作区模型来创建服务器和客户端应用程序。不幸的是,我遇到了 VsCode 的问题,它试图将 tsconfig.json 文件解析到根目录而不是项目文件夹中。
这是我的结构:
project-root
├── .vscode
│ └── settings.json
├── client
│ ├── .eslintrc.js
│ ├── package.json
│ ├── src
│ │ └── App.tsx
│ ├── tsconfig.json
│ └── yarn.lock
├── package.json
└── server
└── ...
这里是上述文件的内容。
.vscode/settings.json:
{
"javascript.validate.enable": false,
"editor.tabSize": 4,
"eslint.enable": true,
"eslint.packageManager": "yarn",
"eslint.codeActionsOnSave.mode": "all",
"eslint.workingDirectories": [
{ "directory": "../server", "changeProcessCWD": true },
{ "directory": "../client", "changeProcessCWD": true }
],
}
project-root/package.json:
{
"name": "root-project",
"version": "0.0.1",
"private": true,
"main": "index.js",
"author": "Rhuan Karlus Silva",
"license": "MIT",
"workspaces": {
"packages": [
"server",
"client"
]
}
}
project-root/client/.eslintrc.js:
module.exports = {
env: {
browser: true,
es6: true,
jest: true,
},
extends: [
'react-app',
'airbnb',
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: 'module',
project: 'tsconfig.json',
},
plugins: ['react', 'import', 'jsx-a11y'],
rules: {
'react/jsx-filename-extension': [
'error',
{
extensions: ['.tsx'],
},
],
"import/prefer-default-export": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-member-accessibility": "off",
"indent": ["error", "tab"],
"no-tabs": ["error", { allowIndentationTabs: true }],
"react/jsx-indent": [2, 'tab', {
checkAttributes: true,
indentLogicalExpressions: true
}]
},
settings: {
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx'],
},
'import/resolver': {
typescript: {},
},
},
};
project-root/client/package.json:
{
"name": "project-client",
"version": "0.1.0",
"private": true,
"dependencies": {
"@emotion/core": "^10.0.28",
"@emotion/styled": "^10.0.27",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/node": "^12.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"emotion-theming": "^10.0.27",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"typescript": "~3.7.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "../node_modules/.bin/eslint"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0",
"eslint-config-airbnb": "^18.1.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-react": "^7.20.0",
"prettier": "^2.0.5"
}
}
project-root/client/tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src"
]
}
好吧,既然我暴露了这个问题,我看到了两个问题。他们是:
- 找不到模块“react”。
- 找不到tsconfig.json
现在请注意,第二个 (tsconfig.json) 错误显示的是 project-root 目录的路径,而不是应该指向的 project-root/client 目录。
那么,我是不是搞错了什么?我该如何解决这个问题并让我的工作区正常工作?
【问题讨论】:
标签: node.js reactjs typescript npm eslint