最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Crypto | TypeError: X509Certificate is not a constructor - Stack Overflow

programmeradmin2浏览0评论
const { X509Certificate } = require('crypto');

function getCertificateThumbprint(certificate)
{  
  const cert = new X509Certificate(Buffer.from(certificate, 'base64'));
  return cert.fingerprint;
}

I practically copied this code from the crypto documentation code, also this is most likely not a problem with package installations, because crypto is built-in node.js module. Same error, when passing a plain string and not a Buffer.

const { X509Certificate } = require('crypto');

function getCertificateThumbprint(certificate)
{  
  const cert = new X509Certificate(Buffer.from(certificate, 'base64'));
  return cert.fingerprint;
}

I practically copied this code from the crypto documentation code, also this is most likely not a problem with package installations, because crypto is built-in node.js module. Same error, when passing a plain string and not a Buffer.

Share Improve this question edited Jul 30, 2021 at 15:36 Doctor Gonzo asked Jul 30, 2021 at 15:25 Doctor GonzoDoctor Gonzo 331 silver badge5 bronze badges 3
  • Could you debug to check the contents of X509Certificate? If you can't debug, use console.log(JSON.stringify(X509Certificate)); Let's see why it's not a constructor. – CausingUnderflowsEverywhere Commented Jul 31, 2021 at 4:07
  • I tried reproducing myself, and crypto doesn't include X509Certificate so that may be the problem we both have. – CausingUnderflowsEverywhere Commented Jul 31, 2021 at 4:16
  • I'm working with NodeJS version 20.15.1 on ReactJS but still get this issue. console.log(JSON.stringify(X509Certificate)) shown an undefined value. The error I get is: export 'X509Certificate' (imported as 'X509Certificate') was not found in 'crypto' – Nathalie Commented Sep 4, 2024 at 4:56
Add a ment  | 

1 Answer 1

Reset to default 9

The X509Certificate class was added recently in NodeJS version v15.6.0. Ensure you are using v15.6.0 or newer so that your import works.

发布评论

评论列表(0)

  1. 暂无评论