It is hard to say for sure, not knowing the contents of your main index.js and moduleA , but usually this is done so that you do not import any specific file, and the directory containing package.json is like:
import {moduleA} from "myNpmModule";
Now index.js , referenced as "main" in package.json , should import the rest of the modules and export them as its own module.exports properties.
For example, in dist/index.js :
import {moduleA} from "./moduleA"; module.exports.moduleA = moduleA;
and in your main code:
import {moduleA} from "myNpmModule";
Something like this - with possible differences according to your own module structure.
In fact, I wrote a module that automatically does something similar by importing modules into subdirectories and exporting them as properties. I did not put it on npm because it was for my own use, when I publish it on npm , I will update this answer.
Update
Here is a working example of what I described above - from import changed to require() to avoid the need for a transpilation step.
Module
The module following my answer:
Project Structure:
dist -- index.js -- moduleA -- index.js package.json moduleA.js
dist/index.js Contents:
var {moduleA} = require('./moduleA'); module.exports.moduleA = moduleA;
dist/moduleA/index.js Contents:
module.exports.moduleA = { info: 'This is what dist/moduleA/index.js exports as moduleA' };
package.json Contents:
{ "name": "nested-project-structure-example", "version": "0.0.1", "description": "An example for a Qaru answer", "main": "dist/index.js", "scripts": { "test": "node test.js" },
moduleA.js Contents:
module.exports = require('./dist/moduleA');
Using
A project that uses this module:
It can be imported as follows:
Version 1
var {moduleA} = require('nested-project-structure-example'); console.error(moduleA.info);
This imports dist/moduleA/index.js through the dist/index.js file specified in package.json . See test1.js for a working example.
Version 2
var {moduleA} = require('nested-project-structure-example/dist/moduleA'); console.error(moduleA.info);
This imports dist/moduleA/index.js , directly knowing the internal path, including dist . See test2.js for a working example.
Version 3
var {moduleA} = require('nested-project-structure-example/moduleA'); console.error(moduleA.info);
This imports dist/moduleA/index.js through the moduleA.js file into the main directory of the project. Thus, you do not need to know the internal organization of the project - the dist path is not required. See test3.js for a working example.
The whole moduleA.js project in the project:
module.exports = require('./dist/moduleA');
Without such a file in the project root directory, you cannot import moduleA without including dist in your path, or import it directly through the main js file of your project, included in package.json ( dist/index.js in this case).
These are 3 ways to achieve the goal of your question, two of which do not include dist in the code that the module imports. I hope he answers your question.
These are the only options you have, without dividing your module into a set of completely separate modules, each of which is distributed separately.