tinyhttp/milliparsec

TypeScript - Load ESM

FranciscoMendes10866 opened this issue · 2 comments

Is there already a solution for the issue mentioned in issue #8 ?

Because I'm currently working on a TypeScript project and would love to use milliparsec.

When I use JavaScript I don't have any problems with the package.

It's a shame because this is the only body parser I'd love to use and doesn't work with TypeScript.

This is the error I got in the terminal:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/franciscomendes/Desktop/tinny/node_modules/milliparsec/dist/index.js
require() of ES modules is not supported.
require() of /Users/franciscomendes/Desktop/tinny/node_modules/milliparsec/dist/index.js from /Users/franciscomendes/Desktop/tinny/src/app.ts 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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/franciscomendes/Desktop/tinny/node_modules/milliparsec/package.json.

    at new NodeError (node:internal/errors:329:5)
    at Module._extensions..js (node:internal/modules/cjs/loader:1109:13)
    at require.extensions..jsx.require.extensions..js (/private/var/folders/np/ycmnjy5d4y11shfjzgr981qm0000gn/T/ts-node-dev-hook-8114142920895264.js:114:20)
    at Object.nodeDevHook [as .js] (/Users/franciscomendes/Desktop/tinny/node_modules/ts-node-dev/lib/hook.js:63:13)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (/Users/franciscomendes/Desktop/tinny/src/app.ts:3:1)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
[ERROR] 22:42:18 Error: Must use import to load ES Module: /Users/franciscomendes/Desktop/tinny/node_modules/milliparsec/dist/index.js
require() of ES modules is not supported.
require() of /Users/franciscomendes/Desktop/tinny/node_modules/milliparsec/dist/index.js from /Users/franciscomendes/Desktop/tinny/src/app.ts 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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/franciscomendes/Desktop/tinny/node_modules/milliparsec/package.json.

Hello.

I assume you're working with ts + commonjs build? The quick & dirty solution would be installing milliparsec@1.0.4, as that was the last version to be having a cjs build. Other than that, you should migrate your app to ESM😅

@FranciscoMendes10866 hi,

the solution is to set target and module to ESNext in tsconfig and use something that can run ts+esm (or u can run tsc && node ./dist/index.js)

for example u can use -r ts-node/esm or esmo