Skip to content
This repository was archived by the owner on Sep 2, 2023. It is now read-only.

Commit aea4058

Browse files
committed
Add createRequireFunction
1 parent 49791b4 commit aea4058

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

proposals/esm-in-js-files.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ import { importCommonJS } from 'module';
2121
importCommonJS(import.meta, './commonjs-file.js');
2222
```
2323
24-
### `.cjs`
24+
### `createRequire`
25+
26+
See [nodejs/node#19360](https://github.com/nodejs/node/pull/19360):
27+
28+
```js
29+
import { createRequire } from 'module';
30+
const require = createRequire(new URL(import.meta.url));
31+
const x = require('./commonjs-file.js');
32+
```
33+
34+
### `.cjs` file extension
2535
2636
If a user wants to use the `import` statement to import a CommonJS file, that file needs to have a `.cjs` extension. This would allow intermixing of ESM `.js` files and CommonJS `.cjs` files, and potentially the use of CommonJS in browser environments. The `.cjs` extension would map to `application/node`, even on the Web.
2737

0 commit comments

Comments
 (0)