chrisveness / geodesy

Libraries of geodesy functions implemented in JavaScript
http://www.movable-type.co.uk/scripts/geodesy-library.html
MIT License
1.17k stars 203 forks source link

Require Support #81

Closed dscape closed 4 years ago

dscape commented 4 years ago

Was trying to use the module in node.js but couldn't require it. Ended up installing version 1.1 from npm to circumvent this issue.

Any specific reason not to support require?

chrisveness commented 4 years ago

Version 2 of the geodesy library was a significant development, including adding modern reference frames in addition to traditional datums, and refactoring & extending n-vector functions. I had to use ES6 modules to achieve the new functionality due to the more complex interdependencies.

You're welcome to continue using version 1.

What is the reason you don't want to use import? I no longer find I need to use require() at all!

dscape commented 4 years ago

Hi @chrisveness – I haven't coded in 7 years and I haven't caught up with the differences yet. Also I don't do client side development so have no need for any of such features, in the server it actually makes code a bit less easy to read to have a mixture of requires and imports.

Why would something import but not work with require? can't you simply module.export it if module exists? Thanks for the prompt reply and amazing work, this works incredibly well. I was able to code a simple way to snap my runs to a route in a couple of hours of work!! Thank you

chrisveness commented 4 years ago

ES modules (using export/import statements) is the standardised native way of specifying modules (c39.es/ecma262/#sec-modules). Node.js is transitioning from CommonJS to ES modules (nodejs.org/api/esm.html).

Unfortuntely, ESM is generally incompatible with CommonJS (which uses module.exports/require()) – something of a headache for Node.js developers!

However, ESM has many advantages over CommonJS (static, asynchronous, isomorphic, etc), and is certainly the future for JavaScript. exploringjs.com/es6/ch_modules.html has good information.

Saying that, you should be able to use dynamic import to include ES module files from CommonJS code, though I've not tried it myself: nodejs.org/api/esm.html#esm_import_expressions.

dscape commented 4 years ago

Thanks Chris. Still strange not being able to require something you npm installed, definitely an anti-pattern. Appreciate all the feedback hopefully other with the same issue will know the solution is to use version 1.1.3!