启动Node.js应用时出现Error: Cannot find module 'compression'错误求助
Error: Cannot find module 'compression'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.(.....\node_modules\webpack-dev-server\lib\Server.js:11:18)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tr...
Hey there! This error pops up because the compression module — which webpack-dev-server relies on for its functionality — isn’t installed in your project’s dependencies. Here’s how to fix it step by step:
First, install the missing module
Navigate to your project’s root directory in the terminal, then run one of these commands based on your package manager:- For npm:
npm install compression --save-dev - For Yarn:
yarn add compression --dev
- For npm:
If the error persists after installation
Sometimes cached dependencies can cause issues. Try these steps:- Delete your
node_modulesfolder and lock file:- On macOS/Linux:
rm -rf node_modules package-lock.json - On Windows Command Prompt:
rmdir /s /q node_modules && del package-lock.json - On Windows PowerShell:
Remove-Item -Recurse -Force node_modules; Remove-Item package-lock.json
- On macOS/Linux:
- Reinstall all dependencies:
npm install(oryarn install)
- Delete your
Optional: Specify a compatible version
If you’re using an older version of webpack-dev-server, it might require a specific version ofcompression. You can install a known-compatible version like this:npm install compression@^1.7.4 --save-dev
Once you’ve done this, try starting your application again — it should work without the module not found error!
内容的提问来源于stack exchange,提问作者David Slabý

