Reputation: 942
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';
Upvotes: 17
Views: 27921
Reputation: 942
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
Upvotes: 25
Reputation: 4258
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.
Upvotes: 1
Reputation: 3488
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"
],
Upvotes: 1
Reputation: 8081
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'
}
Upvotes: 1