mirror of
https://github.com/nodejs/node.git
synced 2025-05-01 08:42:45 +00:00

This commit allows require() and import to search the node_modules directories when importing a core module that must have the node: scheme. This prevents these core modules from shadowing userland modules with the same name but no prefix. PR-URL: https://github.com/nodejs/node/pull/42430 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Beth Griggs <bgriggs@redhat.com> Reviewed-By: Richard Lau <rlau@redhat.com>
52 lines
1.6 KiB
JavaScript
52 lines
1.6 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const tmpdir = require('../common/tmpdir');
|
|
const assert = require('assert');
|
|
const { spawnSync } = require('child_process');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { createRequire } = require('module');
|
|
|
|
assert.throws(
|
|
() => require('test'),
|
|
common.expectsError({ code: 'MODULE_NOT_FOUND' }),
|
|
);
|
|
|
|
(async () => {
|
|
await assert.rejects(
|
|
async () => import('test'),
|
|
common.expectsError({ code: 'ERR_MODULE_NOT_FOUND' }),
|
|
);
|
|
})().then(common.mustCall());
|
|
|
|
assert.throws(
|
|
() => require.resolve('test'),
|
|
common.expectsError({ code: 'MODULE_NOT_FOUND' }),
|
|
);
|
|
|
|
// Verify that files in node_modules can be resolved.
|
|
tmpdir.refresh();
|
|
|
|
const packageRoot = path.join(tmpdir.path, 'node_modules', 'test');
|
|
const indexFile = path.join(packageRoot, 'index.js');
|
|
|
|
fs.mkdirSync(packageRoot, { recursive: true });
|
|
fs.writeFileSync(indexFile, 'module.exports = { marker: 1 };');
|
|
|
|
function test(argv) {
|
|
const child = spawnSync(process.execPath, argv, { cwd: tmpdir.path });
|
|
assert.strictEqual(child.status, 0);
|
|
assert.strictEqual(child.stdout.toString().trim(), '{ marker: 1 }');
|
|
}
|
|
|
|
test(['-e', 'console.log(require("test"))']);
|
|
test(['-e', 'import("test").then(m=>console.log(m.default))']);
|
|
test(['--input-type=module', '-e', 'import test from "test";console.log(test)']);
|
|
test(['--input-type=module', '-e', 'console.log((await import("test")).default)']);
|
|
|
|
{
|
|
const dummyFile = path.join(tmpdir.path, 'file.js');
|
|
const require = createRequire(dummyFile);
|
|
assert.strictEqual(require.resolve('test'), indexFile);
|
|
}
|