get base path in javascript in javascript

In JavaScript, you can get the base path of the current file using the import.meta.url property.

Here's an example:

index.tsx
const basePath = new URL('.', import.meta.url).pathname;
console.log(basePath); // outputs the base path of the current file
125 chars
3 lines

This code uses the URL constructor to create a URL object from the import.meta.url property. Then, we access the pathname property of the resulting URL object to get the base path.

Note that this feature is only available in modern JavaScript environments that support ECMAScript modules.

related categories

gistlibby LogSnag