“找不到模块'反应'”和“找不到tsconfig.json”

时间:2020-07-04 00:48:58

标签: node.js reactjs typescript npm eslint

我正在尝试使用TypeScript使用NodeJS和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"
  ]
}

现在,我暴露了这个问题,我看到了两个问题。他们是这些人:

  1. 找不到模块“反应”。

Cannot find module react

  1. 找不到tsconfig.json

tsconfig.json not found

现在请注意,第二个(tsconfig.json)错误显示的是project-root目录的路径,而不是应该指向的project-root/client目录的路径。

那么,我要找出错误的地方吗?如何解决这个问题并使我的工作区正常工作?

2 个答案:

答案 0 :(得分:2)

好的,我已经找到了第二个问题的解决方案,而且感谢@Daniel,我也可以解决第一个问题。这是完整的答案:

首先,我执行了@Daniel在他的回答中告诉我的内容,我从我的.vscode/settings.json文件中删除了此行:

"eslint.workingDirectories": [
    { "directory": "../server", "changeProcessCWD": true },
    { "directory": "../client", "changeProcessCWD": true }
],

然后,我创建了具有以下内容的文件project-root/.eslintrc.js

module.exports = {
    "eslint.workingDirectories": [
      { directory: "./client/", changeProcessCWD: true },
      { directory: "./server/", changeProcessCWD: true },
    ],
};

这样Cannot find module 'react'问题就解决了。

由于tsconfig.json not found的问题,我不知道为什么会这样,因为它对我没有确切的含义,但是如果它起作用,它就会起作用。我将parserOption内的project-root/client/.eslintrc.js更改为此:

parserOptions: {
    ecmaFeatures: {
        jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
    project: './client/tsconfig.json',
},

鉴于设置了changeProcessCWD标志,我不认为我需要更改它。但这有效。就是这样。

感谢所有尝试提供帮助的人。

答案 1 :(得分:1)

我只是在使用类似设置时遇到了同样的问题,我解决的方法是在根目录中创建 .eslintrc.js 并添加以下内容:

Country GDP/capita HDI
USA     67426      0.92
UK      46827      0.92
France  47223      0.89
Germany 52559      0.94

您还应该从 .vscode / settings.json 中删除 eslint.workingDirectories ,或使用上述代码进行更新。

希望这样做可以,如果上述步骤不能解决您的问题,您也可以尝试从 project-root / package.json 中删除工作区。 / p>