Home > Back-end >  TS2307 Error: Cannot find module '../constants' or its corresponding type declarations
TS2307 Error: Cannot find module '../constants' or its corresponding type declarations

Time:10-02

I am currently getting the error while trying to import. Any help or suggestions will be greatly appreciated.

tabs.tsx | src>navigation>tabs.tsx

import React from 'react'
import { StyleSheet, View, Image, Text } from 'react-native'
import {createBottomTabNavigator, BottomTabBar} from "@react-navigation/bottom-tabs"
import {icons} from '../constants'

const Tab = createBottomTabNavigator()

Structure

enter image description here

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "lib": ["es2017"],                        /* Specify library files to be included in the compilation. */
    "allowJs": true,                          /* Allow javascript files to be compiled. */
    "jsx": "react-native",                    /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    "noEmit": true,                           /* Do not emit outputs. */
    "isolatedModules": true,                  /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    "strict": true,                           /* Enable all strict type-checking options. */
  
    "moduleResolution": "node",               /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "allowSyntheticDefaultImports": true,     /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "skipLibCheck": false,                    /* Skip type checking of declaration files. */
    "resolveJsonModule": true                 /* Allows importing modules with a ‘.json’ extension, which is a common practice in node projects. */

  },
  "exclude": [
    "node_modules", "babel.config.js", "metro.config.js", "jest.config.js"
  ]
}

CodePudding user response:

The error tells you there's nothing to import from the location it's currently pointed at.

import-ing from a folder actually looks for a index.[d.](t|j)s file in that folder. If that file exists and it has a default export, you can assign the export a name in local scope, at import:

import stuff from '../constants'

If it contains named exports you can import them using brackets:

import { myExport } from '../constants' //or 
import { myExport as localName } from '../constants'

Note: the above implies your index.ts contains something like:

export const myExport = (/* some expression */)
// you can export types, consts, functions, classes, interfaces, etc...

If you want to import all named exports from that file, use *:

import * from '../constants' // myExport is now usable
// or namespaced:
import * as local from '../constants' // (and use as local.myExport)

More examples here.
Spec here.

If you placed a file named icons.ts, having a default export, in /constants folder, your import should look like this:

import icons from '../constants/icons'
// or, for a named export
import { icons } from '../constants/icons'
  • Related