Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm: constants is undefined #52716

Closed
ShafSpecs opened this issue Apr 26, 2024 · 6 comments
Closed

vm: constants is undefined #52716

ShafSpecs opened this issue Apr 26, 2024 · 6 comments
Labels
vm Issues and PRs related to the vm subsystem.

Comments

@ShafSpecs
Copy link

Version

22.0.0

Platform

Microsoft Windows NT 10.0.22631.0 x64

Subsystem

No response

What steps will reproduce the bug?

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

@Jamesernator
Copy link

It is available if you use default import:

import vm from "node:vm";

But it does seem like a bug that it isn't exposed as a named export (maybe because it's experimental?).

@targos
Copy link
Member

targos commented Apr 27, 2024

Can't reproduce:

$ cat test.mjs
import { constants } from 'node:vm';
console.log(constants);

$ node test.mjs
[Object: null prototype] {
  USE_MAIN_CONTEXT_DEFAULT_LOADER: Symbol(vm_dynamic_import_main_context_default)
}

@RedYetiDev RedYetiDev added the vm Issues and PRs related to the vm subsystem. label Apr 27, 2024
@ShafSpecs
Copy link
Author

import { constants } from 'node:vm';
console.log(constants);

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. 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:

const fs = require("fs");
const path = require("path");
const {Script,constants} = require("node:vm");
const v8 = require("v8");
const Module = require("module");
v8.setFlagsFromString("--no-lazy");
v8.setFlagsFromString("--no-flush-bytecode");
const FLAG_HASH_OFFSET = 12;
const SOURCE_HASH_OFFSET = 8;
let dummyBytecode;
console.log(constants); // logs 'undefined'
function setFlagHashHeader(bytecodeBuffer) {
  if (!dummyBytecode) {
    const script = new Script("", {
      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);
};
// ....

@joyeecheung
Copy link
Member

joyeecheung commented May 1, 2024

Maybe somehow you are not using the right version of Node.js that has vm.constants to load it?

@ShafSpecs
Copy link
Author

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?

@RedYetiDev
Copy link
Member

Hi, it seems the issue was resolved, but if anyone disagrees, please re open this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vm Issues and PRs related to the vm subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants