From 39a94d13e75d4cd4761573b5c4d2f5ce6cd05cd9 Mon Sep 17 00:00:00 2001 From: Swen Kooij Date: Fri, 7 Jun 2024 14:57:21 +0200 Subject: [PATCH] Add `exports` in to make `/native` work in Node 16.x ESM modules (#318) If you're trying to import `react-content-loader/native` from Node 16.x from within a ESM context (`"type": "module"`). You'll get the following error: ``` node:internal/process/esm_loader:97 internalBinding('errors').triggerUncaughtException( ^ Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '~/node_modules/react-content-loader/native' is not supported resolving ES modules imported from /Users/swen/Code/cheetah/test/test.mjs Did you mean to import react-content-loader/native/react-content-loader.native.cjs.js? at new NodeError (node:internal/errors:387:5) at finalizeResolution (node:internal/modules/esm/resolve:425:17) at moduleResolve (node:internal/modules/esm/resolve:1006:10) at defaultResolve (node:internal/modules/esm/resolve:1214:11) at nextResolve (node:internal/modules/esm/loader:165:28) at ESMLoader.resolve (node:internal/modules/esm/loader:844:30) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:431:18) at ModuleWrap. (node:internal/modules/esm/module_job:76:40) at link (node:internal/modules/esm/module_job:75:36) { code: 'ERR_UNSUPPORTED_DIR_IMPORT', url: 'file:///~/node_modules/react-content-loader/native' } ``` By setting up an explicit export for the `/native` subdirectory the import works. No other changes are made to maintain backwards compatibility. --- package.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/package.json b/package.json index 5e812ce..be60990 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,20 @@ "module": "dist/react-content-loader.es.js", "jsnext:main": "dist/react-content-loader.es.js", "types": "dist/web/index.d.ts", + "exports": { + ".": { + "types": "./dist/web/index.d.ts", + "require": "dist/react-content-loader.cjs.js", + "import": "dist/react-content-loader.es.js", + "default": "dist/react-content-loader.cjs.js" + }, + "./native": { + "types": "./native/native/index.d.ts", + "require": "./native/react-content-loader.native.cjs.js", + "import": "./native/react-content-loader.native.es.js", + "default": "./native/react-content-loader.native.cjs.js" + } + }, "bugs": { "url": "https://github.com/danilowoz/react-content-loader/issues" },