Code Monkey home page Code Monkey logo

Comments (6)

Jamesernator avatar Jamesernator commented on May 19, 2024

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

from node.

targos avatar targos commented on May 19, 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)
}

from node.

ShafSpecs avatar ShafSpecs commented on May 19, 2024

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);
};
// ....

from node.

joyeecheung avatar joyeecheung commented on May 19, 2024

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

from node.

ShafSpecs avatar ShafSpecs commented on May 19, 2024

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?

from node.

RedYetiDev avatar RedYetiDev commented on May 19, 2024

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

from node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.