Encountering difficulties importing a component from a library into my Nx Expo React Native application

Having an issue with my Nx monorepo which contains an Expo React Native app and various libraries. The problem arises when trying to import a component from a library within the application, resulting in

Error: Cannot resolve @monorepo/account-manager

Within the libs folder of my Nx workspace, there is a library called account-manager. Inside this library, there's a component App.tsx that I exported in the index.tsx file:

export {default as App} from "../src/app/App"

In my root tsconfig.base.json, I added the following:

"paths": {
      "@monorepo/account-manager": ["libs/account-manager/src/index.tsx"]
    }

Then, I imported it into my project like this (Please ignore the naming):

import React from 'react';
import {App as Hello} from "@monorepo/account-manager"


const App = () => (
  <Hello/>
);

export default App;

I generated the library using the command: npx nx g @nx/expo:library

The full error message after attempting to import the exported component reads as follows:

error: Error: Cannot resolve @monorepo/account-manager
    at firstResolver (C:\DEV\monorepo\node_modules\@nx\expo\plugins\metro-resolver.js:33:15)
    ...
    at DeltaCalculator._getChangedDependencies (C:\DEV\monorepo\node_modules\metro\src\DeltaBundler\DeltaCalculator.js:151:25)

I have already tried renaming the file from index.ts => index.tsx Additionally, I attempted adding the "paths" object inside the projects tsconfig.json.

Answer №1

I have encountered a bug with Nx Expo Monorepos, but I managed to find a solution on GitHub:

To fix the error in your app related to the withNxMetro() function, you need to manually add the path to your libs inside the watchFolders Array in your metro.config.js file:

Update the watchfolders section as follows:

const path = require('node:path');

watchFolders: [
  path.resolve("../..", "libs")
],

If you encounter an issue with babel-plugin-module-resolver, simply install the library using npm:

npm i babel-plugin-module-resolver

After following these steps, everything should work correctly again.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for utilizing PINO to write logs to both file and console

I have successfully implemented logging to a log file using Pino. However, I am wondering if there is a way to also log to the console in order to manage the frequency of 'console.log()' calls. Node Version : 21.6.1 Typescript Version : 5.3.3 Pi ...

Angular 2 and SystemJS: Dealing with the Challenge of Circular Dependencies

I have been struggling with a problem that seems to stem from a circular dependency. Despite conducting thorough research, I have not been able to find a suitable solution. It appears to be similar to the issue discussed here: TypeError: b is undefined in ...

How can I transfer an instance of a class to dataTransfer.setData?

So I created a new instance of a class: let item = new Item(); Next, I attempted to serialize the item and add it to dataTransfer for drag and drop functionality: ev.dataTransfer.setData("info", JSON.stringify(item)); At some point, I need to retriev ...

Guide to reference points, current one is constantly nonexistent

As I work on hosting multiple dynamic pages, each with its own function to call at a specific time, I encounter an issue where the current ref is always null. This poses a challenge when trying to access the reference for each page. export default class Qu ...

My type is slipping away with Typescript and text conversion to lowercase

Here is a simplified version of the issue I'm facing: const demo = { aaa: 'aaa', bbb: 'bbb', } const input = 'AAA' console.log(demo[input.toLowerCase()]) Playground While plain JS works fine by converting &apo ...

Module 'angular2/angular2' not found

Currently, I am working on a node application with angular2 and gulp. One of the components I have created is login.ts: import {Component, View} from 'angular2/angular2'; import {FormBuilder, formDirectives } from 'angular2/forms'; @C ...

Having difficulty creating a TypeScript function

I've encountered a TypeScript error that has left me puzzled: src/helpers.ts:11:14 - error TS2322: There's an issue with this piece of code and I can't quite grasp it: Type '<T extends "horizontal" | "vertical" | undefined, U extends ...

Encountering difficulties accessing props while invoking a component in React

In my project, I've created a component called FilterSliders using Material UI. Within this component, I passed a prop named {classes.title} by destructuring the props with const { classes }: any = this.props;. However, when I try to access this prop ...

The discord.js TypeScript is throwing an error stating that the 'index.ts' file is missing when trying to run 'ts-node index.ts'

I have been working on creating a discord bot using discord.js and TypeScript. However, when I attempt to start the bot by running 'ts-node index.ts', I encounter the following error: Error: Cannot find module 'node:events' Require stac ...

Enforcing object keys in Typescript based on object values

I'm looking to design a structure where the keys of an object are based on values from other parts of the object. For example: type AreaChartData = { xAxis: string; yAxis: string; data: { [Key in AreaChartData['xAxis'] | AreaChart ...

React Div Element Not Extending to Web Page Margin

creating white space between the green div and both the top and sides of the screen This is my index page export default function Home() { return( <div className = {stylesMain.bodyDiv}> <div>home</div> &l ...

Executing NestJS code after applying a pipe but before reaching the method handler

Is it possible to insert additional code after all the pipes have transformed but before the method handler is called? https://i.sstatic.net/IjQvv.png ...

Issue with exporting Typescript React component

Despite searching for answers, none of the related questions have been helpful... My goal is to export React components from a TypeScript library that I plan to publish on npm. For testing purposes before publishing, I am utilizing npm link. The structu ...

When working with formControlName in Angular Material 2, the placeholder may overlap the entered value

After updating my application with angular-cli to angular/material (2.0.0-beta.11) and angular (4.4.4), I noticed that every placeholder in the material input fields overlaps the value when provided with formControlName in reactive forms. However, when usi ...

TS2339: The 'contacts' property is not found within the 'Navigator' type

I am currently developing a contacts application that utilizes the Apache Cordova plugins for contacts. However, when attempting to run the npm run bundle command for my application, I encountered the error mentioned in the title above. Can anyone guide me ...

Convert an interface type variable to a string representation

I'm just starting to learn angular and I'm attempting to convert my response from an interface type into a string so that I can store it in session storage. However, when I try to save them, they are being stored as [object] in the session storag ...

Proper method for determining return type through the use of `infer`

I need to find out the return type based on input values, like in the code below: type ReturnType<S> = { array: S extends 'number' ? number[] : S extends 'string' ? string[] : never; value: S extends 'number' ? n ...

Troubleshooting a Type Parameter Error in React Native TypeScript

I am working on a project in React Native using TypeScript, and I encountered this issue: I am getting the error Argument of type 'GestureResponderEvent' is not assignable to parameter of type 'SetStateAction<string>'.ts(2345) wit ...

Creating an object type that includes boolean values, ensuring that at least one of them is true

To ensure both AgeDivisions and EventStyles have at least one true value, I need to create a unique type for each. These are the types: type AgeDivisions = { youth: boolean; middleSchool: boolean; highSchool: boolean; college: boolean; open: bo ...

React Native: Encounter Unexpected Token Transform Error (

Yesterday, I successfully followed the instructions on React Native's Android Setup Page and ran the code without any issues. However, today when I attempted to run the same code, it displayed a "build is successful" message but only showed a android/ ...