__filename
添加於:v0.0.1
當前模塊的文件名。這是當前模塊文件的絕對路徑,符號鏈接已解析。
對於主程序,這不一定與命令行中使用的文件名相同。
當前模塊的目錄名稱參見
。__dirname
例子:
從/Users/mjr
運行node example.js
console.log(__filename);
// Prints: /Users/mjr/example.js
console.log(__dirname);
// Prints: /Users/mjr
給定兩個模塊: a
和 b
,其中 b
是 a
的依賴項,目錄結構為:
/Users/mjr/app/a.js
/Users/mjr/app/node_modules/b/b.js
在 b.js
中對 __filename
的引用將返回 /Users/mjr/app/node_modules/b/b.js
而在 a.js
中對 __filename
的引用將返回 /Users/mjr/app/a.js
。
相關用法
- Node.js __dirname用法及代碼示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代碼示例
- Node.js http2.Http2ServerRequest request.url用法及代碼示例
- Node.js request.socket用法及代碼示例
- Node.js assert.notEqual(actual, expected[, message])用法及代碼示例
- Node.js tlsSocket.authorized用法及代碼示例
- Node.js zlib.deflateRaw()用法及代碼示例
- Node.js http.IncomingMessage message.rawHeaders用法及代碼示例
- Node.js Console用法及代碼示例
- Node.js GM transparent()用法及代碼示例
- Node.js URL.protocol用法及代碼示例
- Node.js http.Agent.reuseSocket(socket, request)用法及代碼示例
- Node.js fs.filehandle.datasync()用法及代碼示例
- Node.js socket.bind()用法及代碼示例
- Node.js v8.getHeapSpaceStatistics()用法及代碼示例
- Node.js http2session.destroyed用法及代碼示例
- Node.js http.ServerResponse response.statusCode用法及代碼示例
- Node.js Buffer buf.writeBigUInt64BE(value[, offset])用法及代碼示例
- Node.js Http2ServerResponse.finished用法及代碼示例
- Node.js Http2Stream close用法及代碼示例
- Node.js readStream.isRaw用法及代碼示例
- Node.js diffieHellman.getGenerator()用法及代碼示例
- Node.js util.types.isInt16Array(value)用法及代碼示例
- Node.js certificate.verifySpkac(spkac[, encoding])用法及代碼示例
- Node.js util.types.isNativeError(value)用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 __filename。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。