I search on the Internet and it said that the difference between require and import is that require will call entire JS file. So in my situation, does entire module.js file will be called? And if it not, in which case that file will be called entirely ?
module.js
const a = 10;
const b = 20;
const c = 30;
module.exports = { a , b };
app.js:
const nums = require('./module');
console.log(nums);
Require is Non-lexical, it stays where they have put the file. It can be called at any time and place in the program.You can directly run the code with require statement.If you want to use require module then you have to save file with ‘.js’ extension.
Import is lexical, it gets sorted to the top of the file.
It can’t be called conditionally, it always run in the beginning of the file.To run a program containing import statement you have to use experimental module feature flag.If you want to use import module then you have to save file with ‘.mjs’ extension.
For some reason using the name module removed the code highlighting so I added the comments which fixed it....
In you example the entire module file is not loaded. And it is not correct to say that it can be. Module exports is not a file based module loader. Module functionality technically could be spread over many files. But, if you create a default and put everything in it that would be nearly the same thing.
(Assuming you are only using a single file to define your module.)
module.js
export const name = /** < **/ module /** name >**/;
export default /** < **/ module /** name >**/;
You could then use named exports as before or using one of the import default syntax variations grab the entire file.
app.js
import /** < **/ module /** name> **/ from './modules.js';
or alternatively
import {default as /** < **/ module /** name> **/} from './modules.js';