I created a React Native project with Expo.
I have the MainButton.tsx file inside /components folder like this:
import { View, Text } from "react-native";
const MainButton = () => {
return (
...
);
};
export default MainButton;
Now in the index.tsx file in the app folder I want to import this custom button like this:
import { MainButton } from "../components";
But it shows the error: Cannot find module '../components' or its corresponding type declarations. How do I fix this? Should I modify the tsconfig.json file?
The import should be updated to:
import MainButton from "../components/MainButton";
if you want to use directory imports like imports like import { MainButton } from "../components"; without specifying the filename, set up module aliases in the tsconfig.json file.
In your tsconfig.json, add or modify the compilerOptions section to include paths.
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@components/*": ["components/*"]
}
}
}
After adding module aliases, you can update the import to:
import MainButton from "@components/MainButton";
Restart the Expo server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With