Reputation: 165
i have creates a folder named node_esm
and run commands on terminal npm init -y
. create a package.json
. then install two packages ordinal
and date-names
by running commands npm install ordinal
and npm install date-names
which created a folder named node_modules
. after that created a file name index.mjs
and execute it through terminal by using --experimantal-modules
flag and facing the an error.
index.mjs
file is :
import ordinal from "ordinal";
import {days, months} from "date-names";
console.log(ordinal);
console.log(months);
in ordinal
folder there is two js files index.js
and indicator.js
index.js
file is:
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
file is:
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'
}
in date-names
folder index.js
file is:
"use strict";
module.exports = require('./en');
and en.js
file is:
"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'
};
and the error is:
(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)
tell me what`s wrong i am doing. i am using node version 10.13.0
Upvotes: 1
Views: 59
Reputation: 12542
Import + destructuring using CommonJS export was removed because of confusion between valid and invalid Es6 syntax.
You can check more about these here or here
In the meantime, what you can do is (as the link suggests)
import data from './Export.js';
const {key} = data;
*[feel free to edit this answer for more accurate information]
Upvotes: 1