我从“雄辩的JavaScript-第三版”开始学习,在第10章“模块”中,我们需要安装NPM模块'ordinal'和'date-names',book是这样使用的:
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代码中的JavaScript文件中键入此代码时,它无法识别什么是“ordinal”或“date-names”。 我使用'npm installpackage_name
'命令安装了这些软件包。 但我该如何使用它们。 并且“package.json”与Visual-Studio-Code的“launch.json”相同,因为到目前为止,我还没有制作或使用过任何名为“package.json”的文件。 如果可以,请提供一个链接来学习在JavaScript文件中使用模块的基础知识。
为了启动节点应用程序并在安装节点模块之前,您需要使用以下命令对其进行初始化
npm init
而且no launch.json不等同于package.json
launch.json文件用于在Visual Studio代码中配置调试器
JSON是一个普通的JSON(Java脚本对象表示法)文本文件,其中包含所有关于节点,JS项目或应用程序的元数据信息。