简体   繁体   English

如何在nodejs中获取此文件的路径?

[英]How to get the path of this file in nodejs?

I am new with Nodejs. 我是Nodejs的新手。 I want to export from file1.js file to file2.js. 我想从file1.js文件导出到file2.js。 file1.js is located in root-directory and file2.js is in some sub-directory. file1.js位于根目录中,而file2.js位于某些子目录中。 When I am calling it as require('/file1') in file2.js its saying like Uncaught Error: Module name "/file1" has not been loaded yet for context: _. Use require([]) 当我在file2.js中以require('/file1')的形式调用它时,它的说法像Uncaught Error: Module name "/file1" has not been loaded yet for context: _. Use require([]) Uncaught Error: Module name "/file1" has not been loaded yet for context: _. Use require([]) . Uncaught Error: Module name "/file1" has not been loaded yet for context: _. Use require([]) Any help? 有什么帮助吗? Sorry if this is silly, but I am new. 抱歉,这很傻,但是我是新来的。

var Logger = require('./logger');

Requires the module you have written, in a file called logger.js stored in the same directory your code was launched from.(not necessarily the same directory your code is stored in). 在一个名为logger.js的文件中需要您编写的模块,该文件存储在与您启动代码相同的目录中(不一定与您的代码存储在同一目录中)。

var someOtherModule = require('../../someOtherModule');

Requires someOtherModule.js file two directories back from where the node process is launched. 需要someOtherModule.js文件,这两个目录是从节点进程启动位置返回的两个目录。

var someOtherModule = require('./subDir/someOtherModule');

Requires someOtherModule.js file, sotred in subDir. 需要someOtherModule.js文件,位于subDir中。 subDir is a directory located at the level of where the node process was launched. subDir是位于启动节点进程的级别的目录。

var awssum = require('awssum');

Requires a module installed via NPM, in the node_modules directory from which the process was launched, or any globally installed node modules. 需要通过NPM,从其启动进程的node_modules目录中安装的模块,或任何全局安装的节点模块。 For versioning, the node_modules directory takes precedence. 对于版本控制,node_modules目录优先。

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

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