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

258
Vistas
Importing modules using ES6 syntax and dynamic path

This works:

import app from './../app.js';

But this doesn't:

import app from path.join(process.cwd(), 'app');

I'm Getting:

SyntaxError: /path/file.js: Unexpected token (5:16)
> 5 | import app from path.join(process.cwd(), 'app');
    |                 ^

It is possible (and/or how) to use "dynamic" paths? (not hardcoding the path or rely in relative paths).

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

No, this is not possible. ES6 modules need to be able to statically resolve their dependencies, without executing module code, so that import statements do work reliably. The module specifier must be a string literal.

However, the module loader of your choice should support dynamic loading of modules with variable names. You wouldn't be able to get a bound app identifier in your module scope however (and cannot reexport it), it typically would only be available in a callback or so.

over 4 years ago · Santiago Trujillo Denunciar

0

ES6 imports are declarative and meant for static analysis. They cannot be dynamic.

Generally the expectation would be that if you had some conditional imports, it would be the responsibility of the underlying path resolution logic to decide what module to load based on some static path.

That or you'd use the async module loader rather than declarative imports.

over 4 years ago · Santiago Trujillo 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