Skip to content

Commit e6e5412

Browse files
ctavanbroofa
andauthoredMar 17, 2022
chore: run node-esmodule package.json test only in latest node LTS (#621)
While ESM support has been stable in current versions of Node.js 14.x and 16.x, support for importing JSON files is still experimental and evolving. Since we still want to cover the fact that the package is exporting it's package.json file, this test is now extracted to only run with the updated JSON import syntax which requires import assertions and which was introduced in Node.js 16.14.0. Fixes #619 Co-authored-by: Robert Kieffer <robert@broofa.com>
1 parent 3a033f6 commit e6e5412

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed
 

‎a

Whitespace-only changes.

‎examples/node-esmodules/example.mjs

-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
version as uuidVersion,
1111
} from 'uuid';
1212
import * as uuid from 'uuid';
13-
import pkg from 'uuid/package.json';
1413

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

@@ -65,6 +64,3 @@ console.log('uuid.parse()', uuid.parse(MY_NAMESPACE));
6564
console.log('uuid.stringify()', uuid.stringify(uuid.parse(MY_NAMESPACE)));
6665
console.log('uuid.validate()', uuid.validate(MY_NAMESPACE));
6766
console.log('uuid.version()', uuid.version(MY_NAMESPACE));
68-
69-
// Some tools like react-native need to introspect the package.json file
70-
console.log('pkg.name', pkg.name);

‎examples/node-esmodules/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"version": "0.0.0",
44
"private": true,
55
"scripts": {
6-
"test": "node --experimental-modules --experimental-json-modules example.mjs"
6+
"test:package": "( node --version | grep -vq 'v16' ) || ( node --experimental-json-modules package.mjs )",
7+
"test:example": "node example.mjs",
8+
"test": "npm-run-all test:*"
79
},
810
"dependencies": {
911
"uuid": "file:../../.local/uuid"

‎examples/node-esmodules/package.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import pkg from 'uuid/package.json' assert { type: 'json' };
2+
3+
// Some tools like react-native need to introspect the package.json file
4+
console.log('pkg.name', pkg.name);

‎scripts/testpack.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mkdir -p ../test-pack
1010

1111
cp examples/node-commonjs/example.js ../test-pack/commonjs.js
1212
cp examples/node-esmodules/example.mjs ../test-pack/esmodules.mjs
13+
cp examples/node-esmodules/package.mjs ../test-pack/esmodules-package.mjs
1314

1415
cd ../test-pack
1516

@@ -18,4 +19,9 @@ npm init -y
1819
npm install ../uuid/uuid-*.tgz
1920

2021
node commonjs.js
21-
node --experimental-json-modules esmodules.mjs
22+
node esmodules.mjs
23+
24+
# Support for json esm imports requires import assertions starting in Node.js v16.14.0 which were
25+
# not supported in earlier versions. Therefore we restrict the ESM test to more recent versions of
26+
# Node.js:
27+
( node --version | grep -vq 'v16' ) || ( node --experimental-json-modules esmodules-package.mjs )

0 commit comments

Comments
 (0)
Please sign in to comment.