Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esm: add import.meta.require #55730

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,19 @@ import { readFileSync } from 'node:fs';
const buffer = readFileSync(new URL('./data.proto', import.meta.url));
```

### `import.meta.require(id)`

<!-- YAML
added: REPLACEME
-->

> Stability: 1.1 - Active development

* `id` {string} The module name or path.
* Returns: {any} The module exports.

Alias for [`module.createRequire(import.meta.url)(id)`][].

### `import.meta.resolve(specifier)`

<!-- YAML
Expand Down Expand Up @@ -1118,6 +1131,7 @@ resolution for ESM specifiers is [commonjs-extension-resolution-loader][].
[`import.meta.url`]: #importmetaurl
[`import`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
[`module.createRequire()`]: module.md#modulecreaterequirefilename
[`module.createRequire(import.meta.url)(id)`]: module.md#modulecreaterequirefilename
[`module.syncBuiltinESMExports()`]: module.md#modulesyncbuiltinesmexports
[`package.json`]: packages.md#nodejs-packagejson-field-definitions
[`path.dirname()`]: path.md#pathdirnamepath
Expand Down
16 changes: 16 additions & 0 deletions lib/internal/modules/esm/initialize_import_meta.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use strict';

const {
ObjectDefineProperty,
StringPrototypeStartsWith,
} = primordials;

const { getOptionValue } = require('internal/options');
const { fileURLToPath } = require('internal/url');
const { dirname } = require('path');
const { setOwnProperty } = require('internal/util');
const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta-resolve');

/**
Expand Down Expand Up @@ -71,6 +73,20 @@ function initializeImportMeta(meta, context, loader) {

meta.url = url;

ObjectDefineProperty(meta, 'require', {
__proto__: null,
enumerable: true,
configurable: true,
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved
get() {
const { Module: { createRequire } } = require('internal/modules/cjs/loader');
this.require = createRequire(url);
return this.require;
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved
},
set(value) {
setOwnProperty(this, 'require', value);
},
});

return meta;
}

Expand Down
12 changes: 11 additions & 1 deletion test/es-module/test-esm-import-meta.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ import assert from 'assert';

assert.strictEqual(Object.getPrototypeOf(import.meta), null);

const keys = ['dirname', 'filename', 'resolve', 'url'];
const keys = ['dirname', 'filename', 'resolve', 'url', 'require'];
assert.deepStrictEqual(Reflect.ownKeys(import.meta), keys);

{
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved
const requireDescriptor = Object.getOwnPropertyDescriptor(import.meta, 'require');
assert.strictEqual(requireDescriptor.value, undefined);
assert.strictEqual(requireDescriptor.enumerable, true);
assert.strictEqual(requireDescriptor.writable, undefined);
assert.strictEqual(requireDescriptor.configurable, true);
}

delete import.meta.require; // Verified above.

const descriptors = Object.getOwnPropertyDescriptors(import.meta);
for (const descriptor of Object.values(descriptors)) {
delete descriptor.value; // Values are verified below.
Expand Down
34 changes: 34 additions & 0 deletions test/es-module/test-import-meta-require.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import '../common/index.mjs';
import assert from 'node:assert';
import { describe, it } from 'node:test';

describe('import.meta.require', () => {
it('should require a built-in module', () => {
const requiredAssert = import.meta.require('node:assert');
assert.deepStrictEqual(assert, requiredAssert);
});

it('should require a esm module', () => {
const { foo, bar } = import.meta.require('../fixtures/es-module-loaders/module-named-exports.mjs');
assert.strictEqual(foo, 'foo');
assert.strictEqual(bar, 'bar');
});

it('should require a cjs module', () => {
const { foo, bar } = import.meta.require('../fixtures/es-modules/cjs-module-exports.js');
assert.strictEqual(foo, 'foo');
assert.strictEqual(bar, 'bar');
});

it('should require a json module', () => {
const { foo, bar } = import.meta.require('../fixtures/es-modules/foobar.json');
assert.strictEqual(foo, 'foo');
assert.strictEqual(bar, 'bar');
});

it('should throw MODULE_NOT_FOUND', () => {
assert.throws(() => import.meta.require('does-not-exist'), {
code: 'MODULE_NOT_FOUND'
});
});
});
4 changes: 4 additions & 0 deletions test/fixtures/es-modules/cjs-module-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
foo: 'foo',
bar: 'bar'
}
4 changes: 4 additions & 0 deletions test/fixtures/es-modules/foobar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": "foo",
"bar": "bar"
}
Loading