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

Batteries cannot be imported with "module": "NodeNext" #190

Open
cspotcode opened this issue Feb 3, 2023 · 4 comments
Open

Batteries cannot be imported with "module": "NodeNext" #190

cspotcode opened this issue Feb 3, 2023 · 4 comments

Comments

@cspotcode
Copy link

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

@Schniz
Copy link
Owner

Schniz commented Feb 3, 2023

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

I had this issue too when using .mjs

@cspotcode
Copy link
Author

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.

@Schniz
Copy link
Owner

Schniz commented Feb 3, 2023

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

@cspotcode
Copy link
Author

cspotcode commented Feb 3, 2023

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",

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

No branches or pull requests

2 participants