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 20, 2020
1 parent a21e4d8 commit ab3dec6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions examples/node-commonjs/example.js
Original file line number Diff line number Diff line change
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);
4 changes: 4 additions & 0 deletions examples/node-esmodules/example.mjs
Original file line number Diff line number Diff line change
@@ -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,6 @@ 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
console.log('pkg.name', pkg.name);
2 changes: 1 addition & 1 deletion examples/node-esmodules/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion scripts/testpack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ npm init -y
npm install ../uuid/uuid-*.tgz

node commonjs.js
node esmodules.mjs
node --experimental-json-modules esmodules.mjs

0 comments on commit ab3dec6

Please sign in to comment.