简体   繁体   中英

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';

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

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'
  }

You need to create seeyouftp (I assum that seeyouftp is your js module name) folder under /definitions and have types.d.ts inside /definitions/seeyouftp like structure below

|— definitions
 |—seeyouftp
  |— index.d.ts
|— entries
|— fonts
|— less
|— src

And update your tsconfig

"typeRoots": [
  "./assets/definitions",
  "./node_modules/@types"
],

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM