我已经创建了一个名为node_esm
的文件夹,并在终端npm init -y
上运行命令。创建一个package.json
。然后通过运行命令ordinal
和date-names
创建两个名为npm install ordinal
的文件夹,安装两个软件包npm install date-names
和node_modules
。之后,创建了一个文件名index.mjs
,并通过使用--experimantal-modules
标志并面对一个错误通过终端执行该文件名。
index.mjs
文件是:
import ordinal from "ordinal";
import {days, months} from "date-names";
console.log(ordinal);
console.log(months);
在ordinal
文件夹中,有两个js文件index.js
和indicator.js
index.js
文件是:
var indicator = require('./ indicator')
function ordinal (i) {
if (typeof i !== 'number') throw new TypeError('Expected Number, got ' +(typeof i) + ' ' + i)
return i + indicator(i)
}
ordinal.indicator = indicator
module.exports = ordinal
indicator.js
文件是:
module.exports = function indicator (i) {
var cent = i % 100
if (cent >= 10 && cent <= 20) return 'th'
var dec = i % 10
if (dec === 1) return 'st'
if (dec === 2) return 'nd'
if (dec === 3) return 'rd'
return 'th'
}
date-names
文件夹index.js
文件中的是:
"use strict";
module.exports = require('./en');
和en.js
文件是:
"use strict";
module.exports = {
__locale: "en",
days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
abbreviated_days: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
abbreviated_months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
am: 'AM',
pm: 'PM'
};
,错误是:
(node:8402) ExperimentalWarning: The ESM module loader is experimental.
file:///home/amarjeet/eloquentjs/ch-10%20modules_1/format-date.mjs:4
import {days, months} from "date-names";
^^^^
SyntaxError: The requested module 'date-names' does not provide an export named 'days'
at ModuleJob._instantiate (internal/modules/esm/module_job.js:80:21)
告诉我我在做什么错。 我正在使用节点版本10.13.0