"Cannot find module 'custom-module' or its corresponding type declarations" 用于从 GitHub 添加的模块
"Cannot find module 'custom-module' or its corresponding type declarations" for module added from GitHub
我使用 yarn add github:username/custom-module
将自定义模块添加到我的项目中。该模块及其内容出现在我的 node_modules
文件夹中,但我无法从中导入任何内容。
我将它导入到我的文件中,就像使用 import { Component } from "custom-module";
一样,但是 TypeScript returns,“找不到模块:错误:无法在 'filepath...' 中解析 'custom-module'” .
我的项目package.json
:
{
"name": "project-name",
"version": "0.1.0",
"private": true,
"dependencies": {
"@ant-design/icons": "^4.7.0",
"@dnd-kit/core": "^4.0.2",
"@dnd-kit/sortable": "^5.1.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/express": "^4.17.13",
"@types/lodash": "^4.14.176",
"@types/react": "^17.0.38",
"@types/react-dom": "^17.0.11",
"@types/react-router-dom": "^5.3.2",
"@types/styled-components": "^5.1.15",
"@types/ws": "^8.2.2",
"eslint-plugin-react-hooks": "^4.3.0",
"esm": "^3.2.25",
"express": "^4.17.2",
"mobx": "^6.3.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-quick-reactions": "github:username/custom-module",
"react-router-dom": "^5.2.0",
"react-scripts": "5.0.0",
"styled-components": "^5.2.3",
"typescript": "^4.4.4",
"web-vitals": "^1.0.1",
"ws": "^8.4.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"serve": "node -r esm src/backend/server.ts"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:8080",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
"eslint-plugin-react": "^7.28.0"
}
}
和来自模块的 package.json
:
{
"name": "custom-module",
"version": "0.1.0",
"private": false,
"description": "A description.",
"license": "MIT",
"keywords": [
"react",
"react-component",
],
"homepage": "https://github.com/username/custom-module#readme",
"bugs": {
"url": "https://github.com/username/custom-module/issues"
},
"repository": "github:username/custom-module",
"peerDependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/react": "^17.0.39",
"typescript": "^4.5.5"
}
}
正如 Phil 在 中建议的那样,custom-module
package.json
不包含 main
、files
或 directories
。我不得不直接从 "custom-module/file/path/Component"
.
导入
我使用 yarn add github:username/custom-module
将自定义模块添加到我的项目中。该模块及其内容出现在我的 node_modules
文件夹中,但我无法从中导入任何内容。
我将它导入到我的文件中,就像使用 import { Component } from "custom-module";
一样,但是 TypeScript returns,“找不到模块:错误:无法在 'filepath...' 中解析 'custom-module'” .
我的项目package.json
:
{
"name": "project-name",
"version": "0.1.0",
"private": true,
"dependencies": {
"@ant-design/icons": "^4.7.0",
"@dnd-kit/core": "^4.0.2",
"@dnd-kit/sortable": "^5.1.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/express": "^4.17.13",
"@types/lodash": "^4.14.176",
"@types/react": "^17.0.38",
"@types/react-dom": "^17.0.11",
"@types/react-router-dom": "^5.3.2",
"@types/styled-components": "^5.1.15",
"@types/ws": "^8.2.2",
"eslint-plugin-react-hooks": "^4.3.0",
"esm": "^3.2.25",
"express": "^4.17.2",
"mobx": "^6.3.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-quick-reactions": "github:username/custom-module",
"react-router-dom": "^5.2.0",
"react-scripts": "5.0.0",
"styled-components": "^5.2.3",
"typescript": "^4.4.4",
"web-vitals": "^1.0.1",
"ws": "^8.4.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"serve": "node -r esm src/backend/server.ts"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:8080",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.9.1",
"@typescript-eslint/parser": "^5.9.1",
"eslint-plugin-react": "^7.28.0"
}
}
和来自模块的 package.json
:
{
"name": "custom-module",
"version": "0.1.0",
"private": false,
"description": "A description.",
"license": "MIT",
"keywords": [
"react",
"react-component",
],
"homepage": "https://github.com/username/custom-module#readme",
"bugs": {
"url": "https://github.com/username/custom-module/issues"
},
"repository": "github:username/custom-module",
"peerDependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/react": "^17.0.39",
"typescript": "^4.5.5"
}
}
正如 Phil 在 custom-module
package.json
不包含 main
、files
或 directories
。我不得不直接从 "custom-module/file/path/Component"
.