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

Node.js运行报错:找不到指定模块/无调试器可用,求解决方法

问题解决:Node.js找不到模块及Twilio API代码修正

一、解决"Cannot find module '/Users/cliu21/scratch/app.js'"错误

按以下步骤排查:

  • 确认终端当前路径:执行pwd命令,查看输出是否为/Users/cliu21/scratch。若不是,用cd /Users/cliu21/scratch切换到该目录后,再运行Node命令。
  • 检查文件是否存在:执行ls命令,确认app.js确实在当前目录下。
  • 修正执行命令:若不想切换目录,直接用完整路径执行,比如:
    /opt/homebrew/bin/node /Users/cliu21/scratch/app.js
    

二、代码中的错误修正

即便文件找到,你的代码还存在调用问题,修改如下:

// Download the helper library from https://www.twilio.com/docs/node/install
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

console.log("Starting Program");
client.messages
  .list()
  .then(messages => messages.forEach(m => console.log(m.sid)))
  .catch(err => console.error('获取消息列表失败:', err)); // 新增错误捕获,方便排查问题
    
console.log("Gathering message log");

错误点说明:client.messages.list()返回的Promise resolve后直接是消息数组,无需调用.array,直接用messages.forEach即可。

三、环境变量设置注意事项

确保Twilio的账号SID和Auth Token已正确设置:

  • 终端临时设置:执行以下命令后再运行Node程序:
    export TWILIO_ACCOUNT_SID='你的账号SID'
    export TWILIO_AUTH_TOKEN='你的授权Token'
    
  • 持久化设置:可以使用dotenv包,创建.env文件存储变量,在代码顶部添加require('dotenv').config()来加载。

内容的提问来源于stack exchange,提问作者Claire

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 20:20:09