How to fix “Error: No valid exports main found for ‘/node_modules/packname by Godwill Barasa
On 12th May, as I was trying to “npm run dev” a react application that I hadn’t pulled from the master branch in a while, I came across the following error when trying to start the app:
Error: No valid exports main found for ‘/node_modules/postcss’
Quick google search, I noticed the problem is because of one or more of the packages was bumped to support a more recent version. So, I checked what is the version I was running:
node -v
Turns out I was running V.10, yet the package was updated for version 13.
Next thing was to install nvm. This piece of software allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time. visit the project’s GitHub page.
Before piping the command through to bash, it is always a good idea to audit the script to make sure it isn’t doing anything you don’t think might be indifferent from your case
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh
Take a look and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with | bash appended at the end.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
This will install the nvm script to your user account. To use it, you must first source your .bashrc file:
source ~/.bashrc
Now, you can ask NVM which versions of Node are available:
nvm list-remote
This will list a whole bunch of versions, just select a newer version. In my case I contacted the software developer asked him which version he was using in his dev environment. Turns out he uses v14.5
It’s a very long list! You can install a version of Node by typing any of the release versions you see.
nvm install v14.5
You can see the different versions you have installed by typing:
nvm list
You can switch between installed versions with nvm use:
nvm use v13.6.0
Now using node v13.6.0 (npm v6.13.4)
You can verify that the install was successful using the same technique from the other sections, by typing:
node -v
Output
v13.6.0
Then I,
npm cache clean — force
Delete node_modules by
rm -rf node_modules
delete the package-lock.json file too.
To install the dependencies again.
npm install
Last step was to run, npm run dev which worked perfectly