7

So after a bit of digging around the diferences from Export and Imports declared from the package.json file I was wondering what is the best use case for both?

For example the following fields:

"name": "node-api",
"exports": {
  ".": "./application.js",
  "./config/*": "./config/*.js",
  "./controllers": "./controllers/index.js",
  "./helpers/*": "./helpers/*.js",
  "./models": "./models/index.js",
  "./routes": "./routes/index.js"
},
"imports": {
  "#config/*": "./config/*.js",
  "#controllers": "./controllers/index.js",
  "#helpers/*": "./helpers/*.js",
  "#models": "./models/index.js",
  "#routes": "./routes/index.js"
}

And then each of the following with their output in the main JS file:

import routes from './routes/index.js'; // works
import routes from './routes'; // error - ERR_UNSUPPORTED_DIR_IMPORT
import routes from 'node-api/routes'; // works (with the package name)
import routes from '#routes'; // works (without the package name but need the #)

So why not just use the imports field?

In my opinion seems friendlier than to type your package name every time you want to import your own file.

Based on the NODE JS official docs (https://nodejs.org/api/packages.html) it says the following: "The "exports" field allows defining the entry points of a package when imported by name loaded either via a node_modules lookup or a self-reference to its own name.".

Then for the imports field says the following: "it is possible to define internal package import maps that only apply to import specifiers from within the package itself."

From my testing to reference my relative (my own created) files I just use the imports field so that I don't need to type in the package for every import that I want.

So long story short, when is it best to use exports and imports field and in my case does it make sense to use only imports?

1 Answer 1

9

exports is for consumers, while imports is for the internal usage (it even uses the same prefix as private class fields). If you aren't publishing a package, then you don't need to care about exports. Its main usecase is to organize the API surface of the module without spilling all its implementation guts on the consumer.

3
  • Yep, I figured out pretty much the same as I been playing around with it, exports we can set which files to be shareable/exportable in the end, so I went in and used imports in my case. Yes for the # use it makes sense to be consistent with JS private properties/methods. Thanks! Commented Jul 27, 2022 at 14:42
  • I'm finding a specific package has some very useful utilities inside its guts, but they aren't listed in the package.json exports map. Beside forking the package and modifying the exports map, is there any way to bypass this and import the utility anyway? Commented Aug 1 at 21:07
  • That depends on your workflow and the output environment expected. These fields are only strictly respected by NodeJS runtime, so if you are running your code through a bundler then you can import whatever you want. Though it might require some typescript config fiddling to work in your IDE. Commented Aug 2 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.