镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

vm: constants is undefined #52716

Description

@ShafSpecs

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

Activity

  1. Jamesernator commented on Apr 27, 2024

    @Jamesernator

    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?).

  2. targos commented on Apr 27, 2024

    @targos
    Member

    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)
    }
    
  3. added
    vmIssues and PRs related to the vm subsystem.
    on Apr 27, 2024
  4. ShafSpecs commented on Apr 27, 2024

    @ShafSpecs
    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);
    };
    // ....
  5. joyeecheung commented on May 1, 2024

    @joyeecheung
    Member

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

  6. ShafSpecs commented on May 1, 2024

    @ShafSpecs
    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?

  7. avivkeller commented on May 4, 2024

    @avivkeller
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    vmIssues and PRs related to the vm subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions