简体   繁体   English

crypto.createPrivateKey 不是 next.js 应用程序中的 function

[英]crypto.createPrivateKey is not a function in a next.js app

In my next.js app i use the function createPrivateKey from the crypto module from node.js as stated in this thread: TypeError: crypto.createPrivateKey is not a function it says the function got added in v11.6.0 but when I run npx next info i get:在我的 next.js 应用程序中,我使用来自 node.js 的加密模块的 function createPrivateKey,如该线程中所述: TypeError: crypto.createPrivateKey is not a function它说 function 已添加到npx next info

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro N
Binaries:
  Node: 16.16.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 13.0.6
  eslint-config-next: 13.0.6
  react: 18.2.0
  react-dom: 18.2.0

createPrivateKey doc:创建私钥文档:

https://nodejs.org/api/crypto.html#crypto_crypto_createprivatekey_key https://nodejs.org/api/crypto.html#crypto_crypto_createprivatekey_key

how I call the function:我如何称呼 function:

  const [privateKey, setPrivateKey] = useState("");
  const [publicKey, setPublicKey] = useState("");

  useEffect(() => {
    // Create a private key object from the private key
    const _privateKey = createPrivateKey({
      key: privateKey,
      format: "pem",
      type: "pkcs1",
    } as PrivateKeyInput);

    // Export the private key as a public key
    const publicKey = _privateKey.export({
      type: "spki",
      format: "pem",
    });

    console.log("Private key:", privateKey);
    console.log("Public key:", publicKey.toString("hex"));
    setPublicKey(publicKey.toString("hex"));
  }, [privateKey]);

The pre-rendering process of Next.js is as follow: Next.js的预渲染流程如下:

  1. Next.js pre-renders your React page component on the server (represented as the NextPage type in TypeScript), so you need to add import crypto from 'crypto'; crypto.randomUUID() Next.js 在服务器上预渲染您的 React 页面组件(在 TypeScript 中表示为NextPage类型),因此您需要添加import crypto from 'crypto'; crypto.randomUUID() import crypto from 'crypto'; crypto.randomUUID() to generate a unique id on the server. import crypto from 'crypto'; crypto.randomUUID()在服务器上生成一个唯一的 id。

  2. After the HTML page is returned to the browser as a result of pre-rendering, you need to use window.crypto.randomUUID() because crypto.randomUUID() is from the Node.js package in the server environment, not in the browser. HTML页面预渲染返回到浏览器后,需要使用window.crypto.randomUUID() ,因为crypto.randomUUID()是服务器环境下的Node.js package,不是浏览器。 U can add the following check to your code:您可以将以下检查添加到您的代码中:

// The following Node.js package is imported on the server-side 
// and not available in the browser
import crypto from 'crypto';

// Use the web browser Crypto API if you're on the client, 
// otherwise use the Node.js Crypto API on the server
typeof window !== 'undefined' ? window.crypto.randomUUID() : crypto.randomUUID()

See Safely Accessing Web APIs on the Next.js doc to learn more.请参阅 Next.js 文档中的安全访问 Web API以了解更多信息。

Node.js and web browsers have some APIs with the same names [^1], which cause conflict in your Next.js app. Node.js 和 web 浏览器有一些同名的 API [^1],这会导致您的 Next.js 应用发生冲突。 Therefore, you need to be explicit and specific about which environment (Node.js or browsers) the APIs belong to.因此,您需要明确和具体说明 API 属于哪个环境(Node.js 或浏览器)。

[^1]: This might be because both Chrome and Node.js use the same V8 JavaScript engine under the hood, so they share some API interfaces [^1]: 这可能是因为Chrome 和 Node.js 在底层使用相同的 V8 JavaScript 引擎,所以它们共享一些 API 接口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM