簡體   English   中英

我無法修復此錯誤:未捕獲的 ReferenceError:未定義模塊

[英]I cannot fix this error: Uncaught ReferenceError: module is not defined

我正在導出我的 js 文件,所以我可以使用導入到我的單元測試文件中。 這是我的js文件:

function getComputerChoice() {
  //stuff here
}
        
function playRound(playerSelection, computerSelection) {
  //stuff here 
}
    
function game() {
  // stuff
} 
    
module.exports = {
  getComputerChoice,
  playRound,
  game,
};

在我的測試文件中,我以這種方式導入它:

const rockPaperScissors = require('../rockPaperScissors');

test('Verify the case for tie', () => {
  expect(rockPaperScissors.playRound('rock', 'rock')).toBe('TIE');
});
test('Verify the case for win', () => {
  expect(rockPaperScissors.playRound('rock', 'scissors')).toBe('WIN');
});
test('Verify the case for lose', () => {
  expect(rockPaperScissors.playRound('rock', 'paper')).toBe('LOSE');
});

我可以讓我的測試文件工作的唯一方法是以上述格式導出,但是當我為此頁面運行 index.html 時,我看到Uncaught ReferenceError: module is not defined

requiremodule.exportsCommonJS 模塊系統的一部分,它是 Node.js 的默認模塊系統。

瀏覽器不支持 CommonJS,因此如果您想使用使用它編寫的代碼,您需要將其轉換為瀏覽器支持的格式。 通常這是使用捆綁器完成的,例如WebpackParcel

瀏覽器確實支持使用importexport標准 JavaScript 模塊(只要您使用<script type="module">加載它們),因此您也可以重寫模塊以使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM