question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to handle path imports in typescript using babel?

See original GitHub issue

In 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:open
  • Created 4 years ago
  • Reactions:14
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

15reactions
8ofcommented, May 20, 2020

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" in compilerOptions object inside tsconfig.json.

Now you can use absolute paths in imports.

11reactions
tleunencommented, May 29, 2020

If you already use typescript, you can drop any babel plugins for these resolutions and only use the typescript system.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found