Node.js - require empty path

后端 未结 2 1114
梦毁少年i
梦毁少年i 2020-12-11 02:30

I came across this file on github, which does this:

var migrate = require(\'../\')

What does that mean? When I tried the same, I get:

相关标签:
2条回答
  • 2020-12-11 02:58

    It requires the file index.js of the parent folder, which is this one: https://github.com/visionmedia/node-migrate/blob/master/index.js

    0 讨论(0)
  • 2020-12-11 03:01

    Here is the document from the official node js website. I just put the full steps here, but in terms of your question, find ---- here is what you wanted to know ----

    require(X) from module at path Y
    1. If X is a core module,
       a. return the core module
       b. STOP
    2. If X begins with './' or '/' or '../'
       a. LOAD_AS_FILE(Y + X)
       b. LOAD_AS_DIRECTORY(Y + X)
    3. LOAD_NODE_MODULES(X, dirname(Y))
    4. THROW "not found"
    
    LOAD_AS_FILE(X)
    1. If X is a file, load X as JavaScript text.  STOP
    2. If X.js is a file, load X.js as JavaScript text.  STOP
    3. If X.json is a file, parse X.json to a JavaScript Object.  STOP
    4. If X.node is a file, load X.node as binary addon.  STOP
    
    LOAD_AS_DIRECTORY(X) ---- here is what you wanted to know ----
    1. If X/package.json is a file,
       a. Parse X/package.json, and look for "main" field.
       b. let M = X + (json main field)
       c. LOAD_AS_FILE(M)
    2. If X/index.js is a file, load X/index.js as JavaScript text.  STOP
    3. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
    4. If X/index.node is a file, load X/index.node as binary addon.  STOP
    

    More info, please check https://nodejs.org/api/modules.html#modules_all_together

    0 讨论(0)
提交回复
热议问题