Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6208

React Native unable to resolve module from file

$
0
0

I'm trying to store all the CSS style values into one single ts file: styles/base.ts, and export those values in styles/index.ts file.
But when I try to use import the value from index.ts in my App.tsx file, it says

Unable to resolve module "styles/index" from "App.tsx": "styles/index" could not be found within the project.`.

I'm not sure is it a correct way to manage the styles inside application. Can anyone help me?

Here is how my base.ts file looks like:

export const colors = {
  grey: "#E3E1D6",
  black: "#333333",
};

index.ts:

import * as BaseStyle from "./base";
export {BaseStyle};

App.tsx:

import React from "react";
import {StyleSheet, View, Text} from "react-native";
import {BaseStyle} from "styles/index";
const App = () => {
  return (
    <>
      <View style={styles.body}>
        <Text>Test</Text>
      </View>
    </>
  );
};
const styles = StyleSheet.create({
  body: {
    backgroundColor: BaseStyle.colors.grey,
  },
});
export default App;

And here is my package.json looks like:

{
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.9.0",
    "react-native": "0.61.4"
  },
  "devDependencies": {
    "@babel/core": "^7.6.2",
    "@babel/runtime": "^7.6.2",
    "@react-native-community/eslint-config": "^0.0.5",
    "@types/jest": "^24.0.18",
    "@types/react-native": "^0.60.22",
    "@types/react-test-renderer": "16.9.0",
    "babel-jest": "^24.9.0",
    "jest": "^24.9.0",
    "metro-react-native-babel-preset": "^0.56.0",
    "react-test-renderer": "16.9.0",
    "typescript": "^3.6.3"
  },
  "jest": {
    "preset": "react-native",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  }
}

Viewing all articles
Browse latest Browse all 6208

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>