In my NodeJS/Typescript project, I created a script database-init.script.ts
that I run to initialize the database with sample data.
My package.json looks like this:
{
"name": "myproject",
....
"scripts": {
...
"mongodb-setup": "tsc ./scripts/mongodb-setup.ts && node ./scripts/mongo-setup.js"
}
The problem is that the script imports some files that themselves utilize tsconfig paths
shortcuts. For example some files have
import {AccountSchema} from '@schemas/account.schema'
...
My script fails because it doesn't recognize these. It gives me
error TS2307: Cannot find module '@schemas/account.schema' or its corresponding type declarations.
1 import {AccountSchema} from '@schemas/account.schema";
How can I get a script to recognize tsconfig-defined paths, without compiling the entire project?
It seems you are looking for ts-node.
{
"name": "myproject",
....
"devDependencies": {
...
"ts-node": "*"
},
"scripts": {
...
"mongodb-setup": "node -r ts-node/register ./scripts/mongo-setup.ts"
}
}