React npm start issue -- ELIFECYCLE, errno1 - node.js

Any time I try to run npm start I get an error saying:
npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! dbproject#0.1.0 start: set PORT=9999 && react-scripts start npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the dbproject#0.1.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
So far I've tried:
-Changing environment variables, my current environment variables are these
-Cleaning the npm cache, deleting the node_modules and then running npm install again
-I tried running npm start in different project folders, none of the React projects work, they all throw the same error. Node servers run well, I haven't tried running anything else.
Pastebin of the full console log: https://pastebin.com/P8DDDaDq
Pastebin of the debug.txt file: https://pastebin.com/1ebezKX2
I'm running Windows 10 Home Build 17134.
I would be extremely grateful of any help

Problem solved!
Leaving the solution that worked for me here in case it is helpful for anyone.
Here's what I did:
1.- Uninstall node
2.- Restart your computer
3.- Install node again
4.- Install React running npm install -g create-react-app
5.- Create a new React project folder running npm create-react-app yourProjectName, place all your project files here except for the node_modules folder and the package.json files
6.- After you have done that install all of your dependencies manually with npm, it should work fine now!

Related

npx start, npx install npx init is not working

I have been working with react for years using NPX and yarn and I just got assign to built an app with react-native using react native-cli so i tried setting up my environment using the react native docs and some YouTube videos. But after following the instructions like downloading android studios and adding in my new environment variable, I'm getting an error every time i tired to run
"npx react-native init AwesomeProject" which gave me this error
[![npx react-native init result][1]][1]
I also tried npm install and i got this error on git bash and also on my commandline
npm ERR! syscall spawn bash
npm ERR! path C:\Users\Dennis\Desktop\GitHub\LCRN01-travel-onboarding-app-master\LCRN01-travel-onboarding-app-master\node_modules\core-js
npm ERR! errno -4058
npm ERR! enoent spawn bash ENOENT
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Dennis\AppData\Local\npm-cache\_logs\2021-11-13T05_50_35_592Z-debug.log```
and also npx react-native start is showing this error on git bash
```openssl config failed: error:02001003:system library:fopen:No such process
Command `start` unrecognized. Make sure that you have run `npm install` and that you are inside a react-native project.```
I have tried running "npm cache clean --force"
but still nothing changed
I can't even run npx create-react-app anymore because i keep getting this error
npx creat-react-app test
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/creat-react-app - Not found
npm ERR! 404
npm ERR! 404 'creat-react-app#latest' is not in this registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Dennis\AppData\Local\npm-cache_logs\2021-11-13T06_05_46_068Z-debug.log
I'm currently using a windows 10 device
node -v 16.13.0
npm -v 8.1.3
[1]: https://i.stack.imgur.com/OTLYj.png
It's create-react-app.
Not totally sure but another issue could be that your Git bash isn't in your PATH.
Plus, npx start and npx install doesn't sound right, maybe you meant npm start and npm install.
On top of that, it seems you have node v11 and not node v16, maybe you have different versions of node, some in Windows' PATH and some only accessible from bash.
It could help to clean up and reinstall your node versions and Git bash, also check your environment variables for remnants of previous versions, and make sure to not confuse npm and npx.

Cannot find module typescript.js after Windows restoration point. I'm triying to build my angular app

I had problems with my PC and I restored windows to and old restoration point.
After this... When I try to build my Angular App I have the next error:
C:\Udemy\AngularDeCeroAExpertoEdicion2021\03-paisesApp>npm run build
> paises-app#0.0.0 build C:\Udemy\AngularDeCeroAExpertoEdicion2021\03-paisesApp
> npm run config -- --environment=prod && ng build --prod
> paises-app#0.0.0 config C:\Udemy\AngularDeCeroAExpertoEdicion2021\03-paisesApp
> ts-node ./src/assets/scripts/setEnv.ts "--environment=prod"
internal/modules/cjs/loader.js:329
throw err;
^
Error: Cannot find module 'C:\Udemy\AngularDeCeroAExpertoEdicion2021\03-paisesApp\node_modules\typescript\lib\typescript.js'. Please verify that the package.json has a valid "main" entry
at tryPackage (internal/modules/cjs/loader.js:321:19)
at Function.Module._findPath (internal/modules/cjs/loader.js:534:18)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:888:27)
at Function.resolve (internal/modules/cjs/helpers.js:98:19)
at loadCompiler (C:\Users\myuser\AppData\Roaming\npm\node_modules\ts-node\dist\index.js:208:34)
at create (C:\Users\myuser\AppData\Roaming\npm\node_modules\ts-node\dist\index.js:213:28)
at Object.register (C:\Users\myuser\AppData\Roaming\npm\node_modules\ts-node\dist\index.js:184:21)
at main (C:\Users\myuser\AppData\Roaming\npm\node_modules\ts-node\dist\bin.js:129:29)
at Object.<anonymous> (C:\Users\myuser\AppData\Roaming\npm\node_modules\ts-node\dist\bin.js:448:5)
at Module._compile (internal/modules/cjs/loader.js:1085:14) {
code: 'MODULE_NOT_FOUND',
path: 'C:\\Udemy\\AngularDeCeroAExpertoEdicion2021\\03-paisesApp\\node_modules\\typescript\\package.json',
requestPath: 'typescript'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! paises-app#0.0.0 config: `ts-node ./src/assets/scripts/setEnv.ts "--environment=prod"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the paises-app#0.0.0 config script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\myuser\AppData\Roaming\npm-cache\_logs\2021-08-01T08_27_09_159Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! paises-app#0.0.0 build: `npm run config -- --environment=prod && ng build --prod`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the paises-app#0.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\myuser\AppData\Roaming\npm-cache\_logs\2021-08-01T08_27_09_186Z-debug.log
I think that the problem is with the directory:
C:\Users\myuser\AppData\Roaming\npm\node_modules\
What can I do?
remove the folder node_modules?
reinstall npm?
I tried with npm install and all is ok.
Thanks.
Edited:
Maybe the problem was in my merged branches or something like that.
Since you have not specified what was the installed state of apps when the restoration point was lastly set, I presume that you have all the npm packages captured correctly at that point. If so, try doing the below steps...
Since you have not included the entire log with this, I can guess it is possibly an ambiguity issue due to references not properly resolved!
Open command prompt/powershell/terminal, etc. with admin privilege. Do as below (sudo command not to be used with Windows OS): -
sudo npm cache clean --force
sudo npm cache verify
Delete the package-lock.json file located at C:\Users\<your_username>
Try installing your project dependencies now from your project folder with npm i or yarn install (if you're using yarn as your preferred package manager).
Note: - If this doesn't work, follow the same process as above (again) and uninstall npm/yarn and node and reinstall it. It should work then!
I have fixed it by doing the following:
Remove the folder /node_modules of my Angular App.
Run npm install.
And then run npm run build or npm run start or ng serve.
I have also found a package that does the same. The package name is reinstall
reinstall
Reinstall package's node modules without cache.
Removes node_modules directory
Clean's NPM cache
Install's modules from package.json
npm install reinstall -g
Once installed remember restart your terminal and run reinstall in your root dir.
Maybe the problem was in my merged branches or something like that.

error: This is probably not a problem with npm. There is likely additional logging output above

In my project, I'm using Angular6 for the frontend. Now what I'm trying to do is deploy my project which is in remote server into the actual server. I'm using npm run build -prod command to build the frontend first. But I can't build my project since the following error occurs again and again,
npm ERR! code ELIFECYCLE
npm ERR! errno 134
npm ERR! Trackit-Revamp#6.0.0 build: `ng build --prod --build-optimizer --aot`
npm ERR! Exit status 134
npm ERR!
npm ERR! Failed at the Trackit-Revamp#6.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Indrajith.E\AppData\Roaming\npm-cache\_logs\2019-08-22T08_41_00_271Z-debug.log
My error log in the C:\Users\Indrajith.E\AppData\Roaming\npm-cache\_logs\2019-08-22T08_41_00_271Z-debug.log file path contains the same error details mentioned above.
How can I resolve this problem?
Delete your package-lock.json file and node_modules folder.
Then do npm cache clean
npm cache clean --force
do
npm install
again and run
Finally, I found a solution to this problem without reinstalling npm and I'm posting it because in future it will help someone,
Most of the time this error occurs javascript heap went out of the memory. As the error says itself this is not a problem with npm. Only we have to do is
instead of,
npm run build -prod
extend the javascript memory by following,
node --max_old_space_size=4096 node_modules/#angular/cli/bin/ng build --prod
Please delete package-lock.json and clear npm cache with npm cache clear --force
and delete whole node_modules directory
Finally install or update packages again with npm install / npm update
You can also add any new packages with npm install <package-name>
This fixed for me.
Thanks and happy coding.
I'm on Ubuntu 18.04. I fixed this problem by increasing the inotify max_user_watches using this command:
echo 65536 | sudo tee -a /proc/sys/fs/inotify/max_user_watches
I'm new with react... Well i had the same output:
Starting the development server...
events.js:196
throw er; // Unhandled 'error' event
^
Error: ENOSPC: System limit for number of file watchers reached, watch '/opt/lampp/htdocs/react-tuto/public'
at FSWatcher.<computed> (internal/fs/watchers.js:168:26)
at Object.watch (fs.js:1351:34)
at createFsWatchInstance (/opt/lampp/htdocs/react-tuto/node_modules/chokidar/lib/nodefs-handler.js:38:15)
at setFsWatchListener (/opt/lampp/htdocs/react-tuto/node_modules/chokidar/lib/nodefs-handler.js:81:15)
at FSWatcher.NodeFsHandler._watchWithNodeFs (/opt/lampp/htdocs/react-tuto/node_modules/chokidar/lib/nodefs-handler.js:233:14)
at FSWatcher.NodeFsHandler._handleDir (/opt/lampp/htdocs/react-tuto/node_modules/chokidar/lib/nodefs-handler.js:429:19)
at FSWatcher.<anonymous> (/opt/lampp/htdocs/react-tuto/node_modules/chokidar/lib/nodefs-handler.js:477:19)
at FSWatcher.<anonymous> (/opt/lampp/htdocs/react-tuto/node_modules/chokidar/lib/nodefs-handler.js:482:16)
at FSReqCallback.oncomplete (fs.js:165:5)
Emitted 'error' event on FSWatcher instance at:
at FSWatcher._handleError (/opt/lampp/htdocs/react-tuto/node_modules/chokidar/index.js:260:10)
at createFsWatchInstance (/opt/lampp/htdocs/react-tuto/node_modules/chokidar/lib/nodefs-handler.js:40:5)
at setFsWatchListener (/opt/lampp/htdocs/react-tuto/node_modules/chokidar/lib/nodefs-handler.js:81:15)
[... lines matching original stack trace ...]
at FSReqCallback.oncomplete (fs.js:165:5) {
errno: -28,
syscall: 'watch',
code: 'ENOSPC',
path: '/opt/lampp/htdocs/react-tuto/public',
filename: '/opt/lampp/htdocs/react-tuto/public'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! react-tuto#0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the react-tuto#0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/paulo/.npm/_logs/2019-12-16T16_46_27_856Z-debug.log
I just tried:
sudo npm start
And it worked.
I already have the same problem . and I fix it using npm update & npm cache clean --force
Delete node_modules
rm -r node_modules
install packages again
npm install
Delete node_module directory and run below in command line
rm -rf node_modules
rm package-lock.json yarn.lock
npm cache clear --force
npm install
If still not working, try below
npm install webpack --save
For me, the problem was in firebase.json, the site name was incorrect.
For the people it didn't work, I spent two days finding the solution and there was just one command that worked like a magic.
npm install -g live-server
and then run: npm start
it will start as normal.You can read all the details here https://www.npmjs.com/package/live-server
Deleting the package-lock.json did it for me. I'd suggest you not push package-lock.json to your repo as I wasted hours trying to npm install with the package-lock.json in the folder which gave me helluva errors.
Check if port you want to run your app is free. For me, it was the problem.
Following steps solves my problem:
Add "C:\Windows\System32\" to your system path variables
Run npm eject,
Run npm start,
Run npm eject,
and agian run
npm start
And it worked😊
For me, I was trying to install an old version of bcrypt which was not found in npm, I just edited package.json and manually put the latest version and then ran npm install and it worked
first delete the file (project).
then rm -rf \Users\Indrajith.E\AppData\Roaming\npm-cache_logs\2019-08-22T08_41_00_271Z-debug.log (this is the file(log) which is showing error).
recreate your project for example :- npx create-react-app hello_world
then cd hello_world.
then npm start.
I was also having this same error but hopefully after spending 1 day on this error i have got this solution and it got started perfectly and i also hope this works for you guys also...
I had encountered the same error. And, I just simply deleted the whole node_modules folder, and installed the npm packages again solved my error.
Delete node_modules (don't worry you have package.json)
npm install
Novice mistake. Make sure your package.json file is coded correctly. I had:
"start": "index node.js"
instead of:
"start": "node index.js"
17
Delete your package-lock.json file and node_modules folder. Then do npm cache clean
npm cache clean --force
do npm install
I just do two steps:
first:
export METEOR_ALLOW_SUPERUSER=true;
second:
sudo systemctl stop bbb-html5
and finally:
npm start
My project is on wagtail. I solved this problem by running the following command:npm update & npm cache clean --force
I had the issue in React. In my case it was happening, because I didn't see warnings/errors above the message. After fixing the issues, everything worked properly. Hopefully it's going to help someone.
On Ubuntu, I restarted the server and the problem was solved.
For me, it happens because of the low version of Node. Upload my Node version and it worked successfully.

"Permission Denied" in Node on Linux, when running start-script /w local nodemon/mocha/babel-node

I'm having issues with my new linux setup - I've installed nodemon as a local dependency (NOT globally) and I'm trying to execute it from my package.json "start"-script. I'm getting this permission error.
node by itself works great, but as soon as I try to use babel-node or nodemon or even mocha, I get permission problems.
My setup is divided across 2 hard drives - node is installed on my SSD (with my OS), and my project (and it's node_modules, where I'm trying to execute nodemon from is on my storage-HDD.
sh: 1: nodemon: Permission denied
npm ERR! Linux 3.19.0-56-generic
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "start"
npm ERR! node v5.9.0
npm ERR! npm v3.7.3
npm ERR! code ELIFECYCLE
npm ERR! vyggo-easyrtc#1.0.0 start: `nodemon --exec node entry`
npm ERR! Exit status 126
npm ERR!
npm ERR! Failed at the vyggo-easyrtc#1.0.0 start script 'nodemon --exec node entry'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the vyggo-easyrtc package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! nodemon --exec node entry
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs vyggo-easyrtc
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls vyggo-easyrtc
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /media/goatic/HDD/Projects/Node/vyggo-easyrtc/npm-debug.log
goatic#goatic-laptop:/media/goatic/HDD/Projects/Node/vyggo-easyrtc$
I had this problem after changing moving my project from an NTFS filesystem to linux. I changed all the file/directory permissions to 644/755 and also set the owner/group. That's when I started getting this error.
When I tried nodemon in the terminal, I got command not found. Strange.
Reinstalling nodemon fixed it:
sudo npm install -g nodemon
My secondary drive was incompatible with my linux OS in terms of permissions, as it was an NTFS partition.
I ended up moving everything to my primary drive, and of course permissions were working again, allowing me to execute local dependencies.
I guess the solution to my problem, had I relied on keeping projects on the secondary drive, would be to format it to a format compatible with Linux permissions.
If you are installing in MAC Use.
sudo npm install -g nodemon --unsafe-perm.
It will work.
I had the same problem. In my case when I changed my operating system to Linux Ubuntu, 20.04.1 Version, reinstalling nodemon solved the problem.
For Linux users:
sudo npm install -g nodemon
for Windows users, open a new terminal or cmd prompt just write:
npm install -g nodemon
For more information check nodemon documentation:
nodemon documentation
I counter this problem when I git clone a project which initially created on a Windows machine, my simple solution is to delete the .node_modules directory and then run npm install, which solved my problem.
very weird, I had the same problem except I solved it by replacing Nodemon devStart with nodemon devStart and suddenly the error was gone :)

What does the ELIFECYCLE Node.js error mean?

What does ELIFECYCLE mean?
Here's my app code: https://gist.github.com/samholmes/388ca4552c5936b52c5d
When I run the 'blast-emails' command, it will run for a while until shortly crashing with the error:
npm ERR! Linux 3.2.0-4-amd64
npm ERR! argv "/root/.nvm/versions/io.js/v1.6.1/bin/iojs" "/root/.nvm/versions/io.js/v1.6.1/bin/npm" "run" "live"
npm ERR! node v1.6.1
npm ERR! npm v2.7.1
npm ERR! code ELIFECYCLE
npm ERR! emailer#0.0.0 live: `NODE_ENV=production node app.js`
npm ERR! Exit status 137
npm ERR!
npm ERR! Failed at the emailer#0.0.0 live script 'NODE_ENV=production node app.js'.
npm ERR! This is most likely a problem with the emailer package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! NODE_ENV=production node app.js
npm ERR! You can get their info via:
npm ERR! npm owner ls emailer
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /apps/emailer/npm-debug.log
The npm-debug.log file is also included in the gist.
I'm looking for one of two answers: What does ELIFECYCLE mean? (or) Why am I getting the error in my application code?
It's basically saying it fails to spawn your process not due to permission but to an error in your script. Source
You don't have any problem executing NODE_ENV=production node app.js ?
One might think this is because outdated versions of npm and node, but it's not the case.
Just as Pierre Inglebert says, if you look into the source you can see that End of lifecycle means the program unexpectedly stopped. This can have various reasons. So it's not a syntax error and not an expected exception/error.
The error appeared to me when a different tool was already using the http port (3000) defined in my node scripts. When you run your node app on port 80, make sure you have stopped Apache webserver (as an example).
In my case, it was because of low RAM memory, when a photo compression library was unable to process bigger photos.
While working on a WordPress theme, I got the same ELIFECYCLE error with slightly different output:
npm ERR! Darwin 14.5.0
npm ERR! argv "/usr/local/Cellar/node/7.6.0/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v7.6.0
npm ERR! npm v3.7.3
npm ERR! code ELIFECYCLE
npm ERR! foundationsix#1.0.0 postinstall: `bower install && gulp build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the foundationsix#1.0.0 postinstall script 'bower install && gulp build'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the foundationsix package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! bower install && gulp build
After trying npm install one more time with the same result, I tried bower install. When that was successful I tried gulp build and that also worked.
Everything is working just fine now. No idea why running each command separately worked when && failed but maybe someone else will find this answer useful.
If you came here like I did, after receiving a similar error when trying the React Getting Started guide, you might like to know that the problem could have been caused by not having installed Watchman. Download it here, or install it with Homebrew with brew install watchman and try again: https://facebook.github.io/watchman/docs/install.html
PS: You might want to do a brew update first.
When running Webpack build I was getting similar error because node doesn't understand async await keywords on older versions. I added webpack babel-plugin-transform-async-to-generator and it was solved. This replaces them with promises.
at process._tickCallback (internal/process/next_tick.js:10
4:9)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ng-contact-manager#0.0.0 sample: `node src/server/dat
a/seed-db.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the ng-contact-manager#0.0.0 sample script.
npm ERR! This is probably not a problem with npm. There is lik
ely additional logging output above.
npm ERR! A complete log of this run can be found in:
I have the same issue here is how I got solved finally!
the error:
my error from the terminal when i run npm run sample
after correcting my database connection username and password
I was using mlab for my database and under the file .env i forget to properly put the user name and password. When I correct that I works.
> ng-contact-manager#0.0.0 sample /Users/mohammedr.kemal/Downl
oads/Ex_Files_ANGULAR_API_AUTH/Exercise Files/Ch01/01_04/start
> node src/server/data/seed-db.js
connected to mongodb...
connected to mongodb...
2 records inserted.
closing connection...
done.
12 records inserted.
closing connection...
done.
So it might be good to look any data connection we made in our code if we have.
I had the same error after I installed new packages or updated them:
...
npm ERR! code ELIFECYCLE
npm ERR! errno 1
...
It helped me to run installation command once again or a couple of times.
After that, the error disappeared.
I had this issue when I was running two projects that had the same set up and I already had one running. This meant that the other project couldn't use that port number. As soon as I stopped the other project running I had no issues.
This issue can also occur when you pull code from git and not yet installed node modules "npm install".
The Windows solution is the same as the Linux sudo answer. Run the npm start (or whatever) as Administrator. I had added a new module to my project. Worked on some machines but on others that were more locked down, not so much. Took a while to figure it out but the new module needed access to "something" that wasn't available without administrator permissions.
I had the same error code when I was running npm run build inside node docker container.
Locally it was working while inside a container I had set option to throw error when there is a warning during compilation while locally it wasn't set. So this error can mean anything that is connected with stopping the process being done by NPM
In my case I generated a similar error when I copied the project over from another directory. some hidden files, like the critical .babelrc, were missing. SO ahhh... make sure you copy all the files!
:)
For me it was a ternary statement:
It was complaining about this line in particular, about the semicolon:
let num_coin = val.num_coin ? val.num_coin || 2;
I changed it to:
let num_coin = val.num_coin || 2;
Likewise, I saw this error as a result of too little RAM. I cranked up the RAM on the VM and the error disappeared.
I found the solution here - https://javahowtos.com/guides/124-docker/418-npm-exit-status-137-when-building-a-docker-image.html
It says - The cause of this specific npm error status usually means that Docker is going out of memory. So, when this happens, the Linux kernel just kills the process.
if you are running on ubuntu, please check your nodejs version. please update the version to the latest and clear the cache and reinstall the modules (npm install) and build.
or otherwise
please upgrade your RAM size and try again. it'll work sure.
First, you will have to remove the npm cache and upgrade to the latest version of the node and npm will resolve the issue.
sudo npm cache clean -f
sudo npm install -g n install n
sudo n stable
The first command will force clean the npm package manager's cache, After the second and third commands, a stable version of the node and npm will be installed. then you can execute npm i and npm run build for creating an optimized production build.
This solution worked for the same error on the npm run build command in nextjs.

Resources