auto import in vscode for lib exports not working for new project without explicit include in root tsconfig
See original GitHub issueCurrent Behavior
For a new empty project with a nestjs app and a workspace lib called “utils”, when typing the name of an exported function from @org/utils
(for example exportedExampleA
) in a file within the nestjs app I do not get an auto import suggestion from vscode’s IntelliSense.
Expected Behavior
If I’m typing exportedExampleA
in a file within the nestjs app, I would expect vscode to suggest an auto import like import { exportedExampleA } from '@org/utils'
so that I can select it without me having to type in the import manually.
Steps to Reproduce
nx g @nrwl/nest:application webapi
nx g @nrwl/workspace:library utils
// libs/utils/src/index.ts
export function exportedExampleA() {
return 'lol';
}
// apps/webapi/src/main.ts
...
exportedExampleA // type this and expect auto import suggestion
Failure Logs
N/A.
Environment
OS: macOS 10.15.5 vscode: 1.47.1 nx: latest
Workaround
Add the following to the monorepo’s root tsconfig.json
and restart vsc:
{
...
"include": [
"./libs/*/src/**/*",
"./apps/*/src/**/*"
]
}
Auto import now works as expected.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:13 (2 by maintainers)
Top Results From Across the Web
typescript auto imports not working macos - Stack Overflow
In your case, typescript is unable to locate all the files in your project. To locate all the files, modify your include array...
Read more >jsconfig.json Reference - Visual Studio Code
js doesn't reference a file b.ts explicitly (either using import or CommonJS modules), there is no common project context between the two files....
Read more >Enable JavaScript auto-import suggestions in VS Code
Now that VS Code knows to treat all your JS files as an explicit project, auto-import suggestions will populate with all your relevant...
Read more >TypeScript | IntelliJ IDEA Documentation - JetBrains
In the Settings/Preferences dialog ( Ctrl+Alt+S ), go to Editor | General | Auto Import. The Auto Import page opens. In the TypeScript/ ......
Read more >eslint-import-resolver-typescript - npm
Start using eslint-import-resolver-typescript in your project by running `npm ... If you're facing some problems on rules import/default or ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Unfortunately, we cannot add “include” into the root tsconfig because it is inherited by all the projects. They have to be independent from each other.
Tollowing works:
Add the following to the lib:
Add the following the app:
Now when type in “funcB”, vscode will autocomplete it. Basically as long as your app imports mylib anywhere in its source, the autocompletion from mylib will work. Otherwise it won’t. You can “fix” it manually by adding path to the lib to tsconfig.app.json of your app, but I personally would not do it. This is a limitation of how vscode autocompletion works. The language service has to know that myapp points to mylib before it can autocomplete tokens from it. The same is true about autocompleting tokens from node_modules.
As a side note, WebStorm doesn’t have this issue because their autocompletion doesn’t rely on the language service.
I don’t think this issue can be fixed on our side, so I’m going to close it.
Just in case anyone else is stumbling through this, I have pretty much a blank NX workspace that I created yesterday with 1 barebones angular app and 1 barebones nestjs app. Both were generated via the NX CLI. Doing these 2 things fixed the issue for me:
node_modules
from theexcludes
property intsconfig.base.json
(only doing this did not fix the issue for me like others have suggested)"../../node_modules/@angular/**/*.d.ts"
to theinclude
array intsconfig.editor.json
for your angular app. You may be able to add it intsconfig.json
ortsconfig.app.json
, but based on file names alone, I figured it was more appropriate to place in the editor json since this is an editor related issue.