Node模块找不到错误:运行Express服务器失败求助
Express服务器启动报错:MODULE_NOT_FOUND 问题排查与解决
错误信息
node:internal/modules/cjs/loader:1063 throw err; ^ Error: Cannot find module '/Users/valemich/Desktop/code/src/vscode/calls_sales/start' at Module._resolveFilename (node:internal/modules/cjs/loader:1060:15) at Module._load (node:internal/modules/cjs/loader:905:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) at node:internal/main/run_main_module:23:47 { code: 'MODULE_NOT_FOUND', requireStack: [] }
环境信息
- 系统:macOS Ventura
- Node版本:v19.5.0
package.json 内容
}, "author": "", "license": "ISC", "dependencies": { "axios": "^1.2.5", "express": "^4.18.2" }, "devDependencies": { "ngrok": "^4.3.3" }
服务器文件代码
/* eslint-disable object-shorthand */ /* eslint-disable comma-dangle */ /* eslint-disable semi */ require('dotenv').config(); const express = require('express'); const axios = require('axios').default; // const Airtable = require('airtable'); // const base = new Airtable({ apiKey: `${process.env.AIRTABLE_TOKEN}` }).base( // `${process.env.AIRTABLE_CRM_ID}` // ); const app = express(); const port = 3000; app.use(express.json()); app.get('/', (req, res) => res.send(` <html> <head><title>Success!</title></head> <body> <h1>You did it!</h1> <img src="https://media.giphy.com/media/XreQmk7ETCak0/giphy.gif" alt="Cool kid doing thumbs up" /> </body> </html> `) ); app.post(`${process.env.AIRTABLE1_WEBHOOKS_URL}`, (req, res) => { // TODO: Update the base to show call date, call time, and questions // TODO: Create API to Calendly to find out who is assigned the call and the event name. Then post back again with information // TODO: duplicate this information to post to Slack as well (depending on event name to which channel it posts to.) const eventUrl = req.body.payload.event; const event = eventUrl.split('/')[4]; console.log(event); const questions = req.body.payload.questions_and_answers; const question = []; // eslint-disable-next-line no-undef for (i = 0; i < questions.length; i++) { // eslint-disable-next-line no-undef question.push(questions[i].question, questions[i].answer); console.log(question); } // const content = `:wave: ${username} just starred ${repoName} :taco: :rocket:`; // const avatarUrl = req.body.sender.avatar_url; axios .post(process.env.AIRTABLE2_WEBHOOKS_URL, { content: content, embeds: [ { image: { url: avatarUrl, }, }, ], }) .then((airTableResponse) => { console.log('Success!'); res.status(204).send(); }) .catch((err) => console.error(`Error sending to AirTable: ${err}`)); }); app.use((error, req, res, next) => { res.status(500); res.send({ error: error }); console.error(error.stack); next(error); }); app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`) );
用户疑问
是否需要完全卸载并重新安装Node?是不是Node安装路径错误?完全不知道问题出在哪里,请求帮助。
解决方案
不需要卸载重装Node,问题核心是Node找不到你指定要运行的文件,从报错路径/Users/valemich/Desktop/code/src/vscode/calls_sales/start来看,可从以下几个方向排查:
启动命令错误
你可能运行了node start,但你的服务器文件名并非start.js。先确认服务器文件的实际名称(比如server.js或app.js),再用正确命令启动,例如node server.js。
如果是通过npm start启动,检查package.json的scripts字段——你提供的配置里缺少这部分,需要添加类似内容(替换为你的实际文件名):"scripts": { "start": "node server.js" }文件路径错误
确认终端当前工作目录是否在/Users/valemich/Desktop/code/src/vscode/calls_sales/下,或者启动时指定正确的文件路径。比如文件在当前目录叫app.js,就运行node app.js;若在子目录,使用相对路径或绝对路径启动。文件缺失
检查/Users/valemich/Desktop/code/src/vscode/calls_sales/目录下是否存在start.js文件,若不存在,要么创建该文件,要么修改启动命令指向现有的服务器文件。
另外,你的服务器代码还有几个潜在问题,虽不影响当前启动,但后续运行可能报错:
- 缺少
dotenv依赖,需运行npm install dotenv安装; app.post回调里的content和avatarUrl变量未定义,若需要使用相关逻辑,补全变量定义;若不需要,删除对应代码避免报错。
内容的提问来源于stack exchange,提问作者Val Kuikman
相关产品推荐
相关产品推荐

