sourcecode

형식 설명: 'react' 모듈을 찾을 수 없습니다.

copyscript 2023. 2. 28. 23:42
반응형

형식 설명: 'react' 모듈을 찾을 수 없습니다.

왜 못 찾는지 이해가 안 돼요.

$ cat tsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "target": "es6",
    "jsx": "react",
    "types": [
      "lodash",
      "react",
      "react-dom"
    ]
  }
}

$ tree node_modules/@types

node_modules/@types/
├── lodash
│   ├── README.md
│   ├── index.d.ts
│   ├── package.json
│   └── types-metadata.json
├── react
│   ├── README.md
│   ├── index.d.ts
│   ├── package.json
│   └── types-metadata.json
└── react-dom
    ├── README.md
    ├── index.d.ts
    ├── package.json
    ├── server.d.ts
    └── types-metadata.json

컴포넌트 파일로 Import.

// src/components/component.tsx

import * as React from "react";

왜 그러고 있어?

저도 같은 문제가 있었어요.@types만 설치하면 됩니다.

npm i -D @types/react

타입스크립트 2.1을 사용하지 않는 경우는, 그 타입스크립트 2.1로 업그레이드할 필요가 있습니다.사용하고 있는 @types의 2.x 버전을 사용하고 있는 것 같습니다.

여기 작업이 있습니다.tsconfig.json현재 사용하고 있는 파일:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "noResolve": false,
    "noImplicitAny": false,
    "removeComments": true,
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react"
  },
  "exclude": [
    "./node_modules/**/*"
  ]
}

당신이 겪었던 같은 문제를 해결한 지 며칠이 지났지만 여기서 중요한 건"moduleResolution": "node"그리고."allowJs": true.

eslint 규칙에서 가져온 "Cannot find module 'react"라는 타이프스크립트를 해결하려면

를 인스톨 할 필요가 있습니다.@types/react:

$ npm i -D @types/react

또는

$ yarn add -D @types/react

그리고 ".eslintrc" 파일에서extends리액트 플러그인 배열:

...
extends: [
        'plugin:react/recommended',
        ...  
],

버전 2.4와 같습니다.* 이 에러의 원인 설정 엔트리(대부분의 경우)는 다음과 같습니다.

"compilerOptions": {
          ...
          "moduleResolution": "node" <---------- this entry
          ...
}

같은 문제가 있었어. 경우 데노 확장을 비활성화하면 문제가 해결되었습니다.왠지 수입품들을 엉망으로 만들고 있었다.

를 인스톨 하고 있는 경우@types/react아직 동작하지 않습니다.최신 버전을 사용하는 것을 추천합니다.Typescript그런 다음 IDE/에디터를 닫고 삭제합니다.node_modules폴더 및 실행npm install또는yarn install다시 한 번 확인해 주세요.이제 작동해야 합니다.

이 코멘트를 쓸 때 cmd에 typescript가 설치되어 있지 않다는 오류가 표시되었습니다.

npm install typescript

그리고 그것은 효과가 있었다.

다른 사람이 시작한 프로젝트에 참여하는 경우, 레포는 이미 적절한 정보를 가지고 있을 수 있습니다.devDependencies(예:@types/react)의 경우package.json. 다음과 같이 시작하기만 하면 됩니다.

npm install

node_modules 가 존재하는지 확인합니다.그래도 문제가 해결되지 않으면 삭제하고 npm install을 실행하여 다시 설치합니다.이것으로 나는 문제를 해결했다.

타입에 대해서도 같은 문제를 해결했습니다.

npm i -D @types/react or yarn add @types/react

tsconfig.json "jsx" 파라미터에 대한 자세한 내용은 실 시작 시 자동으로 "react"에서 "react-jsx"로 변경되었습니다.도움이 될 수 있는 경우는, 다음과 같이 기능하는 tsconfig 입니다.

{
  "compilerOptions": {
    "jsx": "react",
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "allowJs": true,
    "removeComments": true,
    "sourceMap": true,
    "noResolve": false,
    "noImplicitAny": false
  },
  "include": [
    "src",
    "./node_modules/**/*"
  ]
}

npm i @types/react또는yarn add @types/react문제가 해결됩니다.누락된 반응 유형으로 인해 오류가 발생했습니다.

삭제하다node_modules패키지 및yarn_lock파일(존재하는 경우)그럼 한 번 해봐yarn installVS 코드를 재시작합니다.나한테는 완벽하게 먹혔어

이러한 답변이 모두 작동하지 않는 경우 개발자가 Windows에서 시도해 보는 것이 좋습니다.장치에 서로 다른 권한을 가진 여러 사용자가 있을 수 있으며 대부분 관리자 권한이 아닌 사용자를 사용해야 합니다.이 경우 관리자만 액세스할 수 있는 일부 폴더(프로젝트로 가져오는 모듈)는 사용자가 액세스할 수 없습니다.솔루션:관리자 권한으로 VS 코드를 실행합니다.

i am writing for `react-native` assuming you have used `babel-plugin-module-resolver` and all your code is inside src folder <br />
These are the files required to be configured in-order to work on absolute imports in typescript<br />

1. babel.config.js add the module-resolver plugin as below <br/>
`module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      'module-resolver',
      {
        root: ['./'],
        alias: {
          src: './src',
        },
      },
    ],
  ],
};`

2. update ts config with below code <br />
 `"baseUrl": ".",
    "paths": {
      "src": ["src/*"],
      "tests": ["tests/*"]
    },`

3. then all your import will starts with src for example. `import funkyImage from src/images/funkyImage.png`
const React = require("react");

이것으로 문제가 해결되었습니다.

언급URL : https://stackoverflow.com/questions/40899868/typescript-cannot-find-module-react

반응형