最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot find module 'prompt-sync' - Stack Overflow

programmeradmin8浏览0评论

I'm making Tic Tac Toe game in JavaScript needed prompt-sync to get user inputs. After I installed prompt-sync module using npm I was getting this error whenever I tried to run this file. Did a quick google search to find that it requires npm install -g to activate it globally. Despite doing this I'm still getting the same Error. If anyone could please tell what's going wrong here!

Error:

$ 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._pile (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 ]
}

The Code

/*
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();

The path of the file is /b/Javascript Projects/Tic Tac Toe

mand used to install prompt-synce npm install -g prompt-sync

I've tried restarting VS Code several times but it's still the same.

I'm making Tic Tac Toe game in JavaScript needed prompt-sync to get user inputs. After I installed prompt-sync module using npm I was getting this error whenever I tried to run this file. Did a quick google search to find that it requires npm install -g to activate it globally. Despite doing this I'm still getting the same Error. If anyone could please tell what's going wrong here!

Error:

$ 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._pile (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 ]
}

The Code

/*
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();

The path of the file is /b/Javascript Projects/Tic Tac Toe

mand used to install prompt-synce npm install -g prompt-sync

I've tried restarting VS Code several times but it's still the same.

Share Improve this question asked Jan 12, 2021 at 8:17 ArmaanArmaan 231 gold badge1 silver badge5 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

Not sure if you still need it, but for anyone who does:

Try using npm install prompt-sync.

1.Make sure you have Node and NPM installed

2.Run npm install prompt-sync in the terminal

Use this instead in the mand prompt (I wrote it while in the same folder as the .js file but that might not be necessary)

npm i prompt-sync

Source: https://www.npmjs./package/prompt-sync

After you install the prompt package go to package.json and add "type": "module" right before the keywords, like this:

{
  "type": "module",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "prompt-sync": "^4.2.0"
  }
}
发布评论

评论列表(0)

  1. 暂无评论