TypeError: crypto.createPrivateKey is not a function

别说谁变了你拦得住时间么 提交于 2021-01-07 03:14:36

问题


I am running into this error when trying to login with node. error:

TypeError: crypto.createPrivateKey is not a function

When I researched it appears that I need to be on version v.11.+ of node, not v.10.+ However, when I upgrade to node 11, the error is still there. I have uninstalled node completely, cleared npm cache, and reinstalled... yet that didn't work either.

export const InitializeJWT = async (): Promise<void> => {
  const password = crypto.pseudoRandomBytes(25).toString('base64');
  const salt = crypto.pseudoRandomBytes(25).toString('base64');
  jwtPayloadKey = crypto.scryptSync(password, salt, 32);
  jwtPayloadIv = crypto.randomBytes(16);
  const rsaKeys = crypto.generateKeyPairSync('rsa', {
    modulusLength: 2048,
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem',
    },
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem',
      cipher: 'aes-256-cbc',
      passphrase: password,
    },
  });
  jwtSign.privateKey = crypto.createPrivateKey({
    key: rsaKeys.privateKey,
    format: 'pem',
    type: 'pkcs8',
    passphrase: password,
  });
  jwtSign.publicKey = crypto.createPublicKey({
    key: rsaKeys.publicKey,
    format: 'pem',
    type: 'spki',
  });
  debug('JWT Keys Initialized: ', password);
};

Has anyone ran into this error before? I am on node version 11.3.0 and npm version is 6.4.1


回答1:


I ran into this problem when writing an Electron app. Even though I have NodeJS v12 installed, Electron bundles its own version of NodeJS, which in my case was much older. For anyone using Electron, from the developer tools console, this information is in process.version for NodeJS, and process.versions for other bundled components, including the version of Electron itself.

To address your immediate question, Yuri Tarabanko has the correct answer. You say you are using Node version 11.3, the docs state that createPrivateKey was added in 11.6.0: https://nodejs.org/api/crypto.html#crypto_crypto_createprivatekey_key



来源:https://stackoverflow.com/questions/59166307/typeerror-crypto-createprivatekey-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!