It won't let me comment on the answer that worked for me, but I need to add context.
If you have
"type": "commonjs",in your package.json, remove it helped me with same error
I've been trying to learn webpack lately and keep running into the same error in every tutorial. The difference is that with npm init -y the package.json file now adds a default "type": "commonjs" ; I guess it didn't before. I still don't understand why there are 3 states to this setting when declaring it only gives you 2 options, but "null" or not having the setting is the winner.