Reputation: 3974
I am trying to get into writing a discord bot, but I seem to have hit a snag with setting up typescript config / paths. Even though the compiler claims that it can find my file, the build command, npm run build, fails to find my module, and crashes.
my folder structure:
|src
>|startup
>|StartupManager.ts
|-app.ts
|-package.json
|-tsconfig.json
as there is a few moving parts here, I will outline what I have and the error I am trying to resolve:
tsconfig.json:
...
"baseUrl": ".",
"paths": {
"bot/*": [ "./src/*" ]
}
...
app.ts (the complaining file)
import { StartupManager } from 'bot/startup/StartupManager';
export { StartupManager };
new StartupManager();
my build command:
"build": "tsc --build && tsc app.ts && node app.js",
In StartupManager:
import { ILogger, Logger } from 'bot/core/logger';
export class StartupManager {
...
}
Currently, I am working on Visual Studio 2019 Community Edition. To re-iterate, My Text Editor shows that everything is fine, but the moment the build kicks off, I am greeted with:
app.ts:1:32 - error TS2307: Cannot find module 'bot/startup/StartupManager'.
1 import { StartupManager } from 'bot/startup/StartupManager';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error.
Any advice anyone can lend me?
Upvotes: 0
Views: 35
Reputation: 3658
From this page in the Typescript Handbook: When input files are specified on the command line, tsconfig.json files are ignored.
So when you run tsc app.ts
it does not know about the paths property set in tsconfig.json
. Probably what you want to do is run only tsc --build
, because that compiles the files including app.ts
.
Regarding the paths
property, it may still be necessary to install an additional package to run .js files that use the shorthand paths in the module imports. There are NPM packages like tsconfig-paths that can help with this.
Upvotes: 1