chrome-cookies-secure icon indicating copy to clipboard operation
chrome-cookies-secure copied to clipboard

Cannot find module 'win-dpapi'

Open sanjarani opened this issue 1 year ago • 7 comments

I'm uisng this package with puppeteer.

This is package.json file:

{
  "name": "cookie-puppeteer",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "chrome-cookies-secure": "^2.1.1",
    "puppeteer": "^21.6.1",
    "puppeteer-extra": "^3.3.6",
    "puppeteer-extra-plugin-stealth": "^2.11.2"
  }
}

And this is my code from sample:

const chrome = require('chrome-cookies-secure');
const puppeteer = require('puppeteer-extra');
const StealthPlugin = require('puppeteer-extra-plugin-stealth');

puppeteer.use(StealthPlugin());

const url = 'https://nowsecure.nl/';


const getCookies = (callback) => {
    chrome.getCookies(url, 'puppeteer', function (err, cookies) {
        if (err) {
            console.log(err, 'error');
            return
        }
        console.log(cookies, 'cookies');
        callback(cookies);
    }, 'yourProfile') // e.g. 'Profile 2'
};

getCookies(async (cookies) => {
    const browser = await puppeteer.launch({
        headless: false
    });
    const page = (await browser.pages())[0];

    await page.setCookie(...cookies);
    await page.goto(url);
    // await page.waitFor(1000);
    // browser.close(); //You missed await for this line but in my case I do not want to close browser so it is commented
});

And I have this error:

Uncaught Error: Cannot find module 'win-dpapi' Module._resolveFilename loader:1144 Module._load loader:985 Module.require loader:1235 require helpers:176 getDerivedKey index.js:72 getCookies index.js:368 getCookies index.js:11 (anonymous function) index.js:21 Module._compile loader:1376 Module._extensions..js loader:1435 Module.load loader:1207 Module._load loader:1023 executeUserEntryPoint run_main:135 (anonymous function) run_main_module:28

index.js from `node-modules/chrome-cookies-secure/index.js:

.
.
.
	} else if (process.platform === 'linux') {

		chromePassword = 'peanuts';
		crypto.pbkdf2(chromePassword, SALT, ITERATIONS, KEYLENGTH, 'sha1', callback);

	} else if (process.platform === 'win32') {

		// On Windows, the crypto is managed entirely by the OS.  We never see the keys.
		dpapi = require('win-dpapi'); //line 72
		callback(null, null);
	}
.
.
.

line 368:

.
.
.

	getDerivedKey(function (err, derivedKey) { //line 368

		if (err) {
			return callback(err);
		}
.
.
.

sanjarani avatar Dec 18 '23 01:12 sanjarani

keytar and win-dpapi are required on Windows https://github.com/bertrandom/chrome-cookies-secure/blob/8eeca6d405527327f30d95801ec048d38deaee1e/package.json#L38-L41

Install via npm install keytar win-dpapi

mdashlw avatar Jan 04 '24 08:01 mdashlw

Yeah it would be good to have a way to declare and install "platform dependencies", but I don't think it's possible to declare these in package.json even now hence https://github.com/npm/rfcs/discussions/120.

Therefore certain dependencies that are platform specific are listed as optional only.

rubengmurray avatar Mar 10 '24 01:03 rubengmurray

If anyone else is receiving the error and already installed the dependencies and you are using electron, you need to rebuild the native modules after execute the npm install with the following commands:

npm install --save-dev @electron/rebuild

.\node_modules\.bin\electron-rebuild.cmd

here more details: https://www.electronjs.org/docs/latest/tutorial/using-native-node-modules

abcluis avatar Mar 23 '24 18:03 abcluis

Looks like a separate dependency issue? Presumably electron has a different edge case to the machine operating system? I've got no experience with electron so can't comment much on that.

Perhaps this repo should publish individual packages for the separate operating systems. The core logic could be shared but edge case dependencies could be handled in their own package.json files and made available separately on npm.

rubengmurray avatar Mar 24 '24 10:03 rubengmurray

keytar and win-dpapi are required on Windows

https://github.com/bertrandom/chrome-cookies-secure/blob/8eeca6d405527327f30d95801ec048d38deaee1e/package.json#L38-L41

Install via npm install keytar win-dpapi

keytar is actually only required on MacOS https://github.com/bertrandom/chrome-cookies-secure/blob/8eeca6d405527327f30d95801ec048d38deaee1e/index.js#L59

rubengmurray avatar Jun 30 '24 17:06 rubengmurray

https://github.com/bertrandom/chrome-cookies-secure/pull/60

Could be a solution. Welcome thoughts on it.

rubengmurray avatar Jun 30 '24 17:06 rubengmurray

Add README guidance in https://github.com/bertrandom/chrome-cookies-secure/pull/61/files

rubengmurray avatar Jul 06 '24 18:07 rubengmurray