Reputation: 2266
I'm trying to build a workspace based application with NodeJS and React using TypeScript. I did configured a package.json
on the root directory and used the workspaces model to create both, server and client applications. Unfortunately I got stuck on a problem with VsCode that is trying to resolve the tsconfig.json
file to the root directory and not inside the project folder.
This is my structure:
project-root
├── .vscode
│ └── settings.json
├── client
│ ├── .eslintrc.js
│ ├── package.json
│ ├── src
│ │ └── App.tsx
│ ├── tsconfig.json
│ └── yarn.lock
├── package.json
└── server
└── ...
And here are the content of the above files.
.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"
]
}
Alright now that I exposed the problem, I'm seeing two issues. Here are they:
Now please note that the second (tsconfig.json) error is showing the path to the project-root
directory and not to the project-root/client
directory as it should point.
So, am I figuring out something wrong? How can I solve this and get my workspaces working?
Upvotes: 0
Views: 17177
Reputation: 2266
Alright people, I've found a solution for the second problem and, thanks to @Daniel, I could solve the first problem too. And here's the complete answer:
First of all I did what @Daniel told me on his answer, I removed this lines from my .vscode/settings.json
file:
"eslint.workingDirectories": [
{ "directory": "../server", "changeProcessCWD": true },
{ "directory": "../client", "changeProcessCWD": true }
],
And then, I created the file project-root/.eslintrc.js
with this content:
module.exports = {
"eslint.workingDirectories": [
{ directory: "./client/", changeProcessCWD: true },
{ directory: "./server/", changeProcessCWD: true },
],
};
This way the Cannot find module 'react'
problem gone.
Now to the tsconfig.json not found
problem, I don't know exactly why this worked since it have no precise meaning to me, but if it works, it works. I changed the parserOption
inside my project-root/client/.eslintrc.js
to this:
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: 'module',
project: './client/tsconfig.json',
},
Given that the changeProcessCWD
flag is set, I didn't thought I would need to change this. But it worked. So, that's it.
Thanks everyone who tried to help.
Upvotes: 3
Reputation: 11
I just had the same problem with a similar setup, the way I solved was to create a .eslintrc.js at the root directory and added this:
module.exports = {
"eslint.workingDirectories": [
{ directory: "client/", changeProcessCWD: true },
{ directory: "server/", changeProcessCWD: true },
],
};
You should also remove the eslint.workingDirectories from .vscode/settings.json, or update it with the above code.
Hopefully this will do, you could also try to remove the workspaces from project-root/package.json if the above steps won't solve your problem.
Upvotes: 1