Issue: Babel module-resolver does not resolve configured path alias when file extension is cjs, giving error message "Unable to resolve [alias/component] from [importing file]".
Solution: Rename babel.config.cjs to babel.config.js
Expect: babel.config file extension to have no effect on whether module-resolver works, given the config file is valid with respect to its file extension.
Reproduction steps: Steps to reproduce this example project:
- Create an expo app with
npx create-expo-app
- Rename babel.config.js to babel.config.cjs
- Add the following property to babel.config return value, and replace the comments:
plugins: [
[
"module-resolver",
{
alias: {
/* alias name as valid JS object key */: /* relative path to directory from root as string */,
},
},
],
],
- Create file in the path specified in the alias key of the module-resolver
- Import module with path alias in App.js
- Run
npm run start -- -c
- Note: I always run expo on tunnel mode so my command is
npm run start -- --tunnel -c
, but I expect no difference with this
- Note: I always run expo on tunnel mode so my command is
- Expect Metro bundle failure with error message "Unable to resolve [alias/component] from [App.js]"
Steps to fix error:
- Rename babel.config.cjs to babel.config.js
- Run
npm run start -- -c
- Expect Metro to bundle successfully
I have not tested to see if this issue happens when
- not using expo
- not using expo tunnel mode
- using other babel plugins
- using other valid babel.config file extensions