metro: unstable_enableSymlinks does not seems to work
Description
I have this project structure
projectRoot -> node_modules/ -> src/ … -> neWeb/ <- The intention is for this directory to be a symlink
I am getting this error message Unable to resolve module ./neWeb/hello.js
However, if I replace the symlink with a real directory, the error goes away
I did a quick search on the https://reactnative.dev with the unstable_enableSymlinks key word but only found the announcement. Hopefully, the intent of the feature is similar to my use case.
React Native Version
0.72.7
Output of npx react-native info
info Fetching system and libraries information… System: OS: Linux 6.2 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish) CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor Memory: 3.79 GB / 46.94 GB Shell: version: 5.1.16 path: /bin/bash Binaries: Node: version: 16.13.0 path: ~/.nvm/versions/node/v16.13.0/bin/node Yarn: Not Found npm: version: 8.1.0 path: ~/.nvm/versions/node/v16.13.0/bin/npm Watchman: version: 20231001.212519.0 path: /usr/local/bin/watchman SDKs: Android SDK: Not Found IDEs: Android Studio: Not Found Languages: Java: version: 11.0.20.1 path: /usr/bin/javac Ruby: Not Found npmPackages: “@react-native-community/cli”: Not Found react: installed: 18.2.0 wanted: 18.2.0 react-native: installed: 0.72.7 wanted: 0.72.7 npmGlobalPackages: “react-native”: Not Found Android: hermesEnabled: true newArchEnabled: false iOS: hermesEnabled: Not found newArchEnabled: Not found
Steps to reproduce
1 - Init a standard project
2 - Configure metro.config.js with unstable_enableSymlinks, as in the following
const config = { resolver: { unstable_enableSymlinks: true, }, };
3 - Add the required import
4 - npm start – --reset-cache
5 - Type a to run on Android
Snack, screenshot, or link to a repository
The imported file is hello.js and simply have the following code.
export function dummy () {
let result = 'Hello';
return result;
}
And the default App.ts was modified to have
import { dummy } from './neWeb/hello.js';
debugger;
const a = dummy();
console.log(a);
debugger;
Github repo with the defect. https://github.com/clauderobi/rn_Symlink
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 1
- Comments: 15 (6 by maintainers)
Ah, thanks for the report - this is a subtle known issue that typically comes up in non-monorepo shared code. Metro’s transformation process injects dependencies on
@babel/runtime/helpersto your code, and then when transforming that file’s dependencies we see those as any otherrequire()and try to resolve them relative to the file being transformed, in this case, from somewhere underneWeb, which won’t find your project’s dependencies.Two possible workarounds: install
@babel/runtimeas a dependency ofneWeb(assuming it has apackage.json) or an ancestor, or add a custom resolver to resolve@babel/runtimerelative to your project instead of relative to the source location.This has come up a few times now, I’ll prioritise fixing it in Metro. Thanks for the additional info.
@robhogan Sorry for adding into this thread as the issue is indirectly related to unstable_enableSymlinks.
I am trying to setup a new project and I still having issues with the symlink. The reported error is: error: Error: Unable to resolve module, the path listed is correct.
metro.config.js in the new project is identical to the same in the old project, except for the adjustment to the watchFolders array.
The watchman configuration, as reported by watchman watch-list is correct. The root paths are all as they should be.
I am using babel to alter the path in some way but replacing the defective import statement path with an absolute full path fix the resolution issue for that import statement. This tells me that watchman in indeed Ok but the symlink setup is somehow invalid. But of course I have tons of import statement later that fail. I did this check just to make sure babel was not in the way (in any case, babel.config.js for both the old and the new project are identical).
I am under the impression that resolver.unstable_enableSymlinks is not taken into account. To make a long story short (well, shorter… ) in the end I did a grep for unstable_enableSymlinks for both the old and new projects. In the old project, there are a lot of hits under node_modules. But significantly less for the new project.
I won’t list all the differences but of particular note, the following directories do not exist in the new project. node_modules/metro-config/node_modules node_modules/@react-native/metro-config/node_modules node_modules/@react-native-community/cli-plugin-metro/node_modules/metro-config
Another difference is that in the following 2 files, the value for unstable_enableSymlinks is true in the old project but false in the new. I di dnot make the change myself. node_modules/metro-config/src/defaults/index.js.flow node_modules/metro-config/src/defaults/index.js
package.json in the old project does not list anything that explains the difference in node_modules. Doing rm -rf node_modules and running npm i makes not difference.
I exhausted all I can think of. Any suggestion?