1

I use Eclipse. I have downloaded node. The version is 12.16.3

Looking for modularization, sort of like Java classes import kind of thing. What I want to is to be able to write JavaScript code and separate them into several files. I looked at these Stack Overflow questions:

I have two files first.js and second.js. first.js should import and use a function from second.js, and then I should be able to run first.js.

Here is the code:

first.js:

import {aFunction} from "./second.js";
var firstNumber = 42;
var secondNumber = 100;


console.log(firstNumber);
console.log(secondNumber);  
var answer = aFunction(secondNumber);
console.log(answer);

second.js:

export var hello = 200;

export function aFunction(numberToDouble) {
    return numberToDouble * 2;
}

package.json:

{
    "type": "module"
}

All three files are in the same folder. When I run first.js as node program I get this error:

C:\Program Files\nodejs\node.exe first.js 
internal/modules/cjs/loader.js:1149
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Users\xxxx\Desktop\xxxx\xxx\first.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1149:13)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'ERR_REQUIRE_ESM'
}
brat
  • 586
  • 5
  • 17
  • That's ... a lot of text to read through. Questions like this generally don't fare well on Stack Overflow. If you can distill the text to just the main question and enough code to reproduce the error you're seeing, I think you would probably get an answer faster. – Heretic Monkey Aug 11 '20 at 19:23
  • 1
    Also note that using ES Modules in Node.js is experimental in v12: https://nodejs.org/dist/latest-v12.x/docs/api/esm.html#esm_ecmascript_modules – Heretic Monkey Aug 11 '20 at 19:30
  • 3
    your nodejs version is too old. either use --experimental-modules or switch to the current stable. – GottZ Aug 11 '20 at 19:33
  • 2
    Thanks. And damn you are fast at editing! :-o lol. I struggle with balance. If I'm to short people complain that there are no mentions of own research and solutions, if I'm to thorough, then nobody reads it x). But you are right, this one was a bit too long xD. Will edit. – brat Aug 11 '20 at 19:33
  • @GottZ *facepalm*, you where right. Was not THAT long ago I downloaded it as I remember. Also did not think all this was THAT experimental at this point in time. I downloaded latest stable and now it works, albeit with a warning that it is experimental. Make your comment into an answer and I will check it as answer. – brat Aug 11 '20 at 19:55
  • @brat I'm glad i could help you so fast. sometimes another pair of eyes is all that's needed ;) – GottZ Aug 11 '20 at 22:12

1 Answers1

1

Node 12.x comes with es6 modules behind the feature flag --experimental-modules

you either have to add it to your start directive in your package.json to get a npm start shortcut or switch to the current stable version wich ships with es6 modules out of the box.

i also suggest renaming the javascript files to .mjs to stay in convention. in future you can use .mjs and .cjs to switch between modules and common legacy code.

more on that in the official documentation: https://nodejs.org/api/esm.html

GottZ
  • 4,824
  • 1
  • 36
  • 46