-
Notifications
You must be signed in to change notification settings - Fork 12.8k
npm-linked modules (duplicates or not) are not working. #31527
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
Comments
I think the solution involves TypeScript treating linked modules as if they are not linked modules (if it doesn't already), and use a top-down module resolution (file traversal) so that it will pick the top-most libraries from which to get dependencies (types) from. If there's a dependency constraint that can not be matched, then it will use a lower-in-the-tree module (just like NPM) to get types from, and get those types only for the package the needs those types from that specific version (regardless if the types are This would essentially work the same way as NPM's flattening approach, where it flattens everything unless there are dependency forks (i.e. version ranges can't be satisfied for two or more packages that need incompatible versions of the same lib), except TypeScript won't do any flattening, TypeScript will simply trust the flattened structure that NPM will already have put in place. |
I meant to put I'm on TS v3.4.5 up there. Edited. |
The workaround I mentioned, deleting the dependency in the linked module, doesn't work in this case (I thought it did before, but, now I'm not sure). Now I get
As in my suggestion, if TypeScript were to treat the linked module as if it were not linked, and continued to traverse up the tree in the main project where tsc is running from, then it would find |
However, because we don't want to delete the dependencies in the linked modules (so that we can also go and work locally inside the linked modules when making changes to those modules), this is why I suggested a top-down approach which should ultimately result in the same modules being resolved as with NPM's flattening when no modules are linked). Note, when modules are linked, NPM's resolution is bottom-up, and that can introduce other problems, which is something I wish NPM would fix. |
EDIT: The workaround does work: I was deleting The top-down idea still holds though, I think an approach like that would work. |
I just experienced issues again today. As an example, I have a In npm install
npm link
npm run build # generates a dist/ folder After doing this Then in project-a I ran npm install # installs project-b (and its dependencies) from NPM Now
Now I want to work with a local version of npm link project-b # link the local version of project-b into project-a Now
Now what will happen is that TypeScript will give errors like the following:
So it appears that when TypeScript is looking up type definitions, it picks up the ones in The problem can be exacerbated when linking more than one package, and even more so when for example linking a The solution thoughts:I believe TypeScript should default to picking the types from a package which is the top-most package in node_modules. So, from perspective of |
I'm struggling with this. This makes Can I ask, have any of you used |
Is this a regression of #16274 ? |
Are you using [email protected]? I think #31541 should have fixed this. If not can you create sample repro so we can investigate this. |
Alright, I'll report back when I give it a go. |
@trusktr I have faced what I think is this same issue.. I was able to work around it by providing a paths configuration for the module in question, and setting
I am using office-ui-fabric react, and have an imported library with customized components. |
@sheetalkamat I've been on TS 3.7.2 lately, and still encounter issues regarding duplicates. Unfortunately, it hasn't happened on a public repo that I can share yet. |
@trusktr if you need to, you can also share repro code privately at shkamat at Microsoft dot com and after we sign nda. Let me know if that would work to get repro. Unfortunately without repro we cannot investigate this further. |
Ok, thanks, let me chat with someone higher up to see if I can get an NDA approved. In the meantime, I'm getting an error like this, for example:
From that output, it seems that the type system is referring to the same type in two ways: one way is with node-style module resolution, and the other way is with a relative path. When I run
and it is labeled as "invalid" (not sure why, but maybe that has to do with it). I obfuscated the names for this example. After checking all of the import statements in the project, they're all of the form |
Just to re-confirm, if I install the exact same package from git instead of using The only difference between the linked module and the git module, is that due to
the git-installed version does not have any of the extra files (f.e. the npm linked version has things like tsconfig.json, and other files normally used in dev mode). Not sure if this has anything to do with it. I'll do a test... |
Alright, after testing, if I manually create a symlink to the package, in node_modules, and then delete all files in it except the ones that are listed in The file structure in |
you've found a solution or an workaround? |
I'm also having problems with "npm link" and TS, however for me, TS seems to not be able to work with the symlinked module at all ... Anyways, concerning this issue: I don't think it's TS responsibility to manage "duplicates". The thing is, that the concept of "npm link" here already is flawed. It's just not the same thing to have a package being regularly installed with "npm install" and having dependencies flattened correctly, compared with arbitrarily changing the structure of node_modules when doing "npm link". For me, it is perfectly clear that TS just follows the usual node algorithm of including the nearest match (inside node_modules) ... and duplicate types in this case are a problem, because it has to be assumed that the duplicated dependency ends as a duplicate at runtime ... well and then it's really two different types ... e.g. try using To summarize: I almost forgot: the workaround I'm using for the whole |
I'm using |
Re:
Search Terms
"typescript ignore npm linked dependencies"
Suggestion
I'd like for TypeScript to automatically work with
npm link
ed modules.Use Cases
Prevents developers from wasting much time.
Examples
npm link
anything, and it just works.Checklist
My suggestion meets these guidelines:
More details
I'm on TS
v3.4.5
.Even if a project's
npm link
ed module has the exact same version of a dependency,TypeScript complains. For example:
This is quite problematic, especially when needing to link 3, 4, or 5 modules at once during development, and especially more so when those linked modules have other
npm link
ed modules. This issue grinds the dev experience to a halt. Thisnpm link
workflow is commonplace in with plain JS projects using NPM.I've tried all workarounds mentioned in the other four issues, but each workaround brings with it other problems, or just don't work.
One workaround is to remove the duplicate dependency in the
npm link
ed module, and this causes everything to work fine in the project, but then typings don't work while working on thenpm link
ed module. So to get back to developing thenpm link
ed module, we need to reinstall the missing dependencies. Then when we switch back to the project we need to delete the dependencies in the linked package. Then when we switch back to the linked package... etc...I believe that TypeScript should deeply embrace how
npm link
works, and provide a built in solution so that TypeScript users never run into this again.The text was updated successfully, but these errors were encountered: