【问题标题】:"Cannot find module 'react'" and "tsconfig.json not found"“找不到模块 'react'”和“找不到 tsconfig.json”
【发布时间】: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"
  ]
}

好吧,既然我暴露了这个问题,我看到了两个问题。他们是:

  1. 找不到模块“react”。

  1. 找不到tsconfig.json

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

那么,我是不是搞错了什么?我该如何解决这个问题并让我的工作区正常工作?

【问题讨论】:

    标签: node.js reactjs typescript npm eslint


    【解决方案1】:

    好的,我找到了第二个问题的解决方案,感谢@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 问题,我不知道为什么它会起作用,因为它对我没有确切的含义,但如果它起作用,它就会起作用。我将project-root/client/.eslintrc.js 中的parserOption 更改为:

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

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

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

    【讨论】:

      【解决方案2】:

      我只是在类似的设置下遇到了同样的问题,我解决的方法是在根目录创建一个 .eslintrc.js 并添加:

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

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

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

      【讨论】:

      • 非常感谢,它确实解决了问题 1:找不到模块 'react'。现在这可以正常工作了,但是 tsconfig.json 问题并没有解决。
      • 你能检查一下客户端是否会在终端中运行而没有任何问题吗? (即使 vscode 显示错误)也可能是由于 `parserOptions: { project: 'tsconfig.json', } ` in client/.eslintrc.js - 你可以尝试删除上面指定的 project 属性。如果上述解决方案不起作用,可能是您的项目中的 vscode 配置或 .vscode 文件夹,您可以尝试删除项目中的所有 .vscode 文件夹。让我知道这是否有帮助。
      • 好的,我在这里解决了这个问题,我会在今晚(BRT 20:00 之后)发布答案,让其他人有机会解决这两个问题。如果没有其他人可以找到解决方案,我将在此处发布我找到的解决方案。 :) 认为这是让其他开发人员有机会为他们的努力赢得一些好成绩的好方法 :)
      猜你喜欢
      • 2022-08-11
      • 2021-12-25
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      相关资源
      最近更新 更多