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

Node.js使用prompt-sync报错:无法找到模块,寻求解决方法

解决prompt-sync模块找不到的问题

错误信息

node:internal/modules/cjs/loader:998
  throw err;
  ^

Error: Cannot find module 'prompt-sync'
Require stack:
- D:\Code\C++\Concepts\conditionals.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (D:\Code\C++\Concepts\conditionals.js:1:16)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'D:\\Code\\C++\\Concepts\\conditionals.js' ]
}

用户代码

const prompt = require('prompt-sync')();
let num = prompt("Enter your age: ");
console.log(typeof num);

解决方案

  • 确保在项目目录下执行安装命令
    打开终端,切换到D:\Code\C++\Concepts目录,执行本地安装:

    npm install prompt-sync
    

    不要使用npm install -g prompt-sync全局安装,全局模块默认不会被本地项目识别;若已全局安装,需手动链接到项目:

    npm link prompt-sync
    
  • 验证安装结果
    检查项目目录下是否存在node_modules/prompt-sync文件夹,同时查看package.json的dependencies中是否包含prompt-sync条目,确认模块已正确安装到本地项目。

  • 清理缓存重新安装
    若存在缓存导致的安装异常,执行以下命令重置安装:

    npm cache clean --force
    npm uninstall prompt-sync
    npm install prompt-sync
    
  • 升级Node.js版本(可选但推荐)
    当前使用的Node.js 9.2.0是已停止维护的旧版本,存在潜在兼容性问题,建议升级到LTS版本(如18.x或20.x),可避免模块解析相关的旧版bug。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 11:40:43