简体   繁体   English

给定绝对模块名称时,Typescript 在本地目录中查找

[英]Typescript looking in local directory when given an absolute module name

I'm currently working on a typescript project made with the typescript template for create-react-app.我目前正在使用用于 create-react-app 的打字稿模板制作打字稿项目。

I'm trying to import react-table into my project from a component declared in src/components/Table.tsx .我正在尝试从src/components/Table.tsx声明的组件将react-table导入到我的项目中。 I have verified that react-table is installed, and that it is specifically the typed version.我已验证已安装 react-table,并且它是特定类型的版本。 npm list | grep table npm list | grep table returns ├── @types/react-table@7.7.2 . npm list | grep table返回├── @types/react-table@7.7.2

When I attempt to load the page, the error message I receive is Module not found: Can't resolve 'react-table' in/home/user/code/CompanyName/ProjectName/src/components当我尝试加载页面时,我收到的错误消息是Module not found: Can't resolve 'react-table' in/home/user/code/CompanyName/ProjectName/src/components

I may be mistaken, but this suggests to me that javascript is failing to look in node_modules for the module.我可能弄错了,但这向我表明 javascript 未能在node_modules查找模块。 What can I do to direct the compiler toward node_modules?我该怎么做才能将编译器指向 node_modules?

My tsconfig.json is in the project root and is as follows:我的tsconfig.json在项目根目录下,如下:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitReturns": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noUnusedLocals": true,
    "noEmit": true,
    "jsx": "react-jsx",
  
  },
  "include": [
    "src"
  ]
}

It's worth noting that the following import, which exists in a component declared in the src directory, works as expected and produces no error.:值得注意的是,存在于src目录中声明的组件中的以下导入按预期工作并且不会产生错误。:

import {
  BrowserRouter as Router,
  Switch,
  Route,
} from "react-router-dom";

If your npm list | grep table如果你的npm list | grep table npm list | grep table only gives @types/react-table but not react-table , there's your problem. npm list | grep table提供@types/react-table而不是react-table ,这是你的问题。

You always need the actual module, eg react-table .总是需要实际的模块,例如react-table The @types/ version merely downloads the typings for the module, assuming the module doesn't come with typings and someone took the time to add them to @types/ . @types/版本只下载模块的类型,假设模块没有@types/并且有人花时间将它们添加到@types/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM