Node require absolute path

后端 未结 5 1473
小蘑菇
小蘑菇 2020-12-30 23:31

I have a directory that looks like this:

-- app/
   |- models/
      |- user.js
   |- config.json

I want my user.js file to re

5条回答
  •  一生所求
    2020-12-30 23:34

    It seems like the module-loading functionality prepends node_modules/ to the argument. For example, place the following script into /tmp/t.js:

    meow = require('meow/meow.js');
    

    create (mkdir) the /tmp/node_modules/ and try running it with BSD's ktrace (or Linux' strace, which provides comparable functionality). Node will attempt to open the following:

    • /tmp/node_modules/meow/meow.js
    • /tmp/node_modules/meow/meow.js.js
    • /tmp/node_modules/meow/meow.js.json
    • /tmp/node_modules/meow/meow.js.node
    • /tmp/node_modules/meow/meow.js/package.json
    • /tmp/node_modules/meow/meow.js/index.js
    • /tmp/node_modules/meow/meow.js/index.json
    • /tmp/node_modules/meow/meow.js/index.node

    If you do not have the node_modules/ subdirectory next to your script, it will not lookup anywhere relative to your script at all.

提交回复
热议问题