Schniz/cmd-ts

Batteries cannot be imported with `"module": "NodeNext"`

cspotcode opened this issue · 4 comments

import {File} from 'cmd-ts/batteries/fs';

Can be imported when using tsconfig "module": "CommonJS", but in "NodeNext" it breaks with the error: Cannot find module 'cmd-ts/batteries/fs' or its corresponding type declarations.

I'm not sure if this is an issue with how cmd-ts uses a package.json to redirect to dist, or if it's a bug in TypeScript's resolver. I'm using TS 4.9.5

I think I should declare “exports” in the root package.json

I had this issue too when using .mjs

I tried using tsc --traceResolution to see what tsc is doing.

Loading module 'cmd-ts/batteries/fs' from 'node_modules' folder, target file type 'JavaScript'.
File '/my-project/node_modules/cmd-ts/batteries/fs/package.json' exists according to earlier cached lookups.
'package.json' has 'main' field 'dist/cjs/index.js' that references '/my-project/node_modules/cmd-ts/batteries/fs/dist/cjs/index.js'.
Loading module as file / folder, candidate module location '/my-project/node_modules/cmd-ts/batteries/fs/dist/cjs/index.js', target file type 'JavaScript'.
File name '/my-project/node_modules/cmd-ts/batteries/fs/dist/cjs/index.js' has a '.js' extension - stripping it.
File name '/my-project/node_modules/cmd-ts/batteries/fs/index.js' has a '.js' extension - stripping it.
File '/my-project/node_modules/cmd-ts/batteries/fs/index.js' does not exist.
File '/my-project/node_modules/cmd-ts/batteries/fs/index.jsx' does not exist.
======== Module name 'cmd-ts/batteries/fs' was not resolved. ========

In particular, this line:

'package.json' has 'main' field 'dist/cjs/index.js' that references '/my-project/node_modules/cmd-ts/batteries/fs/dist/cjs/index.js'.

It looks like TS is stripping the ../../ prefix from main.

I wonder how does that work out for fp-ts

maybe the double nested path?

https://unpkg.com/browse/fp-ts@2.13.1/Either/package.json

I'm seeing that it also fails for fp-ts:

EDIT: I found the issue: gcanti/fp-ts#1726

Loading module 'fp-ts/Either' from 'node_modules' folder, target file type 'JavaScript'.
File '/my-project/node_modules/fp-ts/Either/package.json' exists according to earlier cached lookups.
'package.json' has 'main' field './lib/index.js' that references '/my-project/node_modules/fp-ts/Either/lib/index.js'.
Loading module as file / folder, candidate module location '/my-project/node_modules/fp-ts/Either/lib/index.js', target file type 'JavaScript'.
File name '/my-project/node_modules/fp-ts/Either/lib/index.js' has a '.js' extension - stripping it.
File name '/my-project/node_modules/fp-ts/Either/index.js' has a '.js' extension - stripping it.
File '/my-project/node_modules/fp-ts/Either/index.js' does not exist.
File '/my-project/node_modules/fp-ts/Either/index.jsx' does not exist.
======== Module name 'fp-ts/Either' was not resolved. ========
'package.json' has 'main' field './lib/index.js' that references '/my-project/node_modules/fp-ts/Either/lib/index.js'.

But it's supposed to be:

"main": "../lib/Either.js",