简体   繁体   English

Node.js - 需要空路径

[英]Node.js - require empty path

I came across this file on github, which does this: 我在github上遇到了这个文件 ,它执行此操作:

var migrate = require('../')

What does that mean? 这意味着什么? When I tried the same, I get: Error: Cannot find module '../' 当我尝试相同的时候,我得到: Error: Cannot find module '../'

它需要父文件夹的文件index.js ,这就是这个: https//github.com/visionmedia/node-migrate/blob/master/index.js

Here is the document from the official node js website. 这是来自官方节点js网站的文档。 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 更多信息,请查看https://nodejs.org/api/modules.html#modules_all_together

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM