astronomy icon indicating copy to clipboard operation
astronomy copied to clipboard

Node importing broken

Open bdougherty opened this issue 2 years ago • 11 comments

I think this was broken by #263.

When importing using import * as Astronomy from 'astronomy-engine'; into a Node project that is marked as a module in package.json or into an .mjs file, it works correctly when using version 2.1.7, but if you use the latest 2.1.15 version, you will get the following error:

/Users/brad/Downloads/test/node_modules/astronomy-engine/esm/astronomy.js:38
export const C_AUDAY = 173.1446326846693;
^^^^^^

SyntaxError: Unexpected token 'export'

It seems to work in both versions when using require.

bdougherty avatar Feb 25 '23 03:02 bdougherty

@bdougherty I updated the package.json in this way the last time if you could figure out a valid node_modules/astronomy-engine/package.json for your setup it would be helpful

matheo avatar Feb 25 '23 03:02 matheo

@bdougherty Did the suggestion from @matheo help you? He is the npm expert here, so please let us know if you there is still a problem we can help you with.

cosinekitty avatar Mar 01 '23 01:03 cosinekitty

Sorry, I haven't had a chance to try anything out yet. I'll let you know what I find out when I'm able to get back to it.

bdougherty avatar Mar 01 '23 04:03 bdougherty

I'm building a project with "type": "module" in package.json, and on top of that I'm browserifying everything (I end up with a 1.2MB build). I often have problems importing stuff probably because of my inexperience with node and browserify, but I ended up doing this:

  1. Copy the esm/astronomy.js file to a different folder and rename it to astronomy.mjs (which lets node know that it's a module)
  2. Import it like this: import * as Astronomy from "./lib/astronomy.mjs";

Everything works as expected, my project builds perfectly, the library works on the browser and all my unit tests are green.

This is far from ideal but hopefully this helps someone move on. I'd love to know when this is fixed so I can go back to relying on the package itself and be able to update it as I would with any other package instead of having to copy the file manually.

sebagr avatar Apr 05 '23 12:04 sebagr

Is this still broken? I would like to support hassle-free package usage for Node.js, but it's not clear to me how to proceed.

cosinekitty avatar Jul 13 '23 01:07 cosinekitty

Had the same issue here in a typescript project. I pulled the source typescript in and used it directly to solve the problem so I'm all good, but I wanted to confirm it is still an issue.

By the way this is an amazing library. The level of clear and apparent care and passion that went into this is astounding! Thank you for the craftsmanship and great work.

KenAKAFrosty avatar Jul 30 '23 00:07 KenAKAFrosty

Had the same issue here in a typescript project. I pulled the source typescript in and used it directly to solve the problem so I'm all good, but I wanted to confirm it is still an issue.

As mentioned by @bdougherty creating this issue, apparently 40cd7e3b0e44dbe09c6e741d63ea6d030c9e9836 fixed problems for Deno users but broke stuff for Node users. It looks like the main change is the addition of an "exports" section to package.json, and changing "typings" to "types".

I'm still hoping someone who is an expert on JavaScript module stuff can drop by and help sort this out. This is too far outside my realm of expertise!

By the way this is an amazing library. The level of clear and apparent care and passion that went into this is astounding! Thank you for the craftsmanship and great work.

Hi @KenAKAFrosty and thank you for the kind words.

cosinekitty avatar Jul 31 '23 01:07 cosinekitty

I have the same problem. Tried to remove export section and it imports fine without any problems.

https://github.com/cosinekitty/astronomy/blob/61dc07020aaa6885d2c7f688a4d82beaf6edb9ef/source/js/package.json#L39-L45

Is it really necessary to define export?

talyguryn avatar Feb 08 '24 10:02 talyguryn

@cosinekitty I change the exports to the following, by removing /esm and it works with node. Maybe this minor change is all that's needed.

"exports": { 
   ".": { 
     "require": "./astronomy.js", 
     "import": "./astronomy.js", 
     "types": "./astronomy.d.ts" 
   } 
 },

pep108 avatar Feb 08 '24 16:02 pep108

@talyguryn and @pep108, I would like to try either or both of these to close this issue, but unfortunately I do not know enough about the Node/JavaScript world to evaluate whether these changes will work for everyone, or only in the specific cases we are testing.

I'm concerned about continuing to make changes that break existing projects using the astronomy-engine npm package for random subsets of developers who depend on it. You can see this issue started because of my previous attempt to fix usage by Deno in #263. I can visualize this trend continuing of me thinking I have fixed it, only for another issue to be opened later.

I guess what we need is a Node.js expert to come forward and help educate us with best practices for exporting the code and types for a TypeScript-based npm package.

cosinekitty avatar Feb 08 '24 17:02 cosinekitty

As I found to use exports we need to upgrade typescript to 4.7+ and define moduleResolution as node16

Related links:

talyguryn avatar Feb 08 '24 19:02 talyguryn

hi, I ran into this issue today and can confirm that making the change mentioned here and rerunning npm install fixed the issue for me.

Thanks for making this package available!

aatishb avatar Jan 23 '25 22:01 aatishb

This is still broken for ESM import. Is there any progress on a fix?

edit: I was able to work around by importing createRequire

import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const Astronomy = require('astronomy-engine');

mfisch04 avatar Aug 12 '25 18:08 mfisch04