What is index.js used for in node.js projects?

后端 未结 4 511
死守一世寂寞
死守一世寂寞 2020-12-02 16:50

Other than a nice way to require all files in a directory (node.js require all files in a folder?), what is index.js used for mainly?

4条回答
  •  自闭症患者
    2020-12-02 16:56

    Here is a good article explaining how Node.js looks for required module https://medium.freecodecamp.org/requiring-modules-in-node-js-everything-you-need-to-know-e7fbd119be8, with folder and index.js file

    Modules don’t have to be files. We can also create a find-me folder under node_modules and place an index.js file in there. The same require('find-me') line will use that folder’s index.js file:

    ~/learn-node $ mkdir -p node_modules/find-me
    ~/learn-node $ echo "console.log('Found again.');" > node_modules/find-me/index.js
    ~/learn-node $ node
    > require('find-me');
    Found again.
    {}
    >
    

提交回复
热议问题