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

Failed to import in Node #89

Closed
rainst opened this issue Oct 24, 2020 · 2 comments
Closed

Failed to import in Node #89

rainst opened this issue Oct 24, 2020 · 2 comments

Comments

@rainst
Copy link

rainst commented Oct 24, 2020

I'm working on a new node project with typescript and I'm getting stuck when trying to import the geodesy module.
I've been doing some research before deciding to write here but I wasn't able to solve the issue.

  • node 15.0.1
  • geodesy 2.2.2

this is how I'm importing the lib:
import LatLon from "geodesy/latlon-ellipsoidal-vincenty.js";

and this is the error the compiler gives me:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/brein/projects/msfs/msfs-airports-api/node_modules/geodesy/latlon-ellipsoidal-vincenty.js
require() of ES modules is not supported.
require() of /Users/brein/projects/msfs/msfs-airports-api/node_modules/geodesy/latlon-ellipsoidal-vincenty.js from /Users/brein/projects/msfs/msfs-airports-api/src/database/index.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename latlon-ellipsoidal-vincenty.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/brein/projects/msfs/msfs-airports-api/node_modules/geodesy/package.json.

at new NodeError (node:internal/errors:258:15)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1100:13)
at Module.load (node:internal/modules/cjs/loader:948:32)
at Function.Module._load (node:internal/modules/cjs/loader:789:14)
at Module.require (node:internal/modules/cjs/loader:972:19)
at require (node:internal/modules/cjs/helpers:88:18)
at Object.<anonymous> (/Users/brein/projects/msfs/msfs-airports-api/src/database/index.ts:2:1)
at Module._compile (node:internal/modules/cjs/loader:1083:30)
at Module.m._compile (/Users/brein/projects/msfs/msfs-airports-api/node_modules/ts-node/src/index.ts:1043:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1112:10)

I found similar issues where the solution was to use esm to import modules, but according to the README:

The library can be loaded from npm to be used in a Node.js app (in Node.js v13.2.0+, or using the esm package in v8.0.0–v12.15.0*)

and to this:
#79 (comment)

since I'm using node 15 it should just work... or am I missing something?

PS: I tried also installing esm and launch with ts-node -r esm just to get a different error

@rainst
Copy link
Author

rainst commented Oct 24, 2020

so after doing some more research I found out I had to add type: module in package.json but then it felt like opening pandora's box as it looks like ts-node is not fully supporting esm modules :(

@rainst rainst closed this as completed Oct 24, 2020
@chrisveness
Copy link
Owner

If you're having difficulties using ES6 modules, the best thing might be for you to use [email protected].

Documentation is available on the v1.1.3 readme, with notes documenting the differences between v1 & v2 at www.movable-type.co.uk/scripts/geodesy-library-migrating-from-v1.html.

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

2 participants