• 如何获得现有档案名称、功能名称和线号?
我想将其用于伐木/gg目的,相当于_FILE__
,_LINE_
。
• 如何获得现有档案名称、功能名称和线号?
我想将其用于伐木/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();
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
确实,你应当使用<代码>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” 。
I am building a web application using NodeJS splitting the application into a back-end to handle database queries with MongoDB and a front end via a node based webserver that uses interactjs with ...
I have a working Express.js-based app running on Node.js. Now, I want to wrap it up with pm2. To do that, I ve defined ecosystem.json: { "apps": [ { "env": {...
I need to read a large JSON file (around 630MB) in Nodejs and insert each object to MongoDB. I ve read the answer here:Parse large JSON file in Nodejs. However, answers there are handling the JSON ...
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.
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 ...
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. ...
Is it possible to use jQuery selectors/DOM manipulation on the server-side using Node.js?
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 ...