我曾經yarn add github:username/custom-module在我的專案中添加一個自定義模塊。該模塊及其內容出現在我的node_modules檔案夾中,但我無法從中匯入任何內容。
我像 using 一樣將它匯入到我的檔案中import { Component } from "custom-module";,但 TypeScript 會回傳“找不到模塊:錯誤:無法決議 '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"
}
}
uj5u.com熱心網友回復:
正如菲爾在此評論中所建議的custom-module package.json那樣,不包含main,files或directories. 我不得不直接從"custom-module/file/path/Component".
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/427154.html
