Comments (6)
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.
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.
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.
Maybe somehow you are not using the right version of Node.js that has vm.constants to load it?
from node.
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.
Hi, it seems the issue was resolved, but if anyone disagrees, please re open this issue
from node.
Related Issues (20)
- Commit queue overwrites commit author HOT 7
- Clarification Needed: require('http') and require('node:http') with existing require.cache entries HOT 20
- Implement `bytes()` method on Blob for reading bytes into a Uint8Array HOT 12
- معالجة مشكلة
- العقدة
- `nextTick` doesn't execute as expected with `.mjs` HOT 7
- [node-api]: New variant `napi_set_named_property` that treats '\0' as values. HOT 3
- `import-in-the-middle` causes exception when creating `ERR_MODULE_NOT_FOUND` message HOT 18
- FixedQueue potential memory savings HOT 3
- Remove calls to APIs deprecated in V8 12.6 HOT 1
- Prefer "import attributes" to "import assertions" HOT 1
- `AttachCppHeap` and `DetachCppHeap` are deprecated in V8
- Backport undici MessageEvent to v20. HOT 2
- [v22.2.0 regression] Some `Worker` use cases are broken HOT 5
- [discuss] Move `lint-js-fix` to `lint-js --fix` + `lint-md --fix` HOT 4
- Default `--experimental-detect-module` to `commonjs` in require hooks HOT 2
- Bare specifiers definition. HOT 24
- Async brotli decompress-compress HOT 13
- dot test-reporter includes more info HOT 4
- [v20.12.2 -> v20.13.0 regression] TestRunner: awaited variables no longer in scope HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node.