【问题标题】:`Cannot find module` from a new owned TypeScript module来自新拥有的 TypeScript 模块的“找不到模块”
【发布时间】:2020-03-01 13:47:04
【问题描述】:

我刚刚在 NPM 注册表puppeteer-jquery 上发布了一个新的 Typescript 项目。代码工作正常。 但是当我尝试在其他项目中使用它时:

npm install puppeteer-jquery

并尝试使用以下命令导入它:

import { PageEx, BrowserEx } from 'puppeteer-jquery';

我得到错误: Cannot find module 'puppeteer-jquery'.ts(2307)

在本地,我只能通过安装本地源副本并导入来使用它:

import { PageEx, BrowserEx } from './node_modules/puppeteer-jquery/src/setup';

来自 pupeteer-jquery 的 package.json

{
  "name": "puppeteer-jquery",
  ...
  "main": "lib/index.js", /* also tried with "./lib/index.js" value */
  "typings": "lib/index.d.ts", /* tried with "types" and "typings" key */
  "devDependencies": {
    ...
    "typescript": "^3.6.4"
  },
  "files": ["lib", "data/*.js"] /* data contains jquery.js as a ressource */
}

来自 puppeteer-jquery 的 tsconfig.json

{
  "compilerOptions": {
    "target": "ES2017",
    "module": "commonjs",
    "declaration": true,
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "outDir": "lib",
    "rootDirs": [
      "src"
    ]
  },
  "include": [ "src" ], /* added after the first post */
  "exclude": ["node_modules", "lib"]
}

npm 发布 --dry-run:

npm notice ????  puppeteer-jquery@0.1.5
npm notice === Tarball Contents === 
npm notice 1.1kB   LICENSE              
npm notice 410B    lib/index.js         
npm notice 280.4kB data/jquery-3.4.1.js 
npm notice 5.7kB   lib/jQueryPlugin.js  
npm notice 77B     lib/PJQuery.js       
npm notice 1.0kB   lib/setup.js         
npm notice 1.2kB   package.json         
npm notice 2.0kB   README.md            
npm notice 242B    lib/index.d.ts       
npm notice 411B    lib/jQueryPlugin.d.ts
npm notice 2.2kB   lib/PJQuery.d.ts     
npm notice 518B    lib/setup.d.ts       
npm notice === Tarball Details === 

这个问题看起来像stackoverflow 54695891,但不一样。

PS:

  • 这不是我的第一个 Typescript 项目
  • 这个项目没有依赖关系
  • 我也尝试将 TS 源和源映射包含到已发布的包中,但没有更多成功。

【问题讨论】:

  • 经过大量测试后,我使用此依赖项重建了一个新项目,它的工作原理。
  • 就我而言,问题是我在 tsconfig.json 文件中缺少 "typeRoots": [ "./node_modules/@types" ]

标签: node.js typescript npm typescript-typings


【解决方案1】:

我下载了该模块,但您的构建或发布设置似乎存在问题 - typings 指向的文件 lib/index.d.ts 在已发布版本中不存在。

├── LICENSE
├── README.md
├── data
│   └── jquery-3.4.1.js
├── lib
│   ├── src
│   │   ├── PJQuery.d.ts
│   │   ├── PJQuery.js
│   │   ├── index.d.ts
│   │   ├── index.js
│   │   ├── jQueryPlugin.d.ts
│   │   ├── jQueryPlugin.js
│   │   ├── setup.d.ts
│   │   └── setup.js
│   └── test
│       ├── basic.spec.d.ts
│       └── basic.spec.js
└── package.json

【讨论】:

  • 奇怪,但你是对的......让我解决这个问题。看看它是否有效。
  • 已修复,但问题仍然与 puppeteer-jquery@0.1.5 相同
  • 您帖子中的 tsconfig 是来自 puppeteer-jquery 还是消费包?
  • 来自 pupeteer-jquery,我在问题中添加了到 github 的直接链接。
猜你喜欢
  • 2019-07-08
  • 2017-03-02
  • 2019-08-16
  • 2016-09-20
  • 1970-01-01
  • 1970-01-01
  • 2017-03-21
  • 2016-10-24
  • 2018-11-19
相关资源
最近更新 更多