Javascript: Export things inside the module, internally
Asked Answered
G

2

6

In Java, we have 4 visibility levels. Except public and private, we have protected level and a default level (with no modifier) that is also called "package-local" or "package-private".

Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y X
no modifier Y Y X X
private Y X X X

See: https://www.programcreek.com/2011/11/java-access-level-public-protected-private/


I especially need this "package-private" level in Javascript. Is there a similar way for Javascript modules?

I'm writing a library (NPM package) and I want to export something (function, class, etc.) but not in the module's public API (to be used by consumers of the library). Just to be used locally between my module's files.

Geiss answered 30/12, 2020 at 20:33 Comment(1)
Seriously? No answer? If this is truly impossible, it's a HUGE problemAbidjan
G
2

Node.js has an exports option that is defined in "package.json": https://nodejs.org/api/packages.html#exports

It can control which file(s) can be imported by the consumers (and how). It has a lot of options that unfortunately is not documented in the above official reference.


As an example, See "package.json" of my smart-color package:

"exports": {
  "./*" : "./lib/*",
  "./Color" : "./lib/Color.js",
  "./ColorCustomInspect" : "./lib/ColorCustomInspect.js",
  "./convertors" : "./lib/convertors.js",
  "./luminance" : "./lib/luminance.js",
  "./luminanceInverter" : "./lib/luminanceInverter.js",
  "./recolorFilter" : "./lib/recolorFilter.js",
  "./web-colors" : "./lib/web-colors.js"
},

The above configuration causes the consumer to be able to import the file: "./lib/Color.js" this way:

import Color from 'smart-color/Color' // Not from '.../lib/Color[.js]'

As other examples, see "package.json" of dotenv package and "package.json" of server-only package.

Geiss answered 3/6, 2023 at 7:22 Comment(0)
A
2

Breaking TypeScript NPM package into multiple files without exposing internal components

This seems to do the trick: package.json defines what is actually exported from an npm package. Everything can still be imported from anywhere inside the project but importing is explicit. What is not explicitly exposed via package.json can not be imported from another project consuming the package.

Abidjan answered 7/3, 2021 at 7:6 Comment(0)
G
2

Node.js has an exports option that is defined in "package.json": https://nodejs.org/api/packages.html#exports

It can control which file(s) can be imported by the consumers (and how). It has a lot of options that unfortunately is not documented in the above official reference.


As an example, See "package.json" of my smart-color package:

"exports": {
  "./*" : "./lib/*",
  "./Color" : "./lib/Color.js",
  "./ColorCustomInspect" : "./lib/ColorCustomInspect.js",
  "./convertors" : "./lib/convertors.js",
  "./luminance" : "./lib/luminance.js",
  "./luminanceInverter" : "./lib/luminanceInverter.js",
  "./recolorFilter" : "./lib/recolorFilter.js",
  "./web-colors" : "./lib/web-colors.js"
},

The above configuration causes the consumer to be able to import the file: "./lib/Color.js" this way:

import Color from 'smart-color/Color' // Not from '.../lib/Color[.js]'

As other examples, see "package.json" of dotenv package and "package.json" of server-only package.

Geiss answered 3/6, 2023 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.