我正在用 JavaScript 制作 Tic Tac Toe 游戏,需要提示同步来获取用户输入。使用
prompt-sync
安装 npm
模块后,每当我尝试运行此文件时,都会收到此错误。快速谷歌搜索发现它需要 npm install -g
才能全局激活它。尽管这样做了,我仍然遇到同样的错误。如果有人可以请告诉这里出了什么问题!
错误:
$ node main.js
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module 'prompt-sync'
Require stack:
- B:\JavaScript Projects\Tic Tac Toe\main.js
←[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:725:27)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:952:19)←[39m
←[90m at require (internal/modules/cjs/helpers.js:88:18)←[39m
at Object.<anonymous> (B:\JavaScript Projects\Tic Tac Toe\main.js:5:16)
←[90m at Module._compile (internal/modules/cjs/loader.js:1063:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:928:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:769:14)←[39m
←[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)←[39m {
code: ←[32m'MODULE_NOT_FOUND'←[39m,
requireStack: [ ←[32m'B:\\JavaScript Projects\\Tic Tac Toe\\main.js'←[39m ]
}
代码
/*
This project implements multiplayer Tic Tac Toe
*/
const prompt = require('prompt-sync')({sigint: true});
const clear_ouput = () => {
for (let i = 1; i < 201; i++) {
console.log();
}
}
const display_board = board => {
clear_ouput();
console.log(' | | ');
console.log(' ' + board[7] + ' | ' + board[8] +' | ' + board[9] + ' ');
console.log(' | | ');
console.log('-----------');
console.log(' | | ');
console.log(' ' + board[4] + ' | ' + board[5] +' | ' + board[6] + ' ');
console.log(' | | ');
console.log('-----------');
console.log(' | | ');
console.log(' ' + board[1] + ' | ' + board[2] +' | ' + board[3] + ' ');
console.log(' | | ');
}
const create_board = char => {
let board = [];
for (let i = 0; i < 11; i++) {
board.push(char);
}
return board;
}
// let game_board = create_board('X');
// display_board(game_board);
const decide_markers = () => {
let marker;
while ( ['X', 'O'].includes(marker) ) {
marker = prompt('Player 1, enter your marker (X - O): ');
if ( !(['X', 'O'].includes(marker)) ) {
console.log(`Sorry but ${marker} is not a valid marker.\nChoose from 'X' or 'O'.\n`);
};
}
if (marker === 'X') {
return ['X', 'O'];
}
return ['O', 'X'];
}
player_markers = decide_markers();
文件路径是
/b/Javascript Projects/Tic Tac Toe
用于安装提示同步的命令
npm install -g prompt-sync
我尝试过多次重启 VS Code 但还是一样。
不确定您是否仍然需要它,但对于任何需要它的人:
尝试使用 npm install提示同步。
1.确保已安装Node和NPM
2.在终端中运行 npm install提示同步
安装提示包后,转到package.json并在关键字之前添加“type”:“module”,如下所示:
“类型”:“模块”,
“关键字”:[],
“作者”:“”,
“许可证”:“ISC”,
“描述”:“”,
“依赖关系”:{ “提示同步”:“^4.2.0”