spawn npm ENOENT, Module Not Found& fetch Not Defined — Fixed [2026]
Three common Node.js errors — all fixed in one guide
Error: spawn npm ENOENT
Error: spawn npm ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:285:19)
at onErrorNT (node:internal/child_process:483:16)
at processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 'ENOENT'
}ENOENT in child_process.spawn() means Node.js could not find the command you are trying to run. On Windows, npm is a batch file (npm.cmd) — spawn cannot find it without the extension or a shell. On all OS, the PATH inside spawn may differ from your terminal PATH.
Use shell: true (Simplest Fix)
Works on all platformsconst { spawn } = require('child_process')
// ❌ WRONG — fails on Windows (npm.cmd not npm)
const proc = spawn('npm', ['install'])
// ✅ CORRECT — shell: true routes through cmd.exe/bash
const proc = spawn('npm', ['install'], { shell: true })
proc.stdout.on('data', (data) => process.stdout.write(data))
proc.stderr.on('data', (data) => process.stderr.write(data))
proc.on('close', (code) => console.log('Exit code:', code))shell: true is the quickest fix. The slight security trade-off (shell injection risk) only matters if the args come from user input — for hardcoded commands it is perfectly safe.
Use npm.cmd on Windows Explicitly
Cross-platform spawn without shellconst { spawn } = require('child_process')
// Platform-aware command
const npmCmd = process.platform === 'win32' ? 'npm.cmd' : 'npm'
const proc = spawn(npmCmd, ['install', '--prefix', './subproject'])
proc.stdout.on('data', d => process.stdout.write(d))
proc.stderr.on('data', d => process.stderr.write(d))
proc.on('close', code => {
if (code !== 0) console.error('npm install failed with code', code)
})Use exec or execSync for npm Commands
Simpler API for short npm commandsFor running npm commands from Node.js scripts, exec is simpler than spawn and uses a shell automatically.
const { exec, execSync } = require('child_process')
// Async exec — buffers output
exec('npm install', { cwd: './subproject' }, (err, stdout, stderr) => {
if (err) console.error('Failed:', stderr)
else console.log('Done:', stdout)
})
// Sync exec — blocks until complete
try {
const output = execSync('npm install', {
cwd: './subproject',
stdio: 'inherit', // pipes output to parent process
})
} catch (err) {
console.error('npm install failed')
process.exit(1)
}Node.js Module Not Found Error
Error: Cannot find module './utils/helpers'
Require stack:
- /app/src/routes/users.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)Fix Wrong require Paths
File exists but path is wrongNode.js module not found for local files means the path in require() does not resolve to an existing file. Always verify the exact path.
// Debug: resolve what Node.js is actually looking for
console.log(require.resolve('./utils/helpers'))
// Prints the absolute path Node.js tried to find
// Common path mistakes:
require('./utils/helpers') // looks for helpers.js, helpers/index.js
require('../utils/helpers') // one level up — common mistake
require('utils/helpers') // looks in node_modules — wrong for local files
// ✅ CORRECT — use path.join for reliable cross-platform paths
const path = require('path')
const helpers = require(path.join(__dirname, '..', 'utils', 'helpers'))
// ✅ Check file structure
// src/
// routes/users.js ← this file
// utils/helpers.js ← require('../utils/helpers') ✅Case sensitivity — Linux is strict
# ❌ WRONG on Linux (but works on macOS/Windows!)
require('./Utils/Helpers') # file is utils/helpers.js
# Linux file system is case-sensitive
# macOS and Windows are not — this is a common local-works-not-on-server bug
# Fix: use the exact same case as the filename
require('./utils/helpers')Module Resolution Order
Understand how Node.js finds modulesWhen you require('something'), Node.js tries these locations in order:
| Require call | Where Node looks |
|---|---|
| require('./module') | Relative — same directory as current file |
| require('../module') | Relative — parent directory |
| require('express') | node_modules — installed package |
| require('fs') | Built-in Node.js core module |
# See exactly where Node is looking
NODE_DEBUG=module node -e "require('./utils/helpers')"
# Prints every path attempted before failingNode.js fetch Not Defined
ReferenceError: fetch is not defined
at Object.<anonymous> (/app/server.js:5:20)
at Module._compile (node:internal/modules/cjs/loader:1376:14)The browser's built-in fetch API was not available in Node.js until version 18. If you are on Node.js 17 or below, fetch does not exist globally.
Use Built-in fetch — Node.js 18+
No package needed# Check your Node version
node --version
# v18.x.x or higher → fetch works natively
# v17 or below → use Fix 7// No import needed — fetch is global in Node 18+
const response = await fetch('https://api.example.com/data')
const data = await response.json()
console.log(data)
// Full usage with options
const res = await fetch('https://api.example.com/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'John' }),
})
if (!res.ok) throw new Error('Request failed: ' + res.status)
const user = await res.json()fetch was experimental in Node 18–20 (still works) and became stable in Node 21. If you see a warning about it being experimental, upgrade to Node 21+ to remove it.
Polyfill fetch for Node.js 17 and Below
Older Node versionsOption A — node-fetch (most popular)
npm install node-fetch// CommonJS (node-fetch v2)
const fetch = require('node-fetch')
// ES Module (node-fetch v3)
import fetch from 'node-fetch'
const res = await fetch('https://api.example.com/data')
const data = await res.json()Option B — axios (alternative to fetch)
npm install axiosconst axios = require('axios')
const { data } = await axios.get('https://api.example.com/data')
const res = await axios.post('/api/users', { name: 'John' })Option C — upgrade Node.js (recommended)
# Upgrade to Node 20 LTS or 22 LTS via nvm
nvm install 20
nvm use 20
npm install # reinstall with new version
node --version # v20.x.xUpgrading to Node.js 20 LTS gives you native fetch, improved performance, and security patches. It is the cleanest long-term solution.
Frequently Asked Questions
What causes Error spawn npm ENOENT?+−
How do I fix spawn ENOENT on Windows?+−
What is the difference between spawn, exec, and execFile?+−
Node.js module not found but the file exists — why?+−
How do I use fetch in Node.js?+−
fetch is not defined in Node.js 18 — why?+−
Need Expert Help?
We Build Production-Grade Node.js Apps
Softplix builds full-stack applications. Dealing with Node.js configuration or tooling issues? We can help.
Talk to an Engineer