I have a issue with npm and the main field. I see the documentation and as of my understanding I point main to be a different entry point than ./index.js. I already tested the package where all dist files are inside the root folder. I ignore src and test during pack phase using .npmignore but I did not like the point that building and packing the project to verify the structure pul all my files into the package root folder. So i changed the output to be dist instead.
If i use npm pack and extract the file I get the following structure:
/
dist
-- index.js
-- moduleA
-- index.js
package.json
README.md
So for so good. But now I am forced to import it as follows:
import {moduleA} from "myNpmModule/dist/moduleA";
But I dont want to have the dist folder in my import. So I set main in package.json
"main": "dist/index.js"
But still it does not work and only works if I import with dist. I use npm 3.10.7 and node 6.7.0.
Can anyone help?
Regards
So here is what I understand of how this works. I am not 100% sure it that is true. I gained this insight from plain observation and reasoning rather from actual seeing this in a doc. assumption 1 (package.json):
assumption 2 (package structure):
doing the above you get:
however it seems like that:
is not equivalent to the statement using require above. What you could do instead id:
assuming you still want to have the direct import from moduleAA with the above given project structure you would need to do:
So here is my conclusion and how i understand this.
... from "my-package/dist/moduleA/moduleAA"; does not look through the project structure from a JS/npm point of view (what is exported) but instead it looks at the file structure of the package as soon as you use a / in the from phrase.
Which means that if you use
it will actually import all exports from dist/index.js but if you import "my-package/moduleA" it actually looks inside the package if the path "/moduleA" exists. In the above case that is not true. If we omit the dist folder and move the structure into the package root that statement would actually work the way you would expect.
So no one can ask why I want to have this stuff in dist folder? It is easy to ignore in git. As I understand the best practise using node you use the "prepublish" step to actually build your package. which means if you make a fresh checkout of the code and run "npm install" which executes "npm run prepublish" by design it spams the folder structure with the packaged and transformed files.
After playing with this a few hours I gave up and just accept that "npm install" would potentially spam my folders. As an alternative I could not define prepublish in my package.json and just run "npm run build" prior to "npm publish". ("scripts": { "build": "babel src --out-dir ." })
It's hard to tell for sure not knowing the contents of your main
index.js
andmoduleA
but it's usually done in a way that you don't import any specific file, but rather the directory containing thepackage.json
- like:Now, the
index.js
referenced as "main" inpackage.json
should import the rest of the modules, and export them as its ownmodule.exports
properties.For example, in
dist/index.js
:and in your main code:
Something like that - with possible differences to suits your own module's structure.
Actually I wrote a module that automatically does something like that, importing modules in subdirectories and exporting them as properties. I haven't put it on
npm
because it was for my own use, when I publish it tonpm
I'll update this answer.Update
Here is a working example of what I described above - with
import
changed torequire()
to avoid the need for a transpilation step.Module
A module following my advice from this answer:
Project structure:
dist/index.js
contents:dist/moduleA/index.js
contents:package.json
contents:moduleA.js
contents:Usage
A project that uses this module:
It can be imported like this:
Version 1This imports the
Version 2dist/ModuleA/index.js
via thedist/index.js
file referenced inpackage.json
. See test1.js for a working example.This imports the
Version 3dist/ModuleA/index.js
directly knowing the internal path includingdist
. See test2.js for a working example.This imports the
dist/ModuleA/index.js
via themoduleA.js
file in the main project directory. That way doesn't need to know the internal project organization -dist
path is not needed. See test3.js for a working example.The whole content of the
moduleA.js
in the project is:Without having such a file in your project's root directory you will not be able to import the
moduleA
without either including thedist
in your path or importing it directly via the mainjs
file of your project included inpackage.json
(dist/index.js
in this case).Those are 3 ways to achieve the goal of your question, two of which don't include the
dist
in the code that imports the module. I hope it answers your question.Those are the only options that you have without splitting your module into a set of completely separate modules, each distributed separately.