How to use the basename function from path

Find comprehensive JavaScript path.basename code examples handpicked from public code repositorys.

path.basename is a Node.js method that returns the last portion of a file path, which represents the file or directory name.

248
249
250
251
252
253
254
255
256
  destinationFolder,
  extraPath,
  prefix +
    (destinationFileBase
      ? destinationFileBase + path.extname(sourceFile)
      : path.basename(sourceFile))
);
// Copy from the source to the destination.
const sourceContents = fs.readFileSync(sourceFile, { encoding: 'utf-8' });
fork icon466
star icon582
watch icon40

3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
  }
}

object = argv._.shift();
fname = argv._.shift();
name = path.basename(argv.attachment_name || fname);
body = fs.readFileSync(fname);

if (argv.content_type) {
  opts.content_type = argv.content_type;
fork icon14
star icon8
watch icon15

+ 7 other calls in file

How does path.basename work?

path.basename is a Node.js function that takes a file path as an argument and returns the last portion of the path, which represents the filename or the directory name. It provides an optional second argument to remove the file extension from the result.

203
204
205
206
207
208
209
210
211
212
213
 */
const createReportSections = async (report, content, transaction) => {
  // add images
  const promises = (content.images || []).map(async ({path: imagePath, key, caption, title}) => {
    return uploadReportImage(report.id, key, imagePath, {
      filename: path.basename(imagePath), caption, title, transaction,
    });
  });


  // add variant sections
fork icon0
star icon5
watch icon5

398
399
400
401
402
403
404
405
406
407
408
409
  return fs.existsSync(filename);
};




const pathToLang = function(filename) {
  const lang = path.basename(filename);
  const lessonBase = `${settings.rootFolder}/lessons`;
  const lessons = `${lessonBase}/${lang}`;
  return {
    lang,
fork icon5
star icon4
watch icon3

+ 13 other calls in file

Ai Example

1
2
3
4
5
6
7
const path = require("path");

const filePath = "/home/user/Documents/example.txt";
const fileName = path.basename(filePath);

console.log(fileName);
// Output: 'example.txt'

In this example, we require the built-in path module and define a filePath variable containing the path of a file. We then use path.basename to extract the file name from the path and store it in a fileName variable. Finally, we log the fileName to the console.

110
111
112
113
114
115
116
117
118
119
}
env.cycleStack.push(fname);
// get content
let htmlContent;
let doc = env.collections.all.find((doc) => {
  return (fname === path.basename(doc.data.page.inputPath).replace(/\.[^/.]+$/, ''));
});
// default case
try {
  const mkdnContent = fs.readFileSync(doc.inputPath, { encoding: 'utf8', flag: 'r' });
fork icon0
star icon1
watch icon0

+ 41 other calls in file

56
57
58
59
60
61
62
63
64
65
            console.log(chalk.red("".concat(x, " exited with code ").concat(code)));
        j("code ".concat(code));
    });
}); };
var compile = function (o) { return spawn("server_ts", 'esbuild', ["--outdir=".concat(o.outdir), "--bundle", "--platform=node", o.ts_file]); };
var restart = function (o) { return spawn("server", 'node', [path.join('dist/api_ts', path.basename(o.ts_file).replace(/\.ts/, '.js'))], true); };
var recompile_then_start = function (o) {
    compile(o).then(function () { return restart(o); });
};
var kill_all = function () {
fork icon0
star icon1
watch icon2

+ 4 other calls in file

110
111
112
113
114
115
116
117
118
119
120
    return header;
}


function getSourcePaths(features) {
    return glob("./src/*.js").map(function(v) {
        return path.basename(v);
    }).then(function(results) {
        if (features) features = features.toLowerCase().split(/\s+/g);
        return results.filter(function(fileName) {
            if (features && optionalModuleRequireMap[fileName] !== undefined) {
fork icon0
star icon0
watch icon1

+ 6 other calls in file

5
6
7
8
9
10
11
12
13
14
15
16
const highlight = require('highlight.js')


// -----------------------------------------------------------------------------
const AppDir = path.dirname(path.dirname(path.dirname(__dirname)))
const Extensions = initExtensions()
// const PluginName = path.basename(__dirname)


exports.toHTML = toHTML
exports.extensions = Extensions.slice()

fork icon0
star icon0
watch icon0