6utt3rfly / jse-eval

Javascript Expression Evaluator
MIT License
22 stars 7 forks source link

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

Closed FrankVisuals closed 2 years ago

FrankVisuals commented 2 years ago

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 commented 2 years ago

@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.

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 1.4.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

6utt3rfly commented 2 years ago

@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 commented 2 years ago

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 :) !