You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Import constants from node:vm. Log it, it returns undefined
How often does it reproduce? Is there a required condition?
No response
What is the expected behavior? Why is that the expected behavior?
constants is meant to return an object
What do you see instead?
undefined
Additional information
Was following along this issue: #51154
I wanted to be able to use import in my Script instance, and the constants are supposed to allow me use the default loader without flags, but the constants object is returning undefined. I am using
Both worked. I am now a tadbit confused, when I build my project and compile to bytecode, I am using the vm later on to interpret it. constants in my bytecode loader script is undefined. But it works when I just create a script in my project and node ./test.cjs directly.
This is some of the bytecode loader code:
constfs=require("fs");constpath=require("path");const{Script,constants}=require("node:vm");constv8=require("v8");constModule=require("module");v8.setFlagsFromString("--no-lazy");v8.setFlagsFromString("--no-flush-bytecode");constFLAG_HASH_OFFSET=12;constSOURCE_HASH_OFFSET=8;letdummyBytecode;console.log(constants);// logs 'undefined'functionsetFlagHashHeader(bytecodeBuffer){if(!dummyBytecode){constscript=newScript("",{produceCachedData: true,// trying to actually use it here});dummyBytecode=script.createCachedData();}dummyBytecode.slice(FLAG_HASH_OFFSET,FLAG_HASH_OFFSET+4).copy(bytecodeBuffer,FLAG_HASH_OFFSET);};// ....
Maybe somehow you are not using the right version of Node.js that has vm.constants to load it?
Running process.versions.node pretty much answered it. I am using version 20 at runtime, but my version is 22. Not sure how to force an upgrade, I tried nvmrc, npmrc, engine strict, and engines config but they didn't do it. Might be a case of my project dependencies bundled with node 20?
Activity
Jamesernator commentedon Apr 27, 2024
It is available if you use default import:
But it does seem like a bug that it isn't exposed as a named export (maybe because it's experimental?).
targos commentedon Apr 27, 2024
Can't reproduce:
ShafSpecs commentedon Apr 27, 2024
Using your same code:
$ node test.mjs [Object: null prototype] { USE_MAIN_CONTEXT_DEFAULT_LOADER: Symbol(vm_dynamic_import_main_context_default) } # used `require` here $ node test.cjs [Object: null prototype] { USE_MAIN_CONTEXT_DEFAULT_LOADER: Symbol(vm_dynamic_import_main_context_default) }Both worked. I am now a tadbit confused, when I build my project and compile to bytecode, I am using the vm later on to interpret it.
constantsin my bytecode loader script isundefined. But it works when I just create a script in my project andnode ./test.cjsdirectly.This is some of the bytecode loader code:
joyeecheung commentedon May 1, 2024
Maybe somehow you are not using the right version of Node.js that has vm.constants to load it?
ShafSpecs commentedon May 1, 2024
Running process.versions.node pretty much answered it. I am using version 20 at runtime, but my version is 22. Not sure how to force an upgrade, I tried nvmrc, npmrc, engine strict, and engines config but they didn't do it. Might be a case of my project dependencies bundled with node 20?
avivkeller commentedon May 4, 2024
Hi, it seems the issue was resolved, but if anyone disagrees, please re open this issue