How to handle path imports in typescript using babel?
See original GitHub issueIn my tsconfig
I have this:
"paths": {
"@app/*": ["public/js/app/*"]
and in my codebase, I use the imports like:
import "@app/test"
Everything works fine. But I’m trying to do server side rendering in Node and tried to import the typescript component. My babelrc looks like:
{
"presets": [
"@babel/preset-env"
"@babel/react",
"@babel/preset-typescript"
],
"plugins": ["@babel/plugin-proposal-class-properties",
"transform-class-properties","@babel/plugin-transform-modules-commonjs",
["module-resolver", {
"alias": {
"root" : ["./public/js/app"],
"@app": ["public/js/app"],
"extensions": [".tsx",".ts"],
"loglevel": "warn",
}
}]],
"compact": true
}
And I have my node code registered with babel-register, however I do get error like
Error: Cannot find module '@app/test'
Looks like paths are not getting resolved. How can I solve this issue? Am I missing something here?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:14
- Comments:14 (3 by maintainers)
Top Results From Across the Web
Using absolute path import with Typescript + babel + ...
You can do this with just babel-plugin-module-resolver , and tsconfig.json or @babel/preset-typescript . There are 2 ways to use absolute path ...
Read more >Documentation - Using Babel with TypeScript
This technique is a hybrid approach, using Babel's preset-typescript to generate your JS files, and then using TypeScript to do type checking and...
Read more >babel/preset-typescript
When set to true , the transform will only remove type-only imports (introduced in TypeScript 3.8). This should only be used if you...
Read more >How do I configure absolute paths for imports in TypeScript ...
How To · Install babel-plugin-module-resolver using npm or yarn. npm i babel-plugin-module-resolver --save-dev # Or (If you're using yarn): yarn ...
Read more >Using absolute paths with TypeScript, Babel and Browserify
Configure TypeScript to use non-relative paths. · Use TypeScript to transpile the code to ES6. · Configure Babel to transform the paths. ·...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For anyone who uses TypeScript and just wants to use import with absolute paths without aliases.
Assuming all of your code folders are inside of
src
.Insert
"baseUrl": "src"
incompilerOptions
object insidetsconfig.json
.Now you can use absolute paths in imports.
If you already use typescript, you can drop any babel plugins for these resolutions and only use the typescript system.