create-t3-turbo icon indicating copy to clipboard operation
create-t3-turbo copied to clipboard

bug: unable to import other workspace packages into expo

Open Mirthis opened this issue 1 year ago • 13 comments

Provide environment information

System: OS: Windows 11 10.0.22631 CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz Memory: 2.81 GB / 15.80 GB

Binaries: Node: 21.7.1 - C:\Program Files\nodejs\node.EXE npm: 10.5.0 - C:\Program Files\nodejs\npm.CMD pnpm: 8.15.5 - ~\AppData\Local\pnpm\pnpm.EXE

Describe the bug

I'm facing an issue when importing packages created in other workspaces into expo (I assume this is not the case for devDependencies as they don't end up in the final bundle).

Android Bundling failed 4976ms (C:\Users\mirth\Coding\create-t3-turbo\node_modules\expo-router\entry.js)
Unable to resolve "@acme/validators" from "src\app\index.tsx"

I originally found this problem when creating a new lib workspace that I'm able to use within the api workspace, but not in the expo workspace.

Just to rule out me having made any mistake with the new package, I've tried the same with the existing validators package and I'm getting the same error.

I can see @acme/validators under the node_modules folder in the expo directory but it looks like the metro bundler cannot resolve it.

I think this may be similar to the issue report here.

To replicate this I attached a repo where I have:

  • cloned a the latest create-t3-turbo repo
  • added and exported a new variable TEST_IMPORT in /packages/validators/src/index.ts
  • added "@acme/validators": "workspace:*" in package.json in app/expo
  • imported the new variable and console.log it in app\expo\src\app\index.tsx

Link to reproduction

https://github.com/Mirthis/create-t3-turbo-expo-import

To reproduce

  • clone the repo
  • set environment variables as usual
  • run the application
  • the TEST_IMPORT variable defined in @acme\validators is going to be logged in the console in the next app while the expo app will throw a Unable to resolve "@acme/validators" error

Additional information

No response

Mirthis avatar Mar 28 '24 16:03 Mirthis

Maybe try this https://metrobundler.dev/docs/package-exports/

juliusmarminge avatar Mar 28 '24 16:03 juliusmarminge

I have now added the following to the same packages/validators/package.json file and things seems to be working ok:

"main": "src/index.ts"

It would be good if someone can validate this as I have no experience working with a monorepo.


Basically, after looking more in depth at the error shown on my device it looks like the module was resolved from @acme/validators/index.ts instead of @acme/validators/src/index.ts so not taking into account what defined under exports in package.json:

exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "default": "./src/index.ts"
    }
  },

Mirthis avatar Mar 28 '24 16:03 Mirthis

Maybe try this https://metrobundler.dev/docs/package-exports/

Thanks! I think I may have found a solution but I'll go through this.

Mirthis avatar Mar 28 '24 17:03 Mirthis

@Mirthis just following up here - I'm trying to use @acme/validators inside expo as well... did you simply add the main field to package.json, create a new build, and then viola?

adamspotlite avatar Apr 25 '24 06:04 adamspotlite

@adamspotlite yes that should do it.

So in package.json under /apps/expo add "@acme/validators": "workspace:^0.1.0" to your dependencies. If with just this your package is not resolved then in package.json under packages\validators add "main": "src/index.ts"

After each change re-run pnpm i

Mirthis avatar Apr 25 '24 09:04 Mirthis

Hey, I had this same problem. After adding "main": "src/index.ts" to @acme/validators's package.json, it worked for me

dBianchii avatar Jun 02 '24 18:06 dBianchii

Hey, I had this same problem. After adding "main": "src/index.ts" to @acme/validators's package.json, it worked for me

this works for single-entrypoint packages but for multi-entrypoint you need exports conditions

juliusmarminge avatar Jun 09 '24 14:06 juliusmarminge

@juliusmarminge: any suggestion on how to make that work? As mentioned earlier in the thread the multi-entry point setup works ok, but not in the expo app.

Mirthis avatar Jun 09 '24 17:06 Mirthis

@juliusmarminge: any suggestion on how to make that work? As mentioned earlier in the thread the multi-entry point setup works ok, but not in the expo app.

This didn't work? https://metrobundler.dev/docs/package-exports/ Then probably file an issue in Metro?

juliusmarminge avatar Jun 09 '24 18:06 juliusmarminge

@juliusmarminge: any suggestion on how to make that work? As mentioned earlier in the thread the multi-entry point setup works ok, but not in the expo app.

This didn't work? https://metrobundler.dev/docs/package-exports/ Then probably file an issue in Metro?

I tried this out when you previously suggested and it was causing all sorts of other issues with packages resolution. and I also mentioned that here. I don't remember all the details now, so I may try this out again.

Mirthis avatar Jun 09 '24 18:06 Mirthis

Imo this is a really low hanging fruit from the Metro team. Exports conditiond have been around for ages now so i dont know why they arent prioritizing getting this stable and default by now...

juliusmarminge avatar Jun 09 '24 18:06 juliusmarminge

Anyone found a solution for this?

dBianchii avatar Jul 01 '24 15:07 dBianchii

https://github.com/expo/expo/issues/26926#issuecomment-2206043848 should help

oney avatar Jul 03 '24 13:07 oney