Gregory Boutte
Gregory Boutte

Reputation: 733

How to use puppeteer inside dependencies?

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'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:

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:

{
  "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": "[email protected]"
  },
  "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 <[email protected]>",
  "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

{
  "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": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.1.3",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "28.0.8",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.1.0",
    "typescript": "^4.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Upvotes: 1

Views: 1035

Answers (1)

Gregory Boutte
Gregory Boutte

Reputation: 733

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

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

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 <url to the github repo>.

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).

Upvotes: 1

Related Questions