Node.jsJavaScriptError Fix2026 Updated

spawn npm ENOENT, Module Not Found& fetch Not Defined — Fixed [2026]

Three common Node.js errors — all fixed in one guide

January 20266 min readNode.js · All OS
Part 1Error: spawn npm ENOENT

Error: spawn npm ENOENT

Terminal Error
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.

1

Use shell: true (Simplest Fix)

Works on all platforms
1 min
server.js
const { 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.

2

Use npm.cmd on Windows Explicitly

Cross-platform spawn without shell
1 min
server.js
const { 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)
})
3

Use exec or execSync for npm Commands

Simpler API for short npm commands
2 min

For running npm commands from Node.js scripts, exec is simpler than spawn and uses a shell automatically.

scripts/install.js
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)
}
Part 2Node.js Module Not Found Error

Node.js Module Not Found Error

Terminal 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)
4

Fix Wrong require Paths

File exists but path is wrong
2 min

Node.js module not found for local files means the path in require() does not resolve to an existing file. Always verify the exact path.

routes/users.js
// 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

terminal
# ❌ 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')
5

Module Resolution Order

Understand how Node.js finds modules
2 min

When you require('something'), Node.js tries these locations in order:

Require callWhere 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
terminal — debug module resolution
# See exactly where Node is looking
NODE_DEBUG=module node -e "require('./utils/helpers')"
# Prints every path attempted before failing
Part 3Node.js fetch Not Defined

Node.js fetch Not Defined

Terminal Error
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.

6

Use Built-in fetch — Node.js 18+

No package needed
1 min
terminal
# Check your Node version
node --version
# v18.x.x or higher → fetch works natively
# v17 or below → use Fix 7
server.js — Node 18+ native fetch
// 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.

7

Polyfill fetch for Node.js 17 and Below

Older Node versions
3 min

Option A — node-fetch (most popular)

terminal
npm install node-fetch
server.js — CommonJS
// 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)

terminal
npm install axios
server.js
const 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)

terminal
# 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.x

Upgrading 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?+
ENOENT in spawn means the command cannot be found. On Windows, npm is npm.cmd not npm — use spawn('npm.cmd') or add { shell: true }. On any OS, the PATH in the spawn environment may not include Node's bin directory.
How do I fix spawn ENOENT on Windows?+
Use spawn('npm.cmd', args) instead of spawn('npm', args) on Windows. Or use { shell: true } option: spawn('npm', ['install'], { shell: true }). This runs through cmd.exe which handles .cmd extensions.
What is the difference between spawn, exec, and execFile?+
spawn streams output and is best for long-running processes. exec buffers output and is best for short commands. execFile is like exec but does not use a shell — safer for user input. For running npm commands, exec is usually simplest.
Node.js module not found but the file exists — why?+
Check the require path — case sensitivity matters on Linux (not on macOS/Windows). Also check the file extension: require('./utils') only works if utils.js, utils/index.js or utils.json exists. Log the exact path: console.log(require.resolve('./utils')).
How do I use fetch in Node.js?+
Node.js 18+ includes fetch natively — just use it: const res = await fetch('https://api.example.com/data'). On Node.js 17 or below, install node-fetch: npm install node-fetch, then import fetch from 'node-fetch'.
fetch is not defined in Node.js 18 — why?+
On Node.js 18+, fetch is available globally but was marked experimental until Node 21. If you are on 18–20, it should work without importing. If not, try: import { fetch } from 'undici' — undici is the underlying HTTP client Node.js uses.

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