Closed prateekuttreja2020 closed 11 months ago
The key part is here: https://github.com/prateekuttreja2020/sample/blob/47e35353d86e0a5d9d19c19845ab65d314001a1c/App.tsx#L27-L28
// import { getNumber } from './src/utility'; //working
import { getNumber } from 'utility'; // not working
@prateekuttreja2020 is this a regression from 0.72?
I'm passing this over to Metro
Hi @prateekuttreja2020 - what you're describing there is known as a "global package" in Metro. Global package resolution is indeed disabled by default now, as a breaking change in Metro 0.79+ (React Native 0.73+).
We felt it was rarely used, non-standard and more often led to confusion or issues with conflicts - it's also not natively understood by the likes of TypeScript, Jest or Node.js.
The recommended approach now that Metro supports symlink resolution is to make packages like your utility
a workspace. For example, if you're using Yarn v1, add "workspaces": [ "src/utility" ]
to your root package.json
, and run yarn
. That creates a symlink at node_modules/utility
to ../src/utility
, which Metro can follow as of React Native 0.73. This should also work seamlessly with other tooling.
If you do still want to use global packages (not recommended), you can enable it by setting resolver.enableGlobalPackages: true
in your Metro config.
// metro.config.js
const config = {
resolver: {
enableGlobalPackages: true,
// ...
},
// ...
}
@robhogan I am not that much into details about how metro works. But AFAIK know in previous RN versions we needed only to set paths in tsconfig.json to make absolute imports (aka aliases) work. Do I understand correct that this is not working anymore due to metro changes?
Hi @sasweb - sorry for the delay. Metro itself(*) doesn't read tsconfig.json
, and never has. If it appeared to work that way it was only because there was some overlap in certain setups between Metro global packages and TS paths
, but they're really completely different mechanisms and never intended to mimic each other - there were always lots of cases where tsconfig.json#paths
would work in TypeScript but not Metro.
() Expo CLI does* support tsconfig.json#paths
via a custom resolver - that should still work, and is a much better solution than enableGlobalPackages
. I'd still recommend using workspaces
if possible though - that should work with zero config for every tool.
Description
Absolute Paths are not working in React Native 0.73.1
Error: undefined Unable to resolve module utility from /Users/apple/sample/App.tsx: utility could not be found within the project or in these directories:
Steps to reproduce
1) Install Application 2) Notice Crash
React Native Version
0.73.1
Affected Platforms
Runtime - Android, Runtime - iOS
Output of
npx react-native info
Stacktrace or Logs
Reproducer
https://github.com/prateekuttreja2020/sample
Screenshots and Videos