What is the process for importing a submodule from a private package?

I'm currently working on a private npm package in TypeScript that needs to be reused in multiple TS projects. To streamline this process, I created and published the @myorg/core package, containing enums and types that are shared across different repositories.

In the tsconfig.json file of the package, I have set "declaration": true to export API descriptions into .d.ts files upon building.

The project structure involves:

 src/
 ├── enumerators/
 │   └── index.ts
 ├── database-models/
 │   └── index.ts
 ├── external-apis/
 │   └── index.ts
 └── index.ts

The purpose of src/index.ts is to export all resources with the following code:

export * as ExternalAPIs from './external-apis';
export * as Enumerators from './enumerators';
export * as DatabaseModel from './database-model';

My goal now is to figure out how to directly import these resources. Instead of using:

import * as MyOrgCore from '@myorg/core'

I would prefer to use:

import * as Enumerators from '@myorg/core/Enumerators'

After researching online, I found suggestions regarding the exports field in the package.json file, simplified as shown below:

{
  "name": "@myorg/core",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "exports": {
    ".": "./dist/index.js",
    "./Enumerators": "./dist/enumerators/index.js",
    "./ExternalAPIs": "./dist/external-apis/index.js",
    "./DatabaseModels": "./dist/database-model/index.js"
  }
}

Despite implementing this, I still encounter the error message:

Cannot find module '@myorg/core/Enumerators' or its corresponding type declarations.ts(2307)

So my final question remains: what is the correct approach for achieving this? And is it considered a good practice?

Answer №1

It seems that the solution lies within the tsconfig.json file of projects utilizing the core package. Simply including

"moduleResolution": "Node16"
in the configuration file did the trick.

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

Using the *ngFor directive within an <ng-container> element and wrapping it in an <ng-template> is a great way to dynamically display various data in

I have encountered an issue with my HTML file. The initial content within the ng-container tag is displaying data correctly. However, upon clicking the button to open the details panel, which utilizes the ng-template, only the first data entry from the arr ...

What is the reason behind the checkbox event status returning the string "on" rather than true/false?

I have implemented a custom checkbox as a child component within a parent component. I properly pass the ngModel, name, etc., and attempt to update the model with a boolean status (true/false) based on the checkbox status using an EventEmitter. However, t ...

Tips for receiving input on one HTML page and displaying it on the next page using Angular 2

Just started learning Angular 2 and trying to work with data binding. I'm having trouble printing the value from one page to another through components. The code below prints the value on the same page instead of passing it to another page. Can anyone ...

Asynchronous problem when using Firebase calls within an Angular ForEach loop

Here's the code snippet I'm working with: getTotalBookListCost(bookList:string[]):number { let cost=0; bookList.forEach(el=>{ this.store.doc("Books/"+el).get().subscribe(data=>{ let temp=<Book>data.da ...

How to connect multiple HTTP requests using observables in Angular 7

Is there a more efficient way to remove an alert only if it is not assigned to any user? Currently, I am checking my users list and filtering out the users who have this alert assigned using observables. But I wonder if there is a better approach to achi ...

Incorporating the Angular2-wizard module into your project

My Angular project is based on the Angular Quickstart seed. I have been developing my own components and now I am looking to integrate the Angular2-wizard npm package. To install the package, I used the following command: $ npm install angular2-wizard -- ...

Creating visual content using Webpack and Angular2

I have a small Angular2 app that utilizes Webpack for project building and scaffolding. One issue I've encountered is the inability to load images specified in TypeScript files during production. After running npm run build, I noticed that these imag ...

Combining functions does not result in a callable function, even when the parameters fulfill the constraints of each individual function

I have encountered an issue while trying to compile a Typescript snippet: function foo(v: string) { return 'foo'; } function bar(v: string | number) { return 'bar'; } const notCallable: typeof foo | typeof bar = function() {} as any; ...

What is the best way to duplicate an object in TypeScript?

After receiving the object credential.user, my goal is to create a modified clone of it and pass it to a function. const credential = await this.afAuth.auth.signInWithEmailAndPassword(email,password); var userInfo= await credential.user userInfo.displayN ...

Having trouble with NPM installation? It seems like you encountered an error message: "error:0906D06C:PEM routines:PEM_read

While trying to install the expo package from npm, I encountered an error message, error:0906D06C:PEM routines:PEM_read_bio:no start line, multiple times during the installation process when fetching .tar.gz files, ultimately resulting in installation fail ...

Modules are being installed to the application's root directory using NPM

I made a mistake and have tried everything to correct it, but no success. Every time I run 'npm install' on a new node project, it installs all dependencies in the root of the application instead of the expected /node_modules/ directory. For ex ...

Send the build number from Azure DevOps to the npm task

I need to update the version in package.json using an Azure DevOps pipeline. I attempted to do so with the following task, but encountered an error. - task: Npm@1 displayName: "npm version" command: "custom" workingDir: sr ...

Exploring TypeScript reflections within a specific namespace

(Building upon previous discussions about dynamically loading a TypeScript class) If I am currently within a namespace, is there a method to reference classes within the namespace in order to utilize 'reflection' to invoke their constructors? n ...

Having trouble installing gulp-uglify using npm

As a newcomer to nodejs and gulp, I am eager to enhance my development skills through these technologies. Everything was going smoothly during the installation of node js and gulp. I verified their versions in the command prompt using the following comman ...

Adding additional properties to JSX components during typescript compilationIs there a way to include additional properties

Currently, I am employing JSX syntax in my TypeScript node.js project (without relying on React). Within my tsconfig, I have specified my custom jsxFactory { "compilerOptions": { ... "jsxFactory": "myJSXFactory", ...

Is there a different way to retrieve the tag name of an element besides using

Currently, I am dealing with an outdated version (Chromium 25) of chromium. My goal is to utilize the tagName method in order to retrieve the name of the specific HTML tag being used. While I am aware that Element.tagName functions for versions 43 and ab ...

Encountering problems with npm while trying to install the upper-case module on a Linux Mint operating

npm encountered errors while trying to install upper-case modules of Node.js NPM on Linux Mint. The specific error messages received are as follows: npm WARN saveError ENOENT: no such file or directory, open '/home/spipl8/package.json' npm WARN ...

What is the method for throwing errors with NestJS guards without relying on an authentication module?

Are you looking to customize error responses in NestJS guards? import { CanActivate, Injectable, ExecutionContext, NotFoundException } from '@nestjs/common'; import { Observable } from 'rxjs'; import { InjectModel } from '@nestjs/m ...

Updating the mat-icon in a row when a button is clicked

I am currently working on implementing the mat-table in my project. I am facing an issue where I need to change the mat-icon of a button based on a click event, but I only want this change to apply to a single row in the table. At the moment, I am able to ...

What makes components declared with "customElements.define()" limited in their global usability?

I've been tackling a project in Svelte, but it involves some web components. The current hurdle I'm facing is with web components defined using the customElements.define() function in Typescript, as they are not accessible unless specifically im ...