Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module 'win-dpapi' #57

Open
sanjarani opened this issue Dec 18, 2023 · 7 comments
Open

Cannot find module 'win-dpapi' #57

sanjarani opened this issue Dec 18, 2023 · 7 comments

Comments

@sanjarani
Copy link

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);
		}
.
.
.
@mdashlw
Copy link

mdashlw commented Jan 4, 2024

keytar and win-dpapi are required on Windows

"optionalDependencies": {
"keytar": "^7.9.0",
"win-dpapi": "^1.1.0"
},

Install via npm install keytar win-dpapi

@rubengmurray
Copy link
Collaborator

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 npm/rfcs#120.

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

@abcluis
Copy link

abcluis commented Mar 23, 2024

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

@rubengmurray
Copy link
Collaborator

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
Copy link
Collaborator

keytar and win-dpapi are required on Windows

"optionalDependencies": {
"keytar": "^7.9.0",
"win-dpapi": "^1.1.0"
},

Install via npm install keytar win-dpapi

keytar is actually only required on MacOS

keytar = require('keytar');

@rubengmurray
Copy link
Collaborator

#60

Could be a solution. Welcome thoughts on it.

@rubengmurray
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants