Code Monkey home page Code Monkey logo

Comments (22)

resolritter avatar resolritter commented on May 11, 2024 35

I've tried other solutions in this thread, in addition to trying to work around the issue with "@babel/register". It did not work.

What worked for me was to edit metro.config.js and use extraNodeModules.


Since I have this in tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "."

And I want to import from "[root]/src" as "src/...", here's my full metro.config.js configuration:

const path = require("path")

module.exports = {
  resolver: {
    extraNodeModules: {
      "src": path.resolve(__dirname, 'src'),
    }
  },
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
}

If your module still isn't being found, try tweaking sourceExts.

from react-native-template-typescript.

solkaz avatar solkaz commented on May 11, 2024 10

The solution to this is to add babel-plugin-module-resolve and then mimic the baseUrl/paths setup in your Babel config to transform your import paths like TypeScript would.

@nkovacic for your configuration, your .babelrc should similar to this:

{
    "presets": ["module:metro-react-native-babel-preset"],
    "plugins": [
        [
            "module-resolver",
            {
                "root": ["./src"],
                "alias": {
                    "@assets/*": "assets/*"
                }
            }
        ]
    ]
}

from react-native-template-typescript.

dawsonc623 avatar dawsonc623 commented on May 11, 2024 9

This solution did not work for me. As far as I can tell, the babel.config.js file is not even being read in my environment; I put some gibberish at the bottom of the file, and the application still built and loaded (minus the fact this error came up).

I am on React Native 0.59.5 - I am not sure if that could explain the difference in behavior.

from react-native-template-typescript.

MR03web avatar MR03web commented on May 11, 2024 8

same question and I used solution of @solkaz, little different.

.babelrc

    [
      "module-resolver",
      {
        "alias": {
          "@kit": "./src/kit"
        }
      }
    ]

tsconfig.json

    "baseUrl": ".",
    "paths": {
      "@kit/*": ["src/kit/*"]
    }

from react-native-template-typescript.

apedroferreira avatar apedroferreira commented on May 11, 2024 5

For everyone for whom using babel-plugin-module-resolve didn't work and is using Expo:
Try running your project with expo start --clear to clear your Javascript transform cache, it worked for me.

from react-native-template-typescript.

afiouni avatar afiouni commented on May 11, 2024 3

The babel-plugin-module-resolver documentation shows how to use regular expressions to address this. So after some experimenting, this is a running example:

tsconfig.json

"baseUrl": ".",
    "paths": {
      "xyz": ["src/components/xyz/index"],
      "xyz/*": ["src/components/xyz/*"],
      "components/*": ["src/components/*"],
    }

babel.config.js

module.exports = {
  presets: ["module:metro-react-native-babel-preset"],
  plugins: [
    [
      "module-resolver",
      {
        alias: {
          "xyz": ["./src/components/xyz/"],
          "components/(.+)": "./src/components/\\1",
        }
      }
    ]
  ]
}

Adding a special path for index imports was the only way to make it work in my experience

Using it is straight forward:

import Something from 'xyz';
import { SomethingElse } from 'components/SomethingElse';

Took some trial and error but it's working fine now.

from react-native-template-typescript.

Bi0max avatar Bi0max commented on May 11, 2024 2

Probably this could help somebody. It helped me: https://www.reactnativeschool.com/how-to-setup-path-alias-in-a-react-native-typescript-app

from react-native-template-typescript.

emin93 avatar emin93 commented on May 11, 2024

Hi @nkovacic,

Thank you for creating an extended issue.

Sadly the .tsconfig can only be used for type checking and not for transpilation. That's because since React Native 0.57, TypeScript has been added as a preset for Babel 7 (https://blogs.msdn.microsoft.com/typescript/2018/08/27/typescript-and-babel-7/) and the TypeScript compiler is not being used anymore.

I have not yet found a solution for that issue and open for contributions.

from react-native-template-typescript.

nkovacic avatar nkovacic commented on May 11, 2024

Is there any other way to atleast set a base path, so that absolute paths can be used?
Because relative path imports like these are really a pain to mantain...
import { currentStepSelector } from '../../../../../modules/auth/reducer';

from react-native-template-typescript.

emin93 avatar emin93 commented on May 11, 2024

I never used absolute paths but I quickly checked and there seems to be a way: https://medium.com/@davidjwoody/how-to-use-absolute-paths-in-react-native-6b06ae3f65d1

from react-native-template-typescript.

nkovacic avatar nkovacic commented on May 11, 2024

@solkaz tried your solution and there seems to be a collision between ./src folder and some react-native internals. Probably should work in other JavaScript projects.

from react-native-template-typescript.

solkaz avatar solkaz commented on May 11, 2024

@nkovacic what errors were you getting?

from react-native-template-typescript.

larryranches avatar larryranches commented on May 11, 2024

Also having this issue with setting baseUrl in the tsconfig. Any valid workarounds or solutions on this?

from react-native-template-typescript.

nkovacic avatar nkovacic commented on May 11, 2024

Even when changing the src main folder to app, paths still do not work. Can anyone share a complete example?

from react-native-template-typescript.

guatedude2 avatar guatedude2 commented on May 11, 2024

I'm seeing the same issue

from react-native-template-typescript.

mrruby avatar mrruby commented on May 11, 2024

Adding babel-plugin-module-resolve and updating babel.config.js with tsconfig.js works fine in my 0.60.5 app

from react-native-template-typescript.

radko93 avatar radko93 commented on May 11, 2024

I don't think this is related to this template if you would like to have this feature consider submitting a PR.

from react-native-template-typescript.

juanlet avatar juanlet commented on May 11, 2024

Neat solution @resolritter . That worked for me. Thanks!

from react-native-template-typescript.

kelvinndmo avatar kelvinndmo commented on May 11, 2024

Saved my life @apedroferreira

from react-native-template-typescript.

revgum avatar revgum commented on May 11, 2024

👍 to @apedroferreira

from react-native-template-typescript.

DarkHaker03 avatar DarkHaker03 commented on May 11, 2024

Thank u @resolritter , your solution really helped me )

from react-native-template-typescript.

gabriel-hahn avatar gabriel-hahn commented on May 11, 2024

Thanks @apedroferreira! It worked for me with the module-resolver solution and resetting the cache too, but for React Native CLI: npm start -- --reset-cache

from react-native-template-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.