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

260
Vistas
Root directory in package.json

My question concerns an existing library that I wish to publish as an NPM module. The library is already in use, and currently required via the local file system.

How can I specify the root directory of my module's files?

If I have a structure like:

.
├── package.json
├── src
|   ├── js
|   └────── lib
|   └───────── my
|   └───────────── thing.js
|   └───────────── that.js

How do I specify that the root of my module, and accessible files is src/js/lib/my/?

I would like to use as follows from an outside project:

var thing = require('my/thing'),
    that = require('my/that');

I saw the "files" property in package.json, is this the right way to go?

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

0

UPDATE 2020

The issue suggesting mainDir is now closed. Instead there is a new field called exports which can be used almost like es import maps to map a folder to an export alias:

// ./node_modules/es-module-package/package.json
{
  "exports": {
    "./my/": "./src/js/lib/my/"
  }
}
import thing from 'es-module-package/my/thing.js';
// Loads ./node_modules/es-module-package/src/js/lib/my/thing.js

As suggested in the issue linked in the original answer below it may be possible to map the root to a folder to access import thing from pkg/thing.js as so:

{
  "type": "module",
  "main": "./dist/index.js",
  "exports": {
    "./": "./src/js/lib/my/"
  }
}

Original Answer

For a native solution, see this node issue https://github.com/nodejs/node/issues/14970

The feature request suggests a mainDir field in the package.json next to main.

The more people that vote, the faster/more likely it will be implemented

over 4 years ago · Santiago Trujillo Denunciar

0

As the doc says:

The main field is a module ID that is the primary entry point to your program.

So you'll have something like "main": "src/js/lib/my/app.js" in your package.json file.

I would suggest you to create an app.js file and module.exports your different children. For example:

 module.exports.thing = require('./thing');
 module.exports.that = require('./that');

And use them like this:

var mylib = require('mylib')
  , thing = mylib.thing
  , that = mylib.that;
over 4 years ago · Santiago Trujillo Denunciar

0

package.json is mainly a file used by npm to install and manage dependencies.

the require construct does not care a lot about package.json so you will not be able to use it to subvert the way require works and make it believe that packages are not where the require loading scheme expects them.

See the documentation on https://nodejs.org/api/modules.html and the loading scheme here: https://nodejs.org/api/modules.html#modules_all_together

you could maybe use the technique that the documentation calls 'Loading from the global folders' and define the NODE_PATH environment variable.

but I advise you to stick to a more standard way : - put your modules in a node_modules directory - or start your module hierarchy in the same directory where your app.js or index.js is located

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