You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义npm包leon-theme导入遇ts(2307)错误:无法找到模块或类型声明

问题:导入自定义npm包时出现TypeScript模块未找到错误

我开发了一个自定义npm包,之后创建了一个基础的create-react-app项目,尝试在项目的src/index.tsx文件中导入该包。添加导入语句import { LeonTheme } from "leon-theme";后,鼠标悬停在该行时出现以下错误:

Cannot find module 'leon-theme' or its corresponding type declarations.ts(2307)

自定义npm包的tsconfig配置

{  
  "compilerOptions": {    
    "module": "ES6",    
    "target": "es2016",    
    "lib": ["ES2020", "DOM"],    
    "jsx": "react",    
    "allowJs": true,    
    "declaration": true,    
    "declarationDir": "types",    
    "sourceMap": true,    
    "allowSyntheticDefaultImports": true,    
    "noEmit": false,    
    "outDir": "./dist",    
    "moduleResolution": "node",    
    "noImplicitAny": false  
  },  
  "include": ["src/**/*.ts"]
}

自定义npm包的package.json配置

{  
  "name": "leon-theme",  
  "version": "0.0.15",  
  "module": "dist/lib/es6/index.js",  
  "description": "A simple React style & component library",  
  "homepage": "https://github.com/leongaban/leon-theme",  
  "bugs": {    
    "url": "https://github.com/leongaban/leon-theme/issues"  
  },  
  "repository": {    
    "type": "git",    
    "url": "git+https://github.com/leongaban/leon-theme.git"  
  },  
  "keywords": [    
    "javascript",    
    "theme",    
    "buttons",    
    "leon"  
  ],  
  "scripts": {    
    "build": "rm -rf dist/lib && tsc --build"  
  },  
  "author": "Leon Gaban",  
  "license": "ISC",  
  "dependencies": {    
    "react": "^18.2.0"  
  },  
  "devDependencies": {    
    "@types/react": "^18.2.22",    
    "jest-environment-jsdom": "^29.7.0",    
    "tslib": "^2.6.2",    
    "typescript": "^5.2.2"  
  },  
  "files": [    
    "dist",    
    "types"  
  ],  
  "types": "dist/index.d.ts"
}

测试项目node_modules中leon-theme的目录结构

目录结构截图

内容的提问来源于stack exchange,提问作者Leon Gaban

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 08:27:06