Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

314
Vistas
How to get relative path to an imported file in node.js

I have the following file:

import {something} from '../../something';

something.do();

And I want to get something's relative path as a string, but dynamically rather than to write it implicitly. Something like this:

import {something} from '../../something';

getPath(something); // will return '../../something'

something.do();

What are my options? Is it even possible?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can get a relative path to an imported file as follows

FIRST APPROACH: (the imported file is modified):

  1. Assume that you have a test folder. Inside the test folder, you have test1.js and /mod/test2.js files.

test/mod/test2.js

// here are some of your codes

export const importedFilePath = import.meta.url;  // export current file absoulte path
  1. Now inside the test/test1.js file:

path.relative(from, to) method returns the relative path from to to based on the current working directory. If from and to each resolve to the same path (after calling path.resolve() on each), a zero-length string is returned.

If a zero-length string is passed as from or to, the current working directory will be used instead of the zero-length strings. Documentation

import path from 'path'
import { importedFilePath } from './mod/test2.js';

const currentFilePath = import.meta.url;

console.log(path.relative(currentFilePath, importedFilePath)); 

Output:

..\mod\test2.js

Now you have the relative path from test/test1.js to test/mod/test2.js

I know this is not a perfect solution but you can achieve the result you want using the above approach.

SECOND APPROACH

If you would be using the CommonJS module system you can be achieved this result without modifying the imported file.

Assume you use the CommonJS module. Without any modification to the imported file you can achieve the same result as above as follows:

test/test1.js file:

const file = require('./mod/test2');
const path = require('path');

const importedFilePath=require.cache[__filename].children[0].id; //this returns imported file path
const currentFilePath = __filename;

console.log(path.relative(currentFilePath, importedFilePath)); 

And output:

..\mod\test2.js
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda