English 中文(简体)
Node.js - 获取现有档案名称
原标题:Node.js - getting current filename
  • 时间:2013-01-07 18:03:32
  •  标签:
  • node.js

• 如何获得现有档案名称、功能名称和线号?

我想将其用于伐木/gg目的,相当于_FILE__,_LINE_

问题回答

Node.js 提供了标准预报:。 Path

因此,很容易找到目前文字的名称:

var path = require( path );
var scriptName = path.basename(__filename);

在单元内,你可以做以下任何工作,以获得全程档案名称。

this.filename;
module.filename;
__filename;

如果你只想实际的名字,没有路途或延伸,你就可以这样做。

module.filename.slice(__filename.lastIndexOf(path.sep)+1, module.filename.length -3);

To get the file name only. No additional module simply:

// abc.js
console.log(__filename.slice(__dirname.length + 1));

 // abc
console.log(__filename.slice(__dirname.length + 1, -3));
 use strict ;

const scriptName = __filename.split(/[\/]/).pop();

Explanation

console.log(__filename);
//  F:\__Storage__WorkspaceStackOverflowyourScript.js 
const parts = __filename.split(/[\/]/);
console.log(parts);
/*
 * [  F: ,
 *    __Storage__ ,
 *    Workspace ,
 *    StackOverflow ,
 *    yourScript.js  ]
 *
**/

Here we use split function with regular expression as the first parameter.
The regular expression we want for separator is [/] (split by / or ) but / symbol must be escaped to distinguish it from regex terminator /, so /[\/]/.

const scriptName = __filename.split(/[\/]/).pop(); // Remove the last array element
console.log(scriptName);
//  yourScript.js 

Don t Use This

确实,你应当使用<代码>path.basename,而(第一中记载的“noreferer”一词,因为它处理所有玉米案件,你不想知道在内部有闪lash(例如,在无意中称为“fooar”的卷宗)。 见path werhttps://stackoverflow.com/a/28319077/521957>。

You might also look at console-plus. This adds filename and linenumber to any logging text and has different colours for .log, .info and .error.

我很想把档案名称作为记录器的标签,以便最直截了当,我想的是:

__filename.substring(__dirname.length + 1, __filename.lastIndexOf("."))

我知道这已经很长一段时间了,但我是__filename.split(......)。 这将使档案名称有完整的道路,将其分为<>/code>,然后获得最后的索引,即您的档案名称。

you can use this function to get filename:

/**
 * @description 
 * get file name from path
 * @param {string} path path to get file name
 * @returns {string} file name
 * @example 
 * getFileName( getFileName/index.js ) // => index.js
 */
export default function getFileName(path) {
    return path.replace(/^.*[\/]/,   );
}

if you use nodejs, you can install the package that include this function https://www.npmjs.com/package/jotils

仅仅因为我看不到此处列出的编号,另一种选择是在<代码>_dirname 上删除<>. >.filename

__filename.replace(`${__dirname}/`,   )

I think that is useful if you don t want to import a the path module. However, I do believe that path.basename(__filename) is the most appropriate.

如果你正在使用ES单元的话:

import path from  node:path ;
const FILE_URL = import.meta.url;
const FILE_NAME = path.basename(import.meta.url);
const FILE_NAME_WITHOUT_EXT = path.basename(import.meta.url, path.extname(import.meta.url));

console.log({
  FILE_URL,
  FILE_NAME,
  FILE_NAME_WITHOUT_EXT
});

http://nodejs.org/api/esm.html#no-__filename-or-__dirname"rel=“nofollow noreferer” 。





相关问题
How to make Sequelize use singular table names

I have an model called User but Sequelize looks for the table USERS whenever I am trying to save in the DB. Does anyone know how to set Sequelize to use singular table names? Thanks.

What is Node.js? [closed]

I don t fully get what Node.js is all about. Maybe it s because I am mainly a web based business application developer. What is it and what is the use of it? My understanding so far is that: The ...

Clientside going serverside with node.js

I`ve been looking for a serverside language for some time, and python got my attention somewhat. But as I already know and love javascript, I now want learn to code on the server with js and node.js. ...

Can I use jQuery with Node.js?

Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?

How do I escape a string for a shell command in node?

In nodejs, the only way to execute external commands is via sys.exec(cmd). I d like to call an external command and give it data via stdin. In nodejs there does yet not appear to be a way to open a ...

热门标签