What methods can be used to conceal internal-only modules from users and consumers of a module?

I have been creating various node packages utilizing ES Modules. Within these packages, some modules are meant to be utilized externally by other packages, while certain module exports are intended for internal use only within my own package.

From my understanding, there is currently no built-in method to designate certain modules as strictly internal in ES modules. Are there any established best practices or guidelines that developers follow when developing node packages to discourage the utilization of internally designated modules?

The need for this arises because my packages are coded in TypeScript and the automatic generation of declaration files by TypeScript includes exports for modules that are intended for internal use only.

Answer №1

With the release of Node version 18 and above, the introduction of the exports attribute has made it possible to specify a precise list of modules to export:

{
  "name: "my-package",
  "version": "1.0.0",

  "exports": {
    ".": "./dist/main.js",
    "./common": "./dist/common.js",
    "./other.js": "./dist/other.js"
  }
}

The use of this feature in Node.js ESM implementation mandates accuracy with file extensions. With the configuration mentioned above, the imports in the consuming application will look like this:

// Importing from `.`
import {} from 'my-package';

// Importing from `./common`
import {} from 'my-package/common';

// Importing from `./other.js`
import {} from 'my-package/other.js

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Ultimate Showdown: Backbone.js Model versus View Battle

When utilizing Backbone, which method is more suitable for setting data - within the Model or the View? // Logic in the Model // Can be accessed from any relevant part of the codebase SomeModel = Backbone.Model.extend({ defaults: { visible: false ...

Using css with display:none and display:block is not allowed

When a user selects one of the two choices, questions appear. This functionality is working fine, but as soon as I make any changes to the CSS, it stops working. I am using display:none and display:block for this purpose. You can find a link to the fiddle ...

Disable sorting options in the Datatable ColumnFilterWidget

I'm currently working with datatables ColumnFilterWidget and I'd like to prevent the widget from sorting the values displayed in the select box. Despite trying the "bSort": false option of the ColumnFilterWidget, it doesn't seem to have any ...

What steps can be taken to prompt an action before a URL is loaded in a specific browser?

I am attempting to perform an action before a specific URL is loaded in the <browser> element, whether it appears in a sidebar or tab. For instance: (in the context of the browser's main window): // This could also be the browser within a tab ...

Comparing identical elements in JavaScript

Crucial Note I want to clarify that I have no intentions of scamming, phishing, or causing any harm. My goal is to develop a website magnifier similar to using a magnifier on paper. Dilemma Let me paint the picture for you. I've effectively copied ...

Dealing with prolonged response times when creating a web page using PHP HttpRequest

Currently, I am utilizing javascript and XMLHttpRequest on a static HTML page to showcase a record in Zotero. Everything is functioning well apart from one issue: the HTML title of the page. I do have the option to modify the <title>...</title> ...

Switching views in AngularJS by using a controller to control the content displayed in the

My JavaScript web app utilizes AngularJS to simplify tasks, but I've encountered an issue. I'm trying to change views from an ng-controller using $location.path, but for some reason, the view isn't updating even though the path in the $loca ...

Identifying Changes in ReactJS Pages

As I delve into learning ReactJS, I have been experimenting with various websites that utilize this technology. Presently, my focus is on making an AJAX call to an API based on the URL of a page. The issue arises when clicking a link breaks my code due t ...

Scaling and mapping textures onto meshes using Three.js TextureLoader

I have a cube and I'm attempting to project an image onto it. I am using a loading Manager to load the image, but I am encountering an issue where material.map is showing as undefined. I suspect that there may be a problem with scaling. The original i ...

Is it possible to swap out images using srcset attribute?

Currently, I am facing an issue regarding changing the img on mobile and tablet devices to display different images. As a beginner with jQuery, I couldn't resolve it using that framework, so I am exploring options with html5. I am wondering if there ...

Angular 2 validation issue not functioning as anticipated

Here is a validator function that I have: export const PasswordsEqualValidator = (): ValidatorFn => { return (group: FormGroup): Observable<{[key: string]: boolean}> => { const passwordCtrl: FormControl = <FormControl>group.contr ...

Missing from the TypeScript compilation are Angular5's polyfills.ts and main.ts files

Here is the structure of my Angular5 project. https://i.stack.imgur.com/tmbE7.png Within both tsconfig.app.json and package.json, there is a common section: "include": [ "/src/main.ts", "/src/polyfills.ts" ] Despite trying various solu ...

A guide on using wrapAll within an each loop in jQuery

I have a series of div elements in my HTML code: <div class="mainClass class_1"></div> <div class="mainClass class_1"></div> <div class="mainClass class_2"></div> <div class="mainClass class_2"></div> <di ...

Resolving React JS API call problem with 400 Error

I'm currently developing a weather application using reactjs and the geolocation(navigator) API along with the darksky weather API. I have successfully implemented the display of longitude and latitude, but I am encountering an error when trying to fe ...

Unallocated functions found within Web Sockets Objects

I am currently utilizing javascript (p5.js) in combination with node.js using express and socket.io. Within my code, I believe the issue lies within this specific section: for (var i = 0; i < 3; i ++){ for (var j = 0; j < 3; j ++){ ...

Upon subscribing to an observable, the initial value is invariably null

Here is an example of the ProfileService I am currently using: export class ProfileService { private user: BehaviorSubject<User> = new BehaviorSubject<User>(null); constructor(private userService: UserService) { this.userService.getUs ...

Issue with Vue.js: "Load more data" button functionality malfunctioning

I've been working on my Vue.js code and I'm trying to implement a "Show More" button for the data fetched from an API. Initially, only 10 items should be displayed, and when the button is clicked, it should load another 10 items and so on. I chec ...

The NodeJS application experiences a crash if incorrect parameters are provided to the API

Currently, I have built a CRUD API using TypeScript with Node.js, Express, and MongoDB. My goal is to ensure that the API functions correctly when the correct parameters are sent through a POST request. However, if incorrect parameters are passed, the Node ...

You are able to use a null type as an index in angular.ts(2538) error message occurred

onClick() { let obj = { fName: "ali", LName: "sarabi", age: "19", } let fieldName = prompt("field"); alert(obj[fieldName]); } I encountered an issue with the code above where alert(obj[fieldName] ...

What is the best way to handle .jsx files in my library bundling process with Rollup?

Currently, I am in the process of developing a component library using storybook and rollup. Here is the configuration file rollup.config.mjs /* eslint-disable import/no-extraneous-dependencies */ import peerDepsExternal from 'rollup-plugin-peer-deps- ...