无法在 node.js repl 之外访问 "v8"

Can't access "v8" outside of node.js repl

我需要使用 node.js 的“v8”变量来序列化和反序列化一些缓冲区。但是我似乎无法在 Node.js repl 之外访问它。这是一些截图

This is accessing v8 inside of the node.js repl

this is trying to access v8 inside of a file

是否有任何解决方法或方法可以用来访问文件内部的 v8?

v8,在 Node.js REPL 中,是一个预定义变量,与 v8 内置 模块 相同。

您可以通过ES模块和CommonJS访问该模块:

// ES
import v8 from "v8";

// CommonJS
const v8 = require("v8");