Last updated: Apr 4, 2024
Reading time·5 min
The "npm ERR! code ENOTEMPTY" issue when running npm install
occurs when you
have a glitched node_modules
directory.
To resolve the issue, delete your node_modules
directory and your dist
directory if you use Webpack and reinstall your dependencies.
Here is the complete stack trace.
npm ERR! code ENOTEMPTY npm ERR! syscall rename npm ERR! path /home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-react/node_modules/acorn-globals npm ERR! dest /home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-react/node_modules/.acorn-globals-44KOYGBS npm ERR! errno -39 npm ERR! ENOTEMPTY: directory not empty, rename '/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-react/node_modules/acorn-globals' -> '/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-react/node_modules/.acorn-globals-44KOYGBS' npm ERR! A complete log of this run can be found in: /home/borislav/.npm/_logs/2023-04-19T16_44_30_279Z-debug-0.log
The first thing you should try is to delete your node_modules
folder and
reinstall your dependencies.
If you are on Windows, open CMD in your project's root directory (where your
package.json
file is) and issue the following commands.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock npm cache clean --force npm install
If you are on macOS or Linux, open bash
or zsh
in your project's root
directory and issue the following commands.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock npm cache clean --force npm install
dist
directory and rebuild your projectIf the issue persists and you use Webpack, delete your dist
(or build
)
directory and rebuild your project.
Assuming your output directory is called dist
, you can issue the following
command.
# on Windows rd /s /q "dist" # on macOS or Linux rm -rf dist
Make sure to rebuild your project after deleting your dist
(or build
)
directory.
Try to run the npm install
command after rebuilding your project.
npm install
If the error persists, try to:
Ctrl
+ C
.npm install
command.npm install
If you have yarn
, you can also try to run the yarn install
command to check
if it succeeds.
yarn install
If the error persists, try to run the npm install
command with the
--legacy-peer-deps
flag.
The flag ignores all peer dependencies when installing (in the style of npm
v4-6).
npm install --legacy-peer-deps
If the error persists, you might have to delete your node_modules
directory
and reinstall your modules multiple times.
Here are the commands for Windows.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock npm cache clean --force npm install
And here are the commands for macOS and Linux.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock npm cache clean --force npm install
react-scripts
If you use the Create React App package, try to
update your version of the react-scripts
module.
Open your terminal in your project's root directory and run the following command.
# with NPM npm install react-scripts@latest # ---------------------------------------------- # or with YARN yarn add react-scripts@latest
If you use the integrated terminal in your IDE,
restart your code editor and rerun the
npm install
command.
# with NPM npm install # or with YARN yarn install
Another thing you can try is to update your NPM version.
Open your terminal and issue the following command.
npm install -g npm@latest
If you still get the "npm ERR! code ENOTEMPTY" error, delete your node_modules
folder and rerun the command.
If you get a permissions error when running the command:
sudo
.sudo npm install -g npm@latest
You can type CMD in the search field.
Right-click on the CMD application and select "Run as an administrator".
Rerun the npm update command.
npm install -g npm@latest
Try to rerun the npm install
command after having updated npm
.
# with NPM npm install # or with YARN yarn install
If the error persists, try to install the long-term supported version of Node.js.
There are 2 main ways to install the LTS version.
The NVM package is very convenient because it allows you to switch between Node.js versions by issuing a single command.
If you already have NVM installed, issue the following commands on macOS or Linux.
# NVM for macOS and Linux nvm install --lts nvm use --lts
If you are on Windows and you have NVM installed, run the following commands to install the LTS version.
# NVM for Windows nvm install lts nvm use lts
If you don't have NVM installed, follow the instructions in my operating system-specific articles:
After you install NVM, install the LTS version of Node.js and try to rerun the
npm install
command.
# with NPM npm install # or with YARN yarn install
In some of my projects, the only way to get rid of the error is to delete my
node_modules
directory and reinstall my dependencies multiple times.
Here are the commands for Windows.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock npm cache clean --force npm install
And here are the commands for macOS and Linux.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock npm cache clean --force npm install
You can learn more about the related topics by checking out the following tutorials: