17

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

3
  • Please include the code where you import seeyouftp Commented Jun 22, 2020 at 12:08
  • Is seeyouftp npm package or your file? if your file, where is it located? Commented Jun 22, 2020 at 12:57
  • seeyouftp is not an npm package. It's only the name of the module contained in my types.d.tsfile. It's declared like so: declare module 'seeyouftp' Commented Jun 22, 2020 at 13:07

4 Answers 4

25

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

Sign up to request clarification or add additional context in comments.

2 Comments

Just a random comment to say thank you so much for following up and answering your post. I ran into the same issue and had a hunch enum was the culprit, since it was the only file that wasn't importing properly from my TS project.
i don't think it's a bug as it is still resolvable to a types file but this variation is necessary for my project which exports eslint rules as javascript.
1

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

1 Comment

I tried exporting every interface in the module but it doesn't work either
1

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"
],

7 Comments

when you say 'I assum that seeyouftp is your js module name' do you mean the name field in package.json ?
The name you normally use to import with js. And one more thing need to be updated in tsconfig file. Please check my updated comment and try it.
I don't know what you mean by "The name you normally use to import with js". I import the types using this kind of import, is it what you mean ? import { Item, PlayableMedia } from 'seeyouftp';
Yes. So I think seeyouftp is correct name. Did you try to update tsconfig?
Yes, actually my tsconfig was already setup this way
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.