Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: generateKeyPairSync not a function

I am trying to use the generateKeyPairSync() function of the crypto module of node.js. But am getting a typerError saying that there is no such function.
Its strange because another function of the same module createCipher() works perfectly!

Here is the sample code:

import * as cryp from 'crypto';

export function generate_keys() {
  console.log('hey there!');
  const keyPair = cryp.generateKeyPairSync('rsa', {
    modulusLength: 4096,
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem'
    },
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem',
      cipher: 'aes-256-cbc',
      passphrase: 'top secret'
    }
  });
  console.log(keyPair);
}


function encrypt(data: string) {
  let cipher = cryp.createCipher('aes-256-ecb', 'pass');
  cipher.update(data, 'utf8');
  return cipher.final('hex');
}


// This is working
console.log(encrypt('asa'));

// Getting error - 'generateKeyPairSync is not a function'
generate_keys();

This is the complete console output:

[Running] ts-node "d:\projects\myProject\src\ply.ts"
4eb35bc94bd4d17fad4857b17558b2ad
hey there!

d:\projects\myProject\src\ply.ts:5
  const keyPair = cryp.generateKeyPairSync('rsa', {
                       ^
TypeError: cryp.generateKeyPairSync is not a function
    at generate_keys (d:\projects\myProject\src\ply.ts:5:24)
    at Object.<anonymous> (d:\projects\myProject\src\ply.ts:33:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Module.m._compile (C:\Users\anmolsingh.jaggi\AppData\Local\Yarn\Data\global\node_modules\ts-node\src\index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\anmolsingh.jaggi\AppData\Local\Yarn\Data\global\node_modules\ts-node\src\index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)

[Done] exited with code=1 in 2.097 seconds
like image 491
Anmol Singh Jaggi Avatar asked Dec 15 '25 05:12

Anmol Singh Jaggi


1 Answers

From the docs:

Added in: v10.12.0

I guess you are running an earlier version of node

like image 157
Tom Cumming Avatar answered Dec 16 '25 17:12

Tom Cumming



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!