【发布时间】: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