knip icon indicating copy to clipboard operation
knip copied to clipboard

Support ESM subpath imports

Open lucasrmendonca opened this issue 1 year ago • 5 comments

It would be cool if Knip could support NodeJs ESM Subpath Imports such as #* out-of-the-box without having to declare the subpaths again in Knip's paths setting. Some other tools such as TypeScript, Vite, Webpack, and Jest already support it from the get-go by reading the package.json's imports setting so if a user has the subpaths already declared in package.json they don't need to re-configure it in any other tools.

lucasrmendonca avatar Mar 27 '24 01:03 lucasrmendonca

They're supported. But there could be a bug. What's the issue you're seeing?

A common pitfall is that the main or subpath imports point to ignored/dist files, which aren't added to the analysis by Knip.

webpro avatar Mar 27 '24 05:03 webpro

I have the following setup:

files:

package.json
📁 src
|__ firstFile.js
|__ secondFile.js

package.json

{
  "name": "knip-issue-576",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "knip": "knip",
    "main": "node \"./src/firstFile.js\""
  },
  "imports": {
    "#*": "./*"
  },
  "devDependencies": {
    "@types/node": "^20.11.30",
    "knip": "^5.6.1",
    "typescript": "^5.4.3"
  }
}

src/firstFile.js

import secondFile from "#src/secondFile.js";

console.log(secondFile.text);

src/secondFile.js

const text = "hello world!";

export default { text };

Output when running npm run knip:

Unused files (1)
src/secondFile.js
Unresolved imports (1)
#src/secondFile.js  src/firstFile.js:1:23

Expected output:

✂️  Excellent, Knip found no issues.

I am currently working around this by adding the following knip.json config file, but it would be better if it wasn't needed:

knip.json

{
  "paths": { "#*": ["./*"] }
}

lucasrmendonca avatar Mar 28 '24 00:03 lucasrmendonca

I remember #* and #/* aren't valid aliases. Maybe TypeScript accepts them, but the Node.js runtime does not.

Can you try valid aliases and see if that works better?

Also see:

  • https://nodejs.org/api/packages.html#subpath-imports
  • https://www.webpro.nl/articles/using-subpath-imports-and-path-aliases

webpro avatar Mar 28 '24 06:03 webpro

Are you sure it isn't just #/* that's invalid?

I've been using #* in some pretty large-scale Node.js applications without TypeScript and never had any problems with it. I've also read through both of the reference links you provided and found no indication of #* being invalid either.

You can run the npm run main script in the setup from the example above (which has no .ts or .tsconfig files and only references typescript in the package.json due to Knip's peer dependency) and see that Node.js outputs the "hello world" string as expected... as opposed to the TypeError [ERR_INVALID_MODULE_SPECIFIER] that shows up when we use an invalid alias

lucasrmendonca avatar Mar 29 '24 02:03 lucasrmendonca

Are you sure it isn't just #/* that's invalid?

No, I haven't actually used this alias myself.

Could you please provide a reproduction of the issue, so I can look into it. I don't have the bandwidth to set that up myself for everyone.

webpro avatar Mar 29 '24 11:03 webpro

Closing due to inactivity.

webpro avatar May 05 '24 06:05 webpro