TypeError:crypto.createPrivateKey不是一个函数 [英] TypeError: crypto.createPrivateKey is not a function
问题描述
尝试登录节点时遇到此错误.错误:
I am running into this error when trying to login with node. error:
TypeError: crypto.createPrivateKey is not a function
当我研究时,似乎需要使用节点的v.11.+版本,而不是v.10.+.但是,当我升级到节点11时,错误仍然存在.我已经完全卸载了节点,清除了npm缓存,然后重新安装了……但还是没有用.
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);
};
以前有人遇到过此错误吗?我的节点版本为11.3.0,npm版本为6.4.1
Has anyone ran into this error before? I am on node version 11.3.0 and npm version is 6.4.1
推荐答案
在编写Electron应用程序时遇到了这个问题.即使我已经安装了NodeJS v12,Electron还是捆绑了自己的NodeJS版本,在我看来,它的版本要旧得多.对于使用Electron的任何人,从开发者工具控制台,此信息在NodeJS的 process.version
中,在其他捆绑的组件的 process.versions
中,包括Electron本身的版本
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.
为了解决您的紧迫问题,尤里·塔拉班波(Yuri Tarabanko)给出了正确答案.您说您正在使用Node版本11.3,文档指出在11.6.0中添加了 createPrivateKey
: https://nodejs.org/api/crypto.html#crypto_crypto_createprivatekey_key
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
这篇关于TypeError:crypto.createPrivateKey不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!