squirrelchat/smol-toml

Cannot import package

Closed this issue · 5 comments

Hocdoc commented

Hello,

Your TOML library looks very promising to me.

Unfortunately I cannot import your library into any of my projects after I've installed it with npm i smol-toml:

import { parse } from "smol-toml";

=> error TS2307: Cannot find module 'smol-toml' or its corresponding type declarations.

Maybe there is an issue with the packaging?

Hi, that's indeed a problem!

What version of TypeScript are you using? Is the library actually impossible to import or is the problem only related to typing information?

Actually, I've just tried and I seem to be able to reproduce this using stock TS config. By specifying moduleResolution to node16 (or nodenext), TypeScript will actually resolve the types using my package.json's exports specification solving this issue.

This is not ideal, and considering I have a way to avoid this issue completely I'll change that in a future release. In the meantime, using the workaround described above should work.

Hocdoc commented

Sorry for my late response and thanks for your moduleResolution-tip!

You need to add "types": "./dist/index.d.ts" and "main": "./dist/index.js" to the package.json (Reference).

TypeScript added support for NodeJS's exports field in TypeScript 4.7 and does correctly look up the types specified in this. main is unrelated to TypeScript and is useless in this context.

Realistically, failure to resolve the package's types is an indication of either an outdated TypeScript installation, or an improper configuration as I described above. While adding types at the root of the package is currently possible, it is only "accidental" and does not fully reflect the true Node Resolve Algorithm used.

References: