node.js - What happens when you do "import someModuleName from someModule" in Javascript -


i understand when want reuse module @ several places. do: module.exports = yourmoduleclassname in make module exportable.

next, when want use in other places, import it, so: import yourmoduleclassname 'yourmodulepath'

i totally understand above concept , have no doubts in that. today, more concerned about, happens when import package:

like importing redux-form package:

so first do: npm install redux-form

what happens next do: import redux-form redux-form use in module.

now, when npm installed package. example; when redux-form folder under node-modules folder. it's complete project server.js , package.json of own, same happens other npm package installs also. totally independent modules(but bigger one) , work in same way local independent modules.

but didn't find module.exports there, how importable. of course, nodejs doing magic make them importable across projects. but, how doing that. let's keep module inside node_modules folder, don't importable(i know no body it, curious!). if node responsible make importable, how take care files don't want importable, want them lie in node_modules folder.

moreover, these modules have there own server.js , webpack.configs executed when call import on packages, if not how work.

i asked question because trying make independent npm package, , want understand how work. there similar question; what happens when import package? more inclined toward python. can explain question nodejs. if there available resorces online, please guide me there, highly appreciate that.

thanks , regards.

when import package installed npm, node follows these steps:

  1. it finds first node_modules folder
    • is contained in ancestor folder of file importing package and
    • contains folder named package importing
  2. if package has package.json file , file has main field,
    • node loads file set in main field
  3. otherwise, node loads index.js file.

(source: https://nodejs.org/api/modules.html#modules_all_together)


moreover, these modules have there own server.js , webpack.configs executed when call import on packages, if not how work.

webpack.config.js configuration webpack, build tool. webpack executed developer of redux-form before publishing package on npm: when npm install it, built/compiled

inside node package, server.js normal file (like foo.js). has special meaning if in application root: gets executed when run npm start.


ps mixing 2 ways of using modules: commonjs , es6. works because transpiling modules (maybe using babel, or other transpiler), might not work when node natively support es6 modules.

which differences between commonjs , es6 modules?

// commonjs module.exports = yourmoduleclassname; // es6 export default yourmoduleclassname;  // commonjs exports.something = yourexportedvariable; // es6 export { yourexportedvariable }  // commonjs var importedmodule = require("module-path"); // es6 import importedmodule "module-path";  // commonjs var = require("module-path").something; // es6 import { } "module-path"; 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -