簡體   English   中英

在另一個文件中導入 javascript 文件並使用 Node.js 進行編譯

[英]Importing a javascript file in another file and compiling using Node.js

為了運行我的練習 JS 文件,我使用了 Node,例如我說node bts.js 我需要一個queue數據結構,所以我安裝了 Collections 使用npm install collections --save並在圖片中看到它確實顯示在層次結構中。

然后在我的 JS 文件中我說const Deque = require('./collections/deque'); 然后后來我想像const dq = new Deque();一樣使用它但我收到“MODULE_NOT_FOUND”錯誤。 但為什么? 如果我的結構也附上屏幕截圖。

在此處輸入圖像描述

您正在嘗試使用相對路徑導入外部模塊(安裝在node_modules中) - ./collections/deque 這將在名為collections的源代碼中的本地文件夾中查找,並嘗試在其中找到一個名為deque.js的文件。 由於它不存在,因此您會收到module not found的錯誤。

您必須從node_moduels導入依賴項:

const Deque = require("collections/deque"); // see the missing ./ at the start of the path

暫無
暫無

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

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