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

191
Vistas
How to load a variable form my app into package.json?

I know this sounds as easy as using globals, but I'm not so sure.

Here's my case:

  • I have multiple files within /src directory of my React app, let's call them src/a.js, src/b.js,
  • every single of these files exports one object which I then use within my app:

./src/a.js:

export default {
  filename: 'a',
  foo: 'bar',
};

./src/b.js:

export default {
  filename: 'b',
  foo: 'bar',
  blah: 'hah',
};

Now I have a command to check whether or not structure of objects within these files match (they are being changed by many developers multiple times a day), so when I do npm check in terminal it will return false for above input, because blah does not exist within two files.

My package.json looks like this:

"scripts": {
  "check": "node check.js runCheck",
  /.../
}

My question is: how the heck do I load these variables to compare them in package.json?

I have a file called:

./check.js:

function check(files) {
 // checking files there
};

module.exports.check = check;

Approach #1 - imports

This is a build file, not part of the application itself, so when I try to do:

./check.js:

import a from './src/a';
import b from './src/b';

I'm getting:

SyntaxError: Cannot use import statement outside a module.

Approach #2 - require

This is going to cause trouble, because I'm using imports, not modules within my app, therefore doing:

./check.js:

const a = require('./src/a');
const b = require('./src/b');

Returns:

Error: Cannot find module './src/a'.

Of course I can't do module.exports within the a.js/b.js files, because they're part of my app and they should use exports, I've tried using both export and module.exports, but it does not work as well and looks shitty.

How do I tackle this? Should I load the files using some file loader, parse it as JSON an then compare? Or maybe there's an easier way?

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

0

You'll need to use something like esm (https://github.com/standard-things/esm) to run node with module support.

It should be as simple as:

npm install esm

Then update your package script to be:

"check": "node -r esm check.js runCheck",

Edit Btw, a very clear and well structured question.

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