Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot importing using Node.js native ESM #271

Open
LinusU opened this issue Aug 5, 2021 · 3 comments
Open

Cannot importing using Node.js native ESM #271

LinusU opened this issue Aug 5, 2021 · 3 comments

Comments

@LinusU
Copy link
Contributor

LinusU commented Aug 5, 2021

When trying to import this library with the native support for ESM that's available in Node.js 12, 14, and 16 I'm running into the following problem:

import { isPointInPolygon } from 'geolib';
         ^^^^^^^^^^^^^^^^
SyntaxError: Named export 'isPointInPolygon' not found. The requested module 'geolib' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'geolib';
const { isPointInPolygon } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:179:5)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)
    at async handleMainPromise (node:internal/modules/run_main:63:12)

According to the readme it seems like this should work:

Screenshot 2021-08-05 at 12 50 31

I think that the problem is that the package.json is missing the exports key which describes how the module should be loaded by the native Node.js ESM.

This is documented here: https://nodejs.org/api/packages.html#packages_package_entry_points

Basically, I think that something like this should be added:

  "exports": {
    ".": "./es/index.js"
  }

I will try this and submit a PR if it works 🚀

@LinusU
Copy link
Contributor Author

LinusU commented Aug 5, 2021

@manuelbieh another approach would be to release a breaking change that drops CommonJS altogether. That would be my recommendation!

Many packages are moving to this since all current versions of Node.js supports ESM now 🚀

You can read about Sindres approach (which I, and many other maintainers are also using) here: sindresorhus/meta#15

@muuvmuuv
Copy link

It is as simple as:

  1. yarn add --dev esbuild
  2. Update typescript to latest version
  3. Update tsconfig.json
  4. yarn tsc
  5. yarn esbuild dist/tsc/*.js --outdir=dist/tsc --minify --allow-overwrite
  6. Remove babel/webpack etc. :) less boilerplate

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2017",
    "lib": [
      "es2018",
      "dom"
    ],
    "outDir": "dist/tsc",
    "rootDir": "src/",
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": false,
    "declaration": true,
    "declarationDir": "dist/types",
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "strictNullChecks": true
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.d.ts"
  ],
  "exclude": [
    "node_modules",
    "**/*.test.ts"
  ]
}

@misaon
Copy link

misaon commented May 2, 2022

Some update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants