如何在依赖项中使用 Puppeteer?

huangapple go评论81阅读模式
英文:

How to use puppeteer inside dependencies?

问题

I am creating a dependancy to generate PDF on nestjs, to do it I am using puppeteer.
我正在创建一个依赖项来在nestjs上生成PDF,我正在使用puppeteer来实现。

When I try to use puppeteer inside the dependancy I get an error:
当我尝试在依赖项中使用puppeteer时,我遇到了错误:

Error: Run npm install to download the correct Chromium revision (1056772).
错误:运行 npm install 以下载正确的Chromium修订版(1056772)。

But when I do it from the project and not the dependancy there is no error. It only happen when I use puppeteer from the dependancy, but it's launched from the same project.
但是,当我在项目中而不是依赖项中执行时,没有错误。只有在我从依赖项中使用puppeteer时才会发生错误,但它是从同一个项目中启动的。

First I generated the PDF inside a nestjs project and it worked fine.
首先,我在nestjs项目中生成了PDF,一切正常。

Then I moved the code to my library inside a service but then there is the error.
然后,我将代码移到我的库中的一个服务中,但随后出现了错误。

I tried to run the following command to install chromium
我尝试运行以下命令来安装chromium

node node_modules/puppeteer/install.js

But nothing change and here is the result of the command:
但是什么都没有改变,以下是命令的结果:

Chromium is already in C:\Users\Greg.cache\puppeteer\chrome\win64-1069273; skipping download.
Chromium已经存在于C:\Users\Greg.cache\puppeteer\chrome\win64-1069273中;跳过下载。

I also tried to delete the node modules and reinstall it but no change.
我还尝试删除node模块并重新安装它,但没有改变。

The package.json of the dependancy:
依赖项的package.json:

{
  "name": "@gboutte/nestjs-pdf",
  "version": "0.0.3",
  "description": "This package provide a service to render PDF from html string or from handlebars tempaltes for nestjs.",
  "keywords": [
    "nestjs",
    "handlebars",
    "hbs",
    "templates",
    "pdf"
  ],
  "homepage": "https://github.com/gboutte/nestjs-pdf#readme",
  "bugs": {
    "url": "https://github.com/gboutte/nestjs-pdf/issues",
    "email": "gboutte@protonmail.com"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/gboutte/nestjs-pdf"
  },
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "rimraf dist && tsc",
    "prepublish": "npm run build"
  },
  "author": "Grégory Boutte <gboutte@protonmail.com>",
  "license": "MIT",
  "dependencies": {},
  "devDependencies": {
    "@nestjs/common": "^9.1.6",
    "@gboutte/nestjs-hbs": "^0.0.3",
    "@types/node": "^18.11.5",
    "puppeteer": "^19.1.2",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.5.7",
    "typescript": "^4.8.4"
  },
  "peerDependencies": {
    "@nestjs/common": "^9.1.6",
    "@gboutte/nestjs-hbs": "^0.0.3",
    "@types/node": "^18.11.5",
    "puppeteer": "^19.1.2",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.5.7",
    "typescript": "^4.8.4"
  }
}

And the package.json of the main project:
主项目的package.json:

{
  "name": "nestjs-hbs-demo",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "main": "dist/index.js",
  "readmeFilename": "README.md",
  "files": [
    "dist/**/*",
    "*.md"
  ],
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@gboutte/nestjs-hbs": "file:../nestjs-hbs",
    "@gboutte/nestjs-pdf": "file:../nestjs-pdf",
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/platform-fastify": "^9.1.6",
    "puppeteer": "^19.4.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "28.1.8",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser":

<details>
<summary>英文:</summary>

I am creating a dependancy to generate PDF on nestjs, to do it I am using puppeteer.


When I try to use puppeteer inside the dependancy I get an error:

Error: Run npm install to download the correct Chromium revision (1056772).


But when I do it from the project and not the dependancy there is no error. It only happen when I use puppeteer from the dependancy, but it&#39;s launched from the same project.
----
First I generated the PDF inside a nestjs project and it worked fine.
Then I moved the code to my library inside a service but then there is the error.
I tried to run the following command to install chromium

node node_modules/puppeteer/install.js


But nothing change and here is the result of the command:
&gt; Chromium is already in C:\Users\Greg\.cache\puppeteer\chrome\win64-1069273; skipping download.
I also tried to delete the node modules and reinstall it but no change.
The package.json of the dependancy:
```json
{
&quot;name&quot;: &quot;@gboutte/nestjs-pdf&quot;,
&quot;version&quot;: &quot;0.0.3&quot;,
&quot;description&quot;: &quot;This package provide a service to render PDF from html string or from handlebars tempaltes for nestjs.&quot;,
&quot;keywords&quot;: [
&quot;nestjs&quot;,
&quot;handlebars&quot;,
&quot;hbs&quot;,
&quot;templates&quot;,
&quot;pdf&quot;
],
&quot;homepage&quot;: &quot;https://github.com/gboutte/nestjs-pdf#readme&quot;,
&quot;bugs&quot;: {
&quot;url&quot;: &quot;https://github.com/gboutte/nestjs-pdf/issues&quot;,
&quot;email&quot;: &quot;gboutte@protonmail.com&quot;
},
&quot;repository&quot;: {
&quot;type&quot;: &quot;git&quot;,
&quot;url&quot;: &quot;https://github.com/gboutte/nestjs-pdf&quot;
},
&quot;main&quot;: &quot;dist/index.js&quot;,
&quot;types&quot;: &quot;dist/index.d.ts&quot;,
&quot;scripts&quot;: {
&quot;test&quot;: &quot;echo \&quot;Error: no test specified\&quot; &amp;&amp; exit 1&quot;,
&quot;build&quot;: &quot;rimraf dist &amp;&amp; tsc&quot;,
&quot;prepublish&quot;: &quot;npm run build&quot;
},
&quot;author&quot;: &quot;Gr&#233;gory Boutte &lt;gboutte@protonmail.com&gt;&quot;,
&quot;license&quot;: &quot;MIT&quot;,
&quot;dependencies&quot;: {},
&quot;devDependencies&quot;: {
&quot;@nestjs/common&quot;: &quot;^9.1.6&quot;,
&quot;@gboutte/nestjs-hbs&quot;: &quot;^0.0.3&quot;,
&quot;@types/node&quot;: &quot;^18.11.5&quot;,
&quot;puppeteer&quot;: &quot;^19.1.2&quot;,
&quot;reflect-metadata&quot;: &quot;^0.1.13&quot;,
&quot;rimraf&quot;: &quot;^3.0.2&quot;,
&quot;rxjs&quot;: &quot;^7.5.7&quot;,
&quot;typescript&quot;: &quot;^4.8.4&quot;
},
&quot;peerDependencies&quot;: {
&quot;@nestjs/common&quot;: &quot;^9.1.6&quot;,
&quot;@gboutte/nestjs-hbs&quot;: &quot;^0.0.3&quot;,
&quot;@types/node&quot;: &quot;^18.11.5&quot;,
&quot;puppeteer&quot;: &quot;^19.1.2&quot;,
&quot;reflect-metadata&quot;: &quot;^0.1.13&quot;,
&quot;rimraf&quot;: &quot;^3.0.2&quot;,
&quot;rxjs&quot;: &quot;^7.5.7&quot;,
&quot;typescript&quot;: &quot;^4.8.4&quot;
}
}

And the package.json of the main project

{
  &quot;name&quot;: &quot;nestjs-hbs-demo&quot;,
  &quot;version&quot;: &quot;0.0.1&quot;,
  &quot;description&quot;: &quot;&quot;,
  &quot;author&quot;: &quot;&quot;,
  &quot;private&quot;: true,
  &quot;main&quot;: &quot;dist/index.js&quot;,
  &quot;readmeFilename&quot;: &quot;README.md&quot;,
  &quot;files&quot;: [
    &quot;dist/**/*&quot;,
    &quot;*.md&quot;
  ],
  &quot;license&quot;: &quot;UNLICENSED&quot;,
  &quot;scripts&quot;: {
    &quot;prebuild&quot;: &quot;rimraf dist&quot;,
    &quot;build&quot;: &quot;nest build&quot;,
    &quot;format&quot;: &quot;prettier --write \&quot;src/**/*.ts\&quot; \&quot;test/**/*.ts\&quot;&quot;,
    &quot;start&quot;: &quot;nest start&quot;,
    &quot;start:dev&quot;: &quot;nest start --watch&quot;,
    &quot;start:debug&quot;: &quot;nest start --debug --watch&quot;,
    &quot;start:prod&quot;: &quot;node dist/main&quot;,
    &quot;lint&quot;: &quot;eslint \&quot;{src,apps,libs,test}/**/*.ts\&quot; --fix&quot;,
    &quot;test&quot;: &quot;jest&quot;,
    &quot;test:watch&quot;: &quot;jest --watch&quot;,
    &quot;test:cov&quot;: &quot;jest --coverage&quot;,
    &quot;test:debug&quot;: &quot;node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand&quot;,
    &quot;test:e2e&quot;: &quot;jest --config ./test/jest-e2e.json&quot;
  },
  &quot;dependencies&quot;: {
    &quot;@gboutte/nestjs-hbs&quot;: &quot;file:../nestjs-hbs&quot;,
    &quot;@gboutte/nestjs-pdf&quot;: &quot;file:../nestjs-pdf&quot;,
    &quot;@nestjs/common&quot;: &quot;^9.0.0&quot;,
    &quot;@nestjs/core&quot;: &quot;^9.0.0&quot;,
    &quot;@nestjs/platform-express&quot;: &quot;^9.0.0&quot;,
    &quot;@nestjs/platform-fastify&quot;: &quot;^9.1.6&quot;,
    &quot;puppeteer&quot;: &quot;^19.4.1&quot;,
    &quot;reflect-metadata&quot;: &quot;^0.1.13&quot;,
    &quot;rimraf&quot;: &quot;^3.0.2&quot;,
    &quot;rxjs&quot;: &quot;^7.2.0&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;@nestjs/cli&quot;: &quot;^9.0.0&quot;,
    &quot;@nestjs/schematics&quot;: &quot;^9.0.0&quot;,
    &quot;@nestjs/testing&quot;: &quot;^9.0.0&quot;,
    &quot;@types/express&quot;: &quot;^4.17.13&quot;,
    &quot;@types/jest&quot;: &quot;28.1.8&quot;,
    &quot;@types/node&quot;: &quot;^16.0.0&quot;,
    &quot;@types/supertest&quot;: &quot;^2.0.11&quot;,
    &quot;@typescript-eslint/eslint-plugin&quot;: &quot;^5.0.0&quot;,
    &quot;@typescript-eslint/parser&quot;: &quot;^5.0.0&quot;,
    &quot;eslint&quot;: &quot;^8.0.1&quot;,
    &quot;eslint-config-prettier&quot;: &quot;^8.3.0&quot;,
    &quot;eslint-plugin-prettier&quot;: &quot;^4.0.0&quot;,
    &quot;jest&quot;: &quot;28.1.3&quot;,
    &quot;prettier&quot;: &quot;^2.3.2&quot;,
    &quot;source-map-support&quot;: &quot;^0.5.20&quot;,
    &quot;supertest&quot;: &quot;^6.1.3&quot;,
    &quot;ts-jest&quot;: &quot;28.0.8&quot;,
    &quot;ts-loader&quot;: &quot;^9.2.3&quot;,
    &quot;ts-node&quot;: &quot;^10.0.0&quot;,
    &quot;tsconfig-paths&quot;: &quot;4.1.0&quot;,
    &quot;typescript&quot;: &quot;^4.7.4&quot;
  },
  &quot;jest&quot;: {
    &quot;moduleFileExtensions&quot;: [
      &quot;js&quot;,
      &quot;json&quot;,
      &quot;ts&quot;
    ],
    &quot;rootDir&quot;: &quot;src&quot;,
    &quot;testRegex&quot;: &quot;.*\\.spec\\.ts$&quot;,
    &quot;transform&quot;: {
      &quot;^.+\\.(t|j)s$&quot;: &quot;ts-jest&quot;
    },
    &quot;collectCoverageFrom&quot;: [
      &quot;**/*.(t|j)s&quot;
    ],
    &quot;coverageDirectory&quot;: &quot;../coverage&quot;,
    &quot;testEnvironment&quot;: &quot;node&quot;
  }
}

答案1

得分: 1

我通过更新本地的 Node.js,并将两个项目的 @types/node 依赖项更改为相同版本来解决了这个问题。

"@types/node": "^18.0.0",

通过尝试从 GitHub 安装库而不是本地安装,我发现了这个问题。

之前我使用了 npm install ../directory-to-my-lib,然后我使用了 npm install <github仓库的url>

不同之处在于,使用 GitHub npm 会报错,指出 @types/node 的两个版本不同,我不知道为什么使用另一条命令时没有出现这个错误。

现在我已经修复了版本问题,它可以在所有安装方法中正常工作(npm包/ GitHub / 本地)。

英文:

I resolved the issue by updating my local nodejs and I changed the @types/node dependency of both project to be the same.

&quot;@types/node&quot;: &quot;^18.0.0&quot;,

I found out that it was this problem by trying to install the library with github instead of locally.

Before I used npm install ../directory-to-my-lib, then I used npm install &lt;url to the github repo&gt;.

The difference is that using github npm gave me an error saying that both version of @types/node were different, I don't know why I did not have this error using the other command.

Now that I fixed the version it works with all installation methods (npm package / github / locally).

huangapple
  • 本文由 发表于 2023年1月8日 07:13:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75044427.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定