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

ESM build is not working #38

Closed
kasperpeulen opened this issue Oct 12, 2023 · 11 comments · Fixed by #42
Closed

ESM build is not working #38

kasperpeulen opened this issue Oct 12, 2023 · 11 comments · Fixed by #42

Comments

@kasperpeulen
Copy link

kasperpeulen commented Oct 12, 2023

Expected Behavior

I should be able to run jsx-email in node in projects that are "type:module":

{
  "type": "module",
  "dependencies": {
    "@jsx-email/all": "^2.0.1"
  }
}

Actual Behavior

I get an error:

file:///Users/kasperpeulen/code/modern-web-app/bla.js:1
import {renderAsync} from '@jsx-email/all';
        ^^^^^^^^^^^
SyntaxError: Named export 'renderAsync' not found. The requested module '@jsx-email/all' 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 '@jsx-email/all';
const {renderAsync} = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)

Node.js v18.18.0

Additional Information

I think this can be fixed by making sure that the ESM build outputs .mjs files.

image

I tried renaming the esm output files locally in node_modules and that worked.

@shellscape
Copy link
Owner

This is not the fault of jsx-email. We export both ESM and CJS. Please fill out the entire issue template next time.

@kasperpeulen
Copy link
Author

Added a repro @shellscape

@shellscape shellscape reopened this Oct 12, 2023
@shellscape
Copy link
Owner

Your reproduction doesn't show an error on my end. Looks like it's working fine.

Screenshot 2023-10-12 at 9 15 00 AM

@kasperpeulen
Copy link
Author

@shellscape I added now a stackblitz start command (node index.js), you should see:
image

@shellscape
Copy link
Owner

OK thanks we'll take a look

@shellscape
Copy link
Owner

I've managed to reproduce this locally. ESM is such a pain. It may be a bit before we roll out a fix for this, it requires altering how our build process works significantly. If you can use TypeScript, I'd recommend doing so.

@kasperpeulen
Copy link
Author

kasperpeulen commented Oct 13, 2023

I think if you want to use typescript, the steps could be.

  1. Place an extra package.json file in the dist/es directory. With as only content:
{
  "type": "module",
}

This avoid having to manually rename js files to mjs files.
2. Change the moduleResolution to node16
3. Add .js file extensions to all relative imports in the source code. For example:
export * from './body.js';

@shellscape
Copy link
Owner

alright, we're going to completely change up our build process in the next few days which should resolve this.

@shellscape
Copy link
Owner

the next few days turned into a week. haven't forgotten about it, just got swamped with work. going to run some tests this evening.

@shellscape
Copy link
Owner

shellscape commented Oct 23, 2023

@kasperpeulen should be resolved now. cheers

@alexanderson1993
Copy link

Hey, huge thanks for putting in the work to do this. You just unblocked me shipping the next version of my personal newsletter platform. Can't wait for my emails to look ✨ beautiful ✨!

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

Successfully merging a pull request may close this issue.

3 participants