找不到模块"反应引导程序"与 commonjs 以外的模块



我将 React 与Typescript 一起使用,但不能将 React Bootstrap 与commonjs以外的模块一起使用。

我首先安装react-bootstrap软件包:

$ npm i react-bootstrap

然后编写代码来使用它,例如,

// src/index.tsx
import { Button } from 'react-bootstrap';

但是编译它npx tsc得到一个错误Cannot find module 'react-bootstrap' or its corresponding type declarations.当我用谷歌搜索这个问题时,我发现react-bootstrap有自己的类型(所以我不需要@types/react-bootstrap(。

如果我在tsconfig.json中将module设置为commonjs,那么我可以正确编译它。

但是,为什么我不能使用react-bootstrap?有没有其他方法可以将 React Bootstrap 与 Typescript(以及commonjs以外的模块(一起使用?

这里的最小示例:

package.json

{
"name": "foo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"type": "npx tsc --noEmit",
"test": "echo "Error: no test specified" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^3.9.5"
},
"dependencies": {
"react-bootstrap": "^1.0.1"
}
}

tsconfig.json

{
"compilerOptions": {
"target": "es2015",
"module": "es2015",
"skipLibCheck": true
},
"include": [
"src/**/*"
]
}

src/index.tsx

import { Button } from 'react-bootstrap';

然后运行

$ npm run type
> foo@1.0.0 type /home/bar/src/foo
> npx tsc --noEmit
src/index.tsx(1,24): error TS2307: Cannot find module 'react-bootstrap' or its corresponding type declarations.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! foo@1.0.0 type: `npx tsc --noEmit`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the foo@1.0.0 type script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/bar/.npm/_logs/2020-07-04T12_11_17_351Z-debug.log
  1. 使用$ npm cache clean --force
  2. 通过$ rm -rf node_modules package-lock.json删除node_modules或手动删除
  3. 安装 npm 模块npm install

这对我有用。希望它也适合你。

最新更新