[英]How to import or use installed modules from 'node_modules' in my javascript file
我一直在學習“Eloquent JavaScript-第三版”,在第 10 章“模塊”中,我們需要安裝 NPM 模塊 'ordinal' 和 'date-names',書上這樣使用它:
const ordinal = require("ordinal");
const {days, months} = require("date-names");
exports.formatDate = function(date, format) {
return format.replace(/YYYY|M(MMM)?|Do?|dddd/g, tag => {
if (tag == "YYYY") return date.getFullYear();
if (tag == "M") return date.getMonth();
if (tag == "MMMM") return months[date.getMonth()];
if (tag == "D") return date.getDate();
if (tag == "Do") return ordinal(date.getDate());
if (tag == "dddd") return days[date.getDay()];
});
};
const {formatDate} = require("./format-date");
console.log(formatDate(new Date(2019, 8, 13),
"dddd the Do"));
// ! Friday the 13th
但是,當我在 Visual Studio Code 的 JavaScript 文件中鍵入此內容時,它無法識別什么是“序數”或“日期名稱”。 我使用“npm install package_name
”命令安裝了這些包。 但是我該如何使用它們。 並且“package.json”是否與 Visual-Studio-Code 的“launch.json”相同,因為到目前為止,我還沒有制作或使用過任何名為“package.json”的文件。 如果可以,請提供一個鏈接來學習在 JavaScript 文件中使用模塊的基礎知識。
要啟動節點應用程序並在安裝節點模塊之前,您需要使用此命令對其進行初始化
npm init
並且沒有啟動。json 不等同於 package.json
launch.json 文件用於在 Visual Studio Code 中配置調試器
package.json 是一個純 JSON(Java Script Object 表示法)文本文件,其中包含有關 Node JS 項目或應用程序的所有元數據信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.