Skip to content Skip to sidebar Skip to footer

Node.js - Javascript Calling Functions From Javascript File

I am working on a Express/NodeJs project. I am new to Express/NodeJs, I am trying to import airportQuery.js into DistanceFormula.js. I am trying to directly import airportQuery.js

Solution 1:

To use modules in node.js, you have to do the following:

  1. Be running a version of nodejs that supports ESM modules (v8.5+).
  2. Run with this command line flag: node --experimental-modules
  3. Name your file with an .mjs file extension OR specify it as a module in package.json

See the relevant documentation for more info.

This is true not only for the top level file you import, but if it also uses import, then the same rules above have to apply to it too.


Note, that once you get your modules to load properly, you will then have a problem with this line of code because getAirports() returns promise, not a value. All async functions return a promise, always. The return value in the function will become the resolved value of the returned promise. That's how async functions work. So, change this:

console.log(getAirports('3c675a'));

To this:

getAirports('3c675a').then(result=> {
    console.log(result);
}).catch(err => {
    console.log(err);
});

Post a Comment for "Node.js - Javascript Calling Functions From Javascript File"