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

Require of jse-eval fails when build with typescript #57

Closed
FrankVisuals opened this issue Mar 28, 2022 · 4 comments · Fixed by #58
Closed

Require of jse-eval fails when build with typescript #57

FrankVisuals opened this issue Mar 28, 2022 · 4 comments · Fixed by #58
Labels
bug Something isn't working released

Comments

@FrankVisuals
Copy link

FrankVisuals commented Mar 28, 2022

I have a typescript project where I import jse-eval - the typescript build works fine, but when executing the built files, it will fail with Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

I've created a very lightweight repo to reproduce this issue: https://github.com/FrankVisuals/error-showcase

I did not find any way (changing to require instead of import, or import * as ...) to get it running when using a typescript build before.

The

import { parse } from "jse-eval";

Is built to

Object.defineProperty(exports, "__esModule", { value: true });
const jse_eval_1 = require("jse-eval");

Based on the package.json I would expect require("jse-eval") to target the .cjs version. The full error shown below, indicates the module is tried to be loaded:

require() of /jse-error/node_modules/jse-eval/dist/jse-eval.module.js from /jse-error/dist/index.js 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 jse-eval.module.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /jse-error/node_modules/jse-eval/package.json.

My node version is v14.19.0
My npm version is 8.5.5
MacOs 12.2.1

Is this an expected behaviour? Based on the docs it seems that this should actually work.

@6utt3rfly
Copy link
Owner

@FrankVisuals - I'll try to look into this later tonight, but in the meantime, can you try with both 1.4.0 and 1.3.0? 1.3.0 only had a default export, not any named exports. 1.4.0 flipped the package.json file for the project from cjs to esm and added the named exports.

@6utt3rfly 6utt3rfly added the bug Something isn't working label Mar 28, 2022
6utt3rfly added a commit that referenced this issue Mar 29, 2022
using a default export causes the module build file to be used by typescript when it transpiles to cjs, which then doesn't work for mixing esm and cjs. Simply removing the default line resolves the issue.

Fixes #57
github-actions bot pushed a commit that referenced this issue Mar 29, 2022
## [1.4.1](v1.4.0...v1.4.1) (2022-03-29)

### Bug Fixes

* remove default export ([b2f5d83](b2f5d83)), closes [#57](#57)
* simplify exported typings ([5f8367f](5f8367f))
@github-actions
Copy link

🎉 This issue has been resolved in version 1.4.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@6utt3rfly
Copy link
Owner

@FrankVisuals - it looks like it was caused by the exports.default in jse-eval's package.json file. I tested with your provided example and it seemed to work properly now with the 1.4.1 release. Thank you for reporting this issue!

@FrankVisuals
Copy link
Author

Thanks for the fast solution! I was just about to test it with 1.3 which forced me to use
import { default } from "jse-eval" - I'll give it a try with 1.4.1 :) !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants