Skip to content

Commit bac0341

Browse files
committed
fix: export package.json required by react-native and bundlers
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
1 parent 3b4b35c commit bac0341

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

examples/node-commonjs/example.js

+4
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ console.log('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));
4545
console.log('uuid.v5() DNS', uuid.v5('hello.example.com', uuid.v5.DNS));
4646
console.log('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL));
4747
console.log('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE));
48+
49+
// Some tools like react-native need to introspect the package.json file
50+
const pkg = require('uuid/package.json');
51+
console.log('pkg.name', pkg.name);

examples/node-esmodules/example.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { v1 as uuidv1, v4 as uuidv4, v3 as uuidv3, v5 as uuidv5 } from 'uuid';
22
import * as uuid from 'uuid';
3+
import pkg from 'uuid/package.json';
34

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

@@ -41,3 +42,6 @@ console.log('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));
4142
console.log('uuid.v5() DNS', uuid.v5('hello.example.com', uuid.v5.DNS));
4243
console.log('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL));
4344
console.log('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE));
45+
46+
// Some tools like react-native need to introspect the package.json file
47+
console.log('pkg.name', pkg.name);

examples/node-esmodules/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.0.0",
44
"private": true,
55
"scripts": {
6-
"test": "node --experimental-modules example.mjs"
6+
"test": "node --experimental-modules --experimental-json-modules example.mjs"
77
},
88
"dependencies": {
99
"uuid": "file:../../.local"

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
"sideEffects": false,
2020
"main": "./dist/index.js",
2121
"exports": {
22-
"require": "./dist/index.js",
23-
"import": "./wrapper.mjs"
22+
"./package.json": "./package.json",
23+
".": {
24+
"require": "./dist/index.js",
25+
"import": "./wrapper.mjs"
26+
}
2427
},
2528
"module": "./dist/esm-node/index.js",
2629
"browser": {

scripts/testpack.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ npm init -y
1818
npm install ../uuid/uuid-*.tgz
1919

2020
node commonjs.js
21-
node esmodules.mjs
21+
node --experimental-json-modules esmodules.mjs

0 commit comments

Comments
 (0)