1

i have creates a folder named node_esm and run commands on terminal npm init -y. create a package.json. then install two packages ordinaland date-names by running commands npm install ordinal and npm install date-nameswhich 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

amarjeet singh
  • 115
  • 2
  • 12
  • 1
    I think you should use `export` instead `module.exports` on en.js. (or better, put it directly on date-names script) – BrTkCa Nov 27 '18 at 18:53
  • 1
    What you are exporting from `en.js` is an object with properties titled `days` and `months`. import the object and access its attributes, like `import dateNames from "date-names";` and then `let days = dateNames.days` or whatever – Katie.Sun Nov 27 '18 at 18:53
  • @LucasCosta i have used that. thats not working. – amarjeet singh Nov 27 '18 at 18:54
  • thank you @Katie.Sun. but by doing this `import {days, months} from "date-names";` what wrong in that way. that is also importing object with keys `days` and `months`. – amarjeet singh Nov 27 '18 at 19:07

1 Answers1

1

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]

Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35