使用require引入data-service.js报错:Cannot find module问题求助
Hey Kevin, let's break down why you're hitting this error and how to fix it quickly:
Common Causes & Solutions
1. Missing Relative Path in require()
Node.js doesn't automatically look for files in your current working directory when you use require('data-service.js') — it first checks core modules and the node_modules folder. To load a local file, you need to add a relative path prefix:
Change your require statement in server.js from:
require('data-service.js')
To:
require('./data-service.js')
The ./ tells Node.js to look in the same directory as server.js.
2. Filename Mismatch (Including Case)
Double-check that your file is exactly named data-service.js — even small typos or case differences (like Data-Service.js or data_service.js) will cause this error. Windows might ignore case, but Node.js follows the exact filename as stored on your system.
3. File Location Doesn't Match
Make sure data-service.js is in the same folder as server.js (which is C:\Users\Kevin Chan\Desktop\web322-app\). If it's in a subfolder, adjust the path accordingly. For example, if it's in a services subfolder:
require('./services/data-service.js')
4. Quick Sanity Check
Even though your data-service.js is empty, that's not the issue here — Node.js can load empty modules without errors. The problem is definitely related to how you're referencing the file.
Once you fix the path/filename, the error should disappear, and you can start adding your JSON data logic to data-service.js!
内容的提问来源于stack exchange,提问作者Kelvin Chen

