Skip to content

Commit f7e35ae

Browse files
committed
Prefix node built-in module imports with node:.
This makes the imports diistinguishable from the corresponding `npm` packages, which can help with: - Development ergonomics: - Marking all `node:*` packages as external in a bundler. - Tracking the dependency graph - Supply chain security (avoiding risks from name squatting). The `node:` protocol prefix is supported in LTS versions all the way back to `node` 12: https://nodejs.org/api/esm.html#node-imports
1 parent a9651ff commit f7e35ae

File tree

13 files changed

+22
-22
lines changed

13 files changed

+22
-22
lines changed

src/lib/libatomic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ addToLibrary({
153153

154154
emscripten_num_logical_cores: () =>
155155
#if ENVIRONMENT_MAY_BE_NODE
156-
ENVIRONMENT_IS_NODE ? require('os').cpus().length :
156+
ENVIRONMENT_IS_NODE ? require('node:os').cpus().length :
157157
#endif
158158
navigator['hardwareConcurrency'],
159159
});

src/lib/libcore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ addToLibrary({
352352
var cmdstr = UTF8ToString(command);
353353
if (!cmdstr.length) return 0; // this is what glibc seems to do (shell works test?)
354354

355-
var cp = require('child_process');
355+
var cp = require('node:child_process');
356356
var ret = cp.spawnSync(cmdstr, [], {shell:true, stdio:'inherit'});
357357

358358
var _W_EXITCODE = (ret, sig) => ((ret) << 8 | (sig));

src/lib/libsockfs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ addToLibrary({
208208
var WebSocketConstructor;
209209
#if ENVIRONMENT_MAY_BE_NODE
210210
if (ENVIRONMENT_IS_NODE) {
211-
WebSocketConstructor = /** @type{(typeof WebSocket)} */(require('ws'));
211+
WebSocketConstructor = /** @type{(typeof WebSocket)} */(require('node:ws'));
212212
} else
213213
#endif // ENVIRONMENT_MAY_BE_NODE
214214
{
@@ -503,7 +503,7 @@ addToLibrary({
503503
if (sock.server) {
504504
throw new FS.ErrnoError({{{ cDefs.EINVAL }}}); // already listening
505505
}
506-
var WebSocketServer = require('ws').Server;
506+
var WebSocketServer = require('node:ws').Server;
507507
var host = sock.saddr;
508508
#if SOCKET_DEBUG
509509
dbg(`websocket: listen: ${host}:${sock.sport}`);

src/lib/libwasi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ var WasiLibrary = {
564564
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
565565
// This block is not needed on v19+ since crypto.getRandomValues is builtin
566566
if (ENVIRONMENT_IS_NODE) {
567-
var nodeCrypto = require('crypto');
567+
var nodeCrypto = require('node:crypto');
568568
return (view) => nodeCrypto.randomFillSync(view);
569569
}
570570
#endif // ENVIRONMENT_MAY_BE_NODE

src/lib/libwasm_worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ if (ENVIRONMENT_IS_WASM_WORKER
286286

287287
emscripten_navigator_hardware_concurrency: () => {
288288
#if ENVIRONMENT_MAY_BE_NODE
289-
if (ENVIRONMENT_IS_NODE) return require('os').cpus().length;
289+
if (ENVIRONMENT_IS_NODE) return require('node:os').cpus().length;
290290
#endif
291291
return navigator['hardwareConcurrency'];
292292
},

src/preamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ function instantiateSync(file, info) {
654654
var binary = getBinarySync(file);
655655
#if NODE_CODE_CACHING
656656
if (ENVIRONMENT_IS_NODE) {
657-
var v8 = require('v8');
657+
var v8 = require('node:v8');
658658
// Include the V8 version in the cache name, so that we don't try to
659659
// load cached code from another version, which fails silently (it seems
660660
// to load ok, but we do actually recompile the binary every time).

src/proxyClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#if ENVIRONMENT_MAY_BE_NODE
1919
var ENVIRONMENT_IS_NODE = typeof process == 'object' && typeof process.versions == 'object' && typeof process.versions.node == 'string' && process.type != 'renderer';
2020
if (ENVIRONMENT_IS_NODE) {
21-
var NodeWorker = require('worker_threads').Worker;
21+
var NodeWorker = require('node:worker_threads').Worker;
2222
global.Worker = function(url, options) {
2323
// Special handling for `data:` URL argument, to match the behaviour
2424
// of the Web API.

src/runtime_shared.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,6 @@ if (ENVIRONMENT_IS_NODE) {
9191
// depends on it for accurate timing.
9292
// Use `global` rather than `globalThis` here since older versions of node
9393
// don't have `globalThis`.
94-
global.performance ??= require('perf_hooks').performance;
94+
global.performance ??= require('node:perf_hooks').performance;
9595
}
9696
#endif

src/shell.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ if (ENVIRONMENT_IS_NODE) {
103103
#if EXPORT_ES6
104104
// When building an ES module `require` is not normally available.
105105
// We need to use `createRequire()` to construct the require()` function.
106-
const { createRequire } = await import('module');
106+
const { createRequire } = await import('node:module');
107107
/** @suppress{duplicate} */
108108
var require = createRequire(import.meta.url);
109109
#endif
110110

111111
#if PTHREADS || WASM_WORKERS
112-
var worker_threads = require('worker_threads');
112+
var worker_threads = require('node:worker_threads');
113113
global.Worker = worker_threads.Worker;
114114
ENVIRONMENT_IS_WORKER = !worker_threads.isMainThread;
115115
#if PTHREADS
@@ -196,8 +196,8 @@ if (ENVIRONMENT_IS_NODE) {
196196

197197
// These modules will usually be used on Node.js. Load them eagerly to avoid
198198
// the complexity of lazy-loading.
199-
var fs = require('fs');
200-
var nodePath = require('path');
199+
var fs = require('node:fs');
200+
var nodePath = require('node:path');
201201

202202
#if EXPORT_ES6
203203
// EXPORT_ES6 + ENVIRONMENT_IS_NODE always requires use of import.meta.url,

src/shell_minimal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ if (ENVIRONMENT_IS_NODE && ENVIRONMENT_IS_SHELL) {
9494
var defaultPrint = console.log.bind(console);
9595
var defaultPrintErr = console.error.bind(console);
9696
if (ENVIRONMENT_IS_NODE) {
97-
var fs = require('fs');
97+
var fs = require('node:fs');
9898
defaultPrint = (...args) => fs.writeSync(1, args.join(' ') + '\n');
9999
defaultPrintErr = (...args) => fs.writeSync(2, args.join(' ') + '\n');
100100
}
@@ -167,7 +167,7 @@ if (!ENVIRONMENT_IS_PTHREAD) {
167167
// Wasm or Wasm2JS loading:
168168

169169
if (ENVIRONMENT_IS_NODE) {
170-
var fs = require('fs');
170+
var fs = require('node:fs');
171171
#if WASM == 2
172172
if (typeof WebAssembly != 'undefined') Module['wasm'] = fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm');
173173
else eval(fs.readFileSync(__dirname + '/{{{ TARGET_BASENAME }}}.wasm.js')+'');

0 commit comments

Comments
 (0)