Node require absolute path

后端 未结 5 1530
小蘑菇
小蘑菇 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:35

    I dont think we need to discuss about this problem. It is design this way for easy of developer.
    If we require /config.json maybe there are alot config.json in the sub folder which will typically make it even harder to understand

    -- app/
       |- controller
          |- config.json(2) 
       |- models/
          |- user.js
          |- config.json(3)
       |- config.json(1)
    
    

    Now tell me how you know which one config file you need to choose.
    Solution: user.js

    var config1 = require('../config.json');
    var config2 = require('../controller/config.json');
    var config3 = require('./config.json');
    

    if there are only one config you better put in the global variable and use it from there.

提交回复
热议问题