ts-node not able to resolve ~ or node imports

I've got some scripts written in TypeScript in my project which I would like to execute. After some digging ts-node seams to be the obvious choice. However neither the ~ nor the import from node itself were working. After some digging I found that I can fix the node imports by either removing the types key from the tsconfig compilerOptions altogether or adding node to the array. Regarding the ~ paths however I'm stuck. I tried adding the tsconfig-paths package which doesn't seam to work at all, tried adding imports to the package.json and switching to a #root prefix instead. Nothing seams to help and I'm stuck with the annoying error message:
CustomError: Cannot find package '~' imported from /home/bikeshedder/projects/myproject/src/scripts/gen_schema.ts
CustomError: Cannot find package '~' imported from /home/bikeshedder/projects/myproject/src/scripts/gen_schema.ts
3 Replies
Michael P. Jung
Michael P. JungOP3y ago
Does anyone know how I can make this work. I'd like to write scripts in the src/scripts directory that can import from the rest of the project which uses ~ imports. Solved it. I simply removed ts-node and use @digitak/esrun instead. It just works. No extra configuration needed.
Michael P. Jung
Michael P. JungOP3y ago
Update: I switched to using the tsx package (https://www.npmjs.com/package/tsx) as it seams to be more active than all the other alternatives and it works like a charm.
npm
tsx
TypeScript Execute (tsx): Node.js enhanced with esbuild to run TypeScript & ESM files. Latest version: 3.12.1, last published: a month ago. Start using tsx in your project by running npm i tsx. There are 30 other projects in the npm registry using tsx.
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?