You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用require引入data-service.js报错:Cannot find module问题求助

Fixing "Cannot find module 'data-service.js'" in Your Web Project

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:38:50