Skip to content

Commit

Permalink
fix: export package.json required by react-native and bundlers
Browse files Browse the repository at this point in the history
It appears that react-native and some bundlers rely on being able to
introspect the package.json file of a npm module. After adding the
`exports` field to package.json, only the paths defined in that field
are accessible to Node.js. In order to support introspection of the
package.json file itself it has to be listed as an export as well.

See also:
ai/nanoevents#44 (comment)

Fixes #444
  • Loading branch information
ctavan committed May 12, 2020
1 parent 3b4b35c commit 2e93665
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions examples/node-commonjs/example.js
Expand Up @@ -45,3 +45,7 @@ console.log('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));
console.log('uuid.v5() DNS', uuid.v5('hello.example.com', uuid.v5.DNS));
console.log('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL));
console.log('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE));

// Some tools like react-native need to introspect the package.json file
const pkg = require('uuid/package.json');
console.log('pkg.name', pkg.name);
3 changes: 3 additions & 0 deletions examples/node-esmodules/example.mjs
@@ -1,5 +1,6 @@
import { v1 as uuidv1, v4 as uuidv4, v3 as uuidv3, v5 as uuidv5 } from 'uuid';
import * as uuid from 'uuid';
import pkg from 'uuid/package.json';

console.log('uuidv1()', uuidv1());

Expand Down Expand Up @@ -41,3 +42,5 @@ console.log('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));
console.log('uuid.v5() DNS', uuid.v5('hello.example.com', uuid.v5.DNS));
console.log('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL));
console.log('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE));

console.log('pkg.name', pkg.name);
2 changes: 1 addition & 1 deletion examples/node-esmodules/package.json
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"scripts": {
"test": "node --experimental-modules example.mjs"
"test": "node --experimental-modules --experimental-json-modules example.mjs"
},
"dependencies": {
"uuid": "file:../../.local"
Expand Down
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -19,8 +19,11 @@
"sideEffects": false,
"main": "./dist/index.js",
"exports": {
"require": "./dist/index.js",
"import": "./wrapper.mjs"
"./package.json": "./package.json",
".": {
"require": "./dist/index.js",
"import": "./wrapper.mjs"
}
},
"module": "./dist/esm-node/index.js",
"browser": {
Expand Down

0 comments on commit 2e93665

Please sign in to comment.