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

Cannot find module '@uni' or its corresponding type declarations. #4399

Open
giapdong opened this issue May 18, 2024 · 6 comments
Open

Cannot find module '@uni' or its corresponding type declarations. #4399

giapdong opened this issue May 18, 2024 · 6 comments

Comments

@giapdong
Copy link

giapdong commented May 18, 2024

I am building multiple apps with shared resources and here is my folder structure
image

⛳ In this way, I want to use uni for both app-a and app-b.

Here is sample for app-a when using it below
app-a/tsconfig.json

{
	"extends": "@vue/tsconfig/tsconfig.dom.json",
	"include": ["../uni/**/*"],
	"exclude": ["../uni/**/__tests__/*"],
	"compilerOptions": {
		"moduleResolution": "bundler",
		"baseUrl": ".",
		"paths": {
			"@uni": ["../uni/uni"],
		}
	}
}

app-a/vite.config.ts

import path from 'node:path';
import { fileURLToPath, URL } from 'node:url';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';

// https://vitejs.dev/config/
export default defineConfig({
	plugins: [
		vue(),
	],
	resolve: {
		alias: {
			'@': fileURLToPath(new URL('./src', import.meta.url)),
			'@uni': fileURLToPath(new URL('../uni/uni', import.meta.url)),
		},
	},
})

app-a/package.json

{
	"scripts": {
		"dev": "vite",
		"build": "run-p type-check \"build-only {@}\" --",
		"preview": "vite preview",
		"build-only": "vite build",
		"type-check": "vue-tsc --build --force"
	},
}

app-a/src/App.vue

<template>
	<div></div>
</template>

<script lang="ts" setup>
import uni from '@uni'
</script>

With this way, I see minor error in app-a/src/App.vue:
image

It works well for both the dev and build-only. It only a typescript check, do not affect or crash app

But when i using command 'type-check' it make really error as this issue title
image

So, how i can resolve it? I really want to using type-check for safe check but with this way i must to ignore to finish build process :((
btw, if this issue done, it will affect to Volar? I don't want to see error during development time :(
Thank you

@RayGuo-ergou
Copy link
Contributor

RayGuo-ergou commented May 19, 2024

You should add the alias to the tsconfig that includes your app.

@giapdong
Copy link
Author

You should add the alias to the tsconfig that includes your app.

@RayGuo-ergou What do you mean? Because I included in tsconfig/include

@RayGuo-ergou
Copy link
Contributor

You only include ../uni/**/*, so typescript server dose not know that app-a and app-b can also use @uni alias.

@giapdong
Copy link
Author

You only include ../uni/**/*, so typescript server dose not know that app-a and app-b can also use @uni alias.

I edited my issue to be clearer. My problem appears at app-a (for example, app-b also). And I registered @uni for both tsconfig and vite.config.ts (you can re-see in my issue description).

I know that in that way it was tell to typescript that using @uni for app-a. So what is the wrong here?

@RayGuo-ergou
Copy link
Contributor

create tsconfig.json under app-a does not automatically include files in app-a you have to explicitly add to the tsconfig.

e.g.

- 	"include": ["../uni/**/*"],
+ 	"include": ["../uni/**/*", "./src/**/*"],

Both IDE error and typecheck error due to this.
Your build works because vite does not require typescript at all which mean it does not read tsconfig.json.

@giapdong
Copy link
Author

create tsconfig.json under app-a does not automatically include files in app-a you have to explicitly add to the tsconfig.

e.g.

- 	"include": ["../uni/**/*"],
+ 	"include": ["../uni/**/*", "./src/**/*"],

Both IDE error and typecheck error due to this. Your build works because vite does not require typescript at all which mean it does not read tsconfig.json.

Ah sure :D, I cutting include "./src/**/*" for reduce noise and only focus on uni module. Because I still using "dev" and "build-only" without error so it must include :D

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