• Stars
    star
    132
  • Rank 273,407 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 4 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Generate ESM wrapper files for CommonJS modules

gen-esm-wrapper

Generate ESM wrapper files for CommonJS modules

This CLI tool makes it easier for module authors to support both ES modules and CommonJS modules for Node.js. It looks at the exports of an existing CommonJS module, and creates an ES module wrapper file that makes the exports available as named exports.

For example, if you are in the root of your npm package, you can use

gen-esm-wrapper . path/to/wrapper/output.mjs

to create a wrapper file, and add

  ...
  "exports": {
    ".": {
      "require": "./<same file as 'main'>",
      "import": "./path/to/wrapper/output.mjs"
    },
    "./": "./"
  }
  ...

to your package.json in order to make your module available as a dual ESM/CJS package. The command can also be added as a build step before publishing.

The above example "exports": key can be simplified to

  ...
  "exports": {
    "require": "./<same file as 'main'>",
    "import": "./path/to/wrapper/output.mjs"
  }
  ...

if there are no other JavaScript files that can be required from the package besides the main entry point (e.g. a file named foo.js that can be loaded through require('package/foo')), or if you are okay with breaking require() calls to those modules. This is the recommended approach for new modules that are being published for the first time.

Example project

There is a minimal example in the example-project directory in this repository. This is its package.json:

{
  "name": "example-project",
  "version": "1.0.0",
  "description": "Example project for gen-esm-wrapper",
  "main": "index.js",
  "exports": {
    ".": {
      "require": "./index.js",
      "import": "./dist/output.mjs"
    },
    "./": "./"
  },
  "scripts": {
    "test": "npm run build && node ./dist/output.mjs",
    "build": "gen-esm-wrapper . ./dist/output.mjs",
    "prepack": "npm run build"
  },
  "devDependencies": {
    "gen-esm-wrapper": "^1.0.4"
  }
}

More Repositories

1

lzma-native

Node.js interface to the native liblzma compression library (.xz file format, among others)
JavaScript
100
star
2

synchronous-worker

Node.js Workers, except on the same thread
C++
75
star
3

levenshtein-sse

SIMD-enabled Levenshtein distance in C++
C++
65
star
4

munkres-js

Munkres (aka Hungarian) algorithm for JS
JavaScript
58
star
5

workers-sudoku

Node.js HTTP server that solves sudokus using Worker threads
JavaScript
36
star
6

nice-napi

nice(2) bindings for Node.js
JavaScript
20
star
7

69

nice(2) bindings for Node.js
JavaScript
18
star
8

node-core-coverage

Create coverage for Node.js core JS and C++ sources
Shell
18
star
9

promise-ratelimit

Rate limiting (throttling) implementation for Promises on Node.js
JavaScript
15
star
10

node-levenshtein-sse

Node.js bindings for the levenshtein-sse lib
JavaScript
9
star
11

json-split-stream

A fast way to split concatenated JSON data into individual chunks
JavaScript
9
star
12

cold-storage

Full serialization/deserialization for JS objects
JavaScript
9
star
13

eventemitter-asyncresource

AsyncResource integration for EventEmitter
TypeScript
8
star
14

set-promise-reject-callback

Direct access to unhandled rejections
C++
6
star
15

remarkup

HTML semantics and program logic separator
JavaScript
6
star
16

frictionary

Wikipedia summaries for fictionary
JavaScript
6
star
17

ztrace

strace equivalent for Node.js โ€“ Still somewhat in progress!
JavaScript
5
star
18

actual-crash

Node.js native addon that just crashes
JavaScript
5
star
19

serdes

Polyfill for the Node.js 8.x serializer API
JavaScript
5
star
20

duplexpair

Make a full duplex stream with 2 Duplex endpoints
JavaScript
5
star
21

stackdump

Get a dump file for your stack and inspect it in a browser
JavaScript
5
star
22

gyp-parser

GYP file format parser in JS
JavaScript
5
star
23

object-chunker

Chunk object-mode streams in Node.js
JavaScript
4
star
24

reverse-test

Test your packageโ€™s dependents!
JavaScript
4
star
25

rlimitr

get/set resource limits on command line in env(1)-style (setrlimit/getrlimit wrapper)
C
4
star
26

even-chunks

Split an array evenly into chunks
JavaScript
3
star
27

npm-get-top-dependents

Get the top dependents of an npm package.
JavaScript
3
star
28

stream-sort

Pick the top n entries from an object-mode stream in Node.js.
JavaScript
3
star
29

what-is-node

Short presentation โ€œWhat is Node.js and what is it used for?โ€
HTML
3
star
30

regjsenum

Enumerate matched strings for a given regular expression.
JavaScript
2
star
31

angular-events-calendar

Calendar in the style of GitHub contributions
HTML
2
star
32

template-inspect

Tag for template strings using util.inspect
JavaScript
2
star
33

electron-spectron-fs-problem-repro

Minimal repro repository for electron 13.5.1/spectron 15.0.0/Windows/Node.js fs API bug
JavaScript
2
star
34

mongodb-connection-string-url-10-nonrepro

Non-repro for mongodb-js/mongodb-connection-string-url#10
TypeScript
2
star
35

q-ratelimit

Rate limiting (throttling) implementation for Q promises on Node.js
JavaScript
2
star
36

stuck

JS library to make all values on the current stack available
JavaScript
2
star
37

promises-talk

HTML
2
star
38

code-and-learn-2016

HTML
2
star
39

webpack-sigh

what it says in the name
JavaScript
2
star
40

action-test

github actions test repo
2
star
41

closures-talk

Short presentation about closures (for NodeSchool)
HTML
1
star
42

checkref

Check your HTML and Markdown for invalid local links & anchors
JavaScript
1
star
43

q-semaphore

Semaphore implementation for Q promises on Node.js
JavaScript
1
star
44

playback-stream

A Node.js stream whose contents can be played back many times
JavaScript
1
star
45

ttyname

ttyname(3) for Node using FFI
JavaScript
1
star
46

genders

A JSON library with genders and translations
JavaScript
1
star
47

xz

Own fork of http://git.tukaani.org/xz.git
C
1
star