Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript Module not found Can't resolve

I'm building a react + typescript app in which I created a module with interfaces that are used all around my project's classes. My IDE resolves these interfaces fine but webpack always sends the following error. I tried different things but can't get that one to go away.

Any help would be greatly appreciated

ERROR in ./assets/src/Pages/Login.tsx Module not found: Error: Can't resolve 'seeyouftp' in 'var/www/app/assets/src/Pages'
 @ ./assets/src/Pages/Login.tsx 43:18-38
 @ ./assets/src/Config/App.tsx
 @ ./assets/entries/bundle.js

My definition file is here

|— definitions
     |— types.d.ts
|— entries
|— fonts
|— less
|— src

Excerpt of my definition file

declare module 'seeyouftp' {
  interface User {
    admin: boolean;
    roles: string[];
    username: string;
  }

  enum AuthStates {
    success = 'success',
    error = 'error'
  }

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "allowUnreachableCode": false,
    "baseUrl": "./assets",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "react",
    "lib": [
      "dom",
      "es2019",
      "esnext"
    ],
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "outDir": "./dist/",
    "resolveJsonModule": true,
    "skipDefaultLibCheck": true,
    "sourceMap": true,
    "strictPropertyInitialization": false,
    "strictNullChecks": true,
    "target": "es5",
    "typeRoots": [
      "./assets/definitions/types.d.ts",
      "./node_modules/@types"
    ],
    "types": [
      "node"
    ]
  },
  "include": [
    "./assets/src/**/*",
    "./assets/definitions/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

I import the created interfaces like so:

import { Item, PlayableMedia } from 'seeyouftp';

like image 603
titiyoyo Avatar asked Jun 20 '20 08:06

titiyoyo


People also ask

How does TypeScript resolve modules?

How TypeScript resolves modules. TypeScript will mimic the Node. js run-time resolution strategy in order to locate definition files for modules at compile-time. To accomplish this, TypeScript overlays the TypeScript source file extensions ( .

Can not find module TS?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.

What is declare module in TypeScript?

The TypeScript declares module is one of the modules and keyword it is used for to surround and define the classes, interfaces; variables are also declared it will not originate with the TypeScript like that module is the set of files that contains values, classes, functions/methods, keywords, enum all these contains ...

How does typescript resolve modules?

How TypeScript resolves modules #. TypeScript will mimic the Node.js run-time resolution strategy in order to locate definition files for modules at compile-time. To accomplish this, TypeScript overlays the TypeScript source file extensions (.ts, .tsx, and .d.ts) over Node’s resolution logic.

Why does typescript say cannot find module when importing fonts?

It happens because of module resolution. You need to declare the font file formats as modules so that TypeScript can understand and parse them correctly. TypeScript relies on definition files ( *.d.ts) to figure out what an import refers to. When that information is missing, you get the "Cannot find module" error.

How do I resolve import and export problems in typescript?

If you are having resolution problems with import s and export s in TypeScript, try setting moduleResolution: "node" to see if it fixes the issue. This used to be TypeScript’s default resolution strategy. Nowadays, this strategy is mainly present for backward compatibility. A relative import will be resolved relative to the importing file.

What is typescript run-time resolution?

TypeScript will mimic the Node.js run-time resolution strategy in order to locate definition files for modules at compile-time. To accomplish this, TypeScript overlays the TypeScript source file extensions ( .ts, .tsx, and .d.ts) over Node’s resolution logic.


3 Answers

The error message was actually very misleading, and looks like a typescript bug. It appears that enums can't be exported directly, it seems necessary to use a const to be able to export them correctly.

So I modified my enum declaration like so

declare module 'seeyouftp' {

  // exporting a const instead of 
  export const enum AuthStates {
    success = 'success',
    error = 'error'
  }
}

Everything works now but that error message is very, very bad and time consuming

like image 117
titiyoyo Avatar answered Oct 24 '22 08:10

titiyoyo


Try to export the declarations, and see if that makes a difference:

declare module 'seeyouftp' {
export  interface User {
    admin: boolean;
    roles: string[];
    username: string;
  }

export  enum AuthStates {
    success = 'success',
    error = 'error'
  }
like image 37
Ivan V. Avatar answered Oct 24 '22 07:10

Ivan V.


Because it is a webpack issue, you have to update your webpack config. Webpack needs to be told where to find the module seeyouftp:

// webpack.config.js
const path = require('path');

module.exports = {
  //...
  resolve: {
    // Add an alias
    alias: {
      'seeyouftp': path.resolve(__dirname, 'definitions/types.d.ts')
    }
  }
};

For the path to the file types.d.ts I assumed your webpack configuration is located next to the definitions folder.

Here is the associated webpack documentation.

like image 1
Baboo Avatar answered Oct 24 '22 07:10

Baboo