TypeError: Cannot read properties of undefined (reading 'transformFile')

avatar
Borislav Hadzhiev

Last updated: Apr 7, 2024
3 min

banner

# TypeError: Cannot read properties of undefined (reading 'transformFile')

To solve the TypeError: Cannot read properties of undefined (reading 'transformFile'):

  1. Update your react-native version to the latest.
  2. Set the NODE_OPTIONS environment variable to --openssl-legacy-provider.
  3. Alternatively, downgrade your Node.js version to 16.13.0.
shell
Metro has encountered an error: TypeError: Cannot read properties of undefined (reading 'transformFile') at Bundler.transformFile (/Users/bobbyhadz/node_modules/metro/src/Bundler.js:48:30) at runMicrotasks (<anonymous>) at processTicksAndRejections (node:internal/process/task_queues:96:5)
If you use react-native, the error is caused because of incompatibility between older versions of Metro with newer versions of Node.js.

The issue has been resolved in newer versions of react-native.

# Delete your node_modules and reinstall your dependencies

Try to delete your node_modules and package-lock.json (not package.json) files and rerun the npm install command.

shell
# ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force npm install

# Upgrade your react-native version

Open your terminal in your project's root directory and run the following command to upgrade react-native.

shell
npx react-native upgrade

If the issue persists after upgrading react-native, set the NODE_OPTIONS environment variable to --openssl-legacy-provider.

# Set the NODE_OPTIONS environment variable

Open your terminal and run the specific command for your shell type.

shell
# ๐Ÿ‘‡๏ธ for macOS, Linux or Windows Git Bash export NODE_OPTIONS=--openssl-legacy-provider # ---------------------------------------------------- # ๐Ÿ‘‡๏ธ for Windows CMD (Command Prompt) set NODE_OPTIONS=--openssl-legacy-provider # ---------------------------------------------------- # ๐Ÿ‘‡๏ธ for Windows PowerShell $env:NODE_OPTIONS="--openssl-legacy-provider" # ---------------------------------------------------- # ๐Ÿ‘‡๏ธ for Docker (in your Dockerfile) ENV NODE_OPTIONS="--openssl-legacy-provider"

set node options environment variable

Make sure to use the command that corresponds to your shell type.

windows set node options environment variable

The --openssl-legacy-provider option is needed when using the latest version of Node.js, because Node.js 17 and later uses OpenSSL 3.0 which has had some breaking changes.

If setting the NODE_OPTIONS environment variable helped, you don't have to make any of the following changes.

If the error persists, try using the --openssl-legacy-provider flag when issuing the command in your package.json file.

First install the cross-env package to make the command work on macOS, Linux and Windows.

shell
npm install cross-env

Here is an example.

package.json
{ "scripts": { "start": "cross-env NODE_OPTIONS=--openssl-legacy-provider && expo start", } }

Simply add NODE_OPTIONS=--openssl-legacy-provider to your command.

Make sure to update the part after cross-env NODE_OPTIONS=--openssl-legacy-provider && if you have to.

If you get an error that "node: --openssl-legacy-provider is not allowed in NODE_OPTIONS", unset the NODE_OPTIONS environment variable and rerun the command.

shell
# ๐Ÿ‘‡๏ธ for macOS, Linux or Windows Git Bash unset NODE_OPTIONS # ----------------------------------------- # ๐Ÿ‘‡๏ธ for Windows CMD (Command Prompt) set NODE_OPTIONS= # ----------------------------------------- # ๐Ÿ‘‡๏ธ for Windows PowerShell [Environment]::SetEnvironmentVariable('NODE_OPTIONS', '', 'User') [Environment]::SetEnvironmentVariable('NODE_OPTIONS', '', 'Machine')

Try to rerun your script after deleting the environment variable.

If none of the suggestions helped, you can downgrade your Node.js version to 16.13.0 to resolve the error.

# Downgrade to Node.js version 16

You can issue the following command to downgrade your Node.js version to 16.13.0 if you use NVM.

shell
nvm install 16 nvm use 16

nvm install 16 13 0

The command will install and switch to version 16, which should resolve the issue.

The error often occurs when installing Node.js version 17+. Rolling back to version 16.X.X solves it.

The --openssl-legacy-provider option is needed when using the latest version of Node.js, because Node.js 17 and later uses OpenSSL 3.0 which has had some breaking changes.

If you get the error when using Docker, switch your Node version to 16.13.0 (or 16.X.X) in your Dockerfile.

If you still get the error after downgrading to Node version 16, try to delete your node_modules and package-lock.json, rerun the npm install command and restart your development server.

shell
# ๐Ÿ‘‡๏ธ (Windows) delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ (macOS/Linux) delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean your npm cache npm cache clean --force npm install

Restart your development server after issuing the npm install command.

click on the article and follow the instructions.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev