Real-world Node.js and npm errors and how heal helps fix them.
$ node app.js
Error: Cannot find module 'express'
Require stack:
- /home/user/project/app.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)$ node app.js 2>&1 | healThe Express module is not installed in your project.
**Solution:**
1. Install the missing module:
```bash
npm install express
-
If you have a package.json, install all dependencies:
npm install
-
Check if node_modules exists:
ls -la node_modules/
-
If using yarn:
yarn install
### Error
```bash
$ npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! Found: react@18.0.0
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^17.0.0" from react-router-dom@5.3.0
$ npm install 2>&1 | healThere's a peer dependency conflict between React versions.
**Solution (choose one):**
1. Use --legacy-peer-deps flag (quick fix):
```bash
npm install --legacy-peer-deps
-
Use --force flag (not recommended):
npm install --force
-
Best solution: Update react-router-dom to a version compatible with React 18:
npm install react-router-dom@latest
-
Or downgrade React to version 17:
npm install react@17 react-dom@17
-
Check for updated versions:
npm outdated
### Error
```bash
$ npm start
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
$ npm start 2>&1 | healPort 3000 is already being used by another process.
**Solution:**
1. Find and kill the process using port 3000:
```bash
# On Linux/Mac
lsof -ti:3000 | xargs kill -9
# Or find the process first
lsof -i :3000
kill -9 <PID>
-
On Windows:
netstat -ano | findstr :3000 taskkill /PID <PID> /F
-
Use a different port:
PORT=3001 npm start
-
Or update your .env file:
PORT=3001
### Error
```bash
$ npm install my-private-package
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/my-private-package
npm ERR! 404 'my-private-package@latest' is not in the npm registry.
$ npm install my-private-package 2>&1 | healThe package doesn't exist in the public npm registry or you don't have access.
**Solution:**
1. Check if the package name is correct:
```bash
npm search my-private-package
-
If it's a private package, login to npm:
npm login npm install my-private-package
-
If using a private registry, configure it:
npm config set registry https://your-registry.com # or use .npmrc file
-
If it's a scoped package, ensure proper authentication:
npm config set @yourscope:registry https://npm.pkg.github.com npm config set //npm.pkg.github.com/:_authToken YOUR_TOKEN
### Error
```bash
$ npm run build
> webpack --mode production
sh: 1: webpack: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
$ npm run build 2>&1 | healWebpack is not installed or not found in PATH.
**Solution:**
1. Install webpack as a dev dependency:
```bash
npm install --save-dev webpack webpack-cli
-
Or install all dependencies:
npm install
-
Use npx to run webpack:
npx webpack --mode production
-
Update package.json scripts to use npx:
{ "scripts": { "build": "npx webpack --mode production" } }
### Error
```bash
$ node app.js
/home/user/project/app.js:5
const data = await fetchData();
^^^^^
SyntaxError: await is only valid in async function
$ node app.js 2>&1 | healYou're using `await` outside of an async function.
**Solution:**
1. Wrap your code in an async function:
```javascript
async function main() {
const data = await fetchData();
console.log(data);
}
main().catch(console.error);
-
Or use top-level await (Node.js 14.8+) with ES modules:
// In package.json, add: { "type": "module" } // Then you can use top-level await const data = await fetchData();
-
Or use .then() instead:
fetchData().then(data => { console.log(data); }).catch(console.error);
### Error
```bash
$ npm install -g typescript
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/typescript
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied
$ npm install -g typescript 2>&1 | healYou don't have permission to install global packages in the system directory.
**Solution (choose one):**
1. **Recommended**: Use a Node version manager (nvm):
```bash
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Install Node.js with nvm
nvm install node
# Now you can install global packages without sudo
npm install -g typescript
-
Change npm's default directory:
mkdir ~/.npm-global npm config set prefix '~/.npm-global' echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc source ~/.bashrc npm install -g typescript
-
Use sudo (not recommended):
sudo npm install -g typescript
-
Install locally instead:
npm install --save-dev typescript npx tsc --version
### Error
```bash
$ npm run build
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed
- JavaScript heap out of memory
$ npm run build 2>&1 | healNode.js ran out of memory during the build process.
**Solution:**
1. Increase Node.js memory limit:
```bash
export NODE_OPTIONS="--max-old-space-size=4096"
npm run build
-
Update package.json script:
{ "scripts": { "build": "node --max-old-space-size=4096 node_modules/.bin/webpack" } } -
For Windows:
set NODE_OPTIONS=--max-old-space-size=4096 npm run build -
Optimize your build:
- Enable production mode
- Remove source maps in production
- Use code splitting
- Check for memory leaks in your code