Create a custom npm package using TypeScript code

Currently, I am working on a npm module that utilizes Angular2 and TypeScript 2.

I am encountering difficulties in figuring out how to properly generate this module.

Uncertain about the necessary steps to take, I created a public repository on github.

Could someone advise me on what needs to be done to generate the npm module successfully?

Answer №1

Your tsconfig.json has a small error where you set "declarations": true instead of "declaration": true. Additionally, the exclude field should be outside of the compilerOptions block. You are also referencing types from a non-existent file called index.d.ts. Helpful links for configuring TypeScript: Compiler Options, tsconfig.json Handbook.

To generate the module, follow these steps:

  • Write code for all utilities to be exposed in your module, entities that should be 'importable'.

  • Create a file in src (assuming src is your rootDir) named index.tsx or another choice. Export the entities to expose in this file.

  • In package.json, add

    "main": "<outDir>/index.js"
    and
    "typings": <outDir>/index.d.ts
    . Replace <outDir> with your output directory from tsconfig. The main field specifies where importing apps should reference. For example, if an app executes
    import {abc} from 'swagger-client'
    ,

    main indicates to look for abc in the "<outDir>/index.js" file.
    The typings field in TypeScript tells where to find types corresponding to abc.
    Note the extensions mentioned in main and typings fields; they are not ts files. It's recommended not to publish .ts files to npm but to github, and publish generated .js, .d.ts files to npm. This practice ensures usability in js and ts projects.

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

What is causing the subscriber to receive the error message "Cannot access 'variable' before initialization" while referencing itself?

Within my Angular 8 project, I have a component that is dependent on a service for data loading. It waits for a notification from the service signaling that it has finished loading in the following manner: constructor(public contentService: ContractServic ...

tips for effectively mocking useUser, a custom TypeScript hook

Imagine we have a unique custom hook called useUser const useUser = (): IUser => useContext(MiniAppContext).user; This hook is then utilized in another component import { useUser } from '@ABC' const { monitoring } = useUser(); // note that & ...

Manipulate and send back information from Observable in Angular

Here is an example of a scenario where I have created a service to retrieve a list of properties. Within this service, I am utilizing the map function to manipulate the response and then returning the final array as an Observable. My question is: How can ...

In Angular Google Maps, here's a step-by-step guide on how to zoom in

I am currently utilizing agm/core to showcase the coordinates of a map. Here is the code snippet I am using: <agm-map [latitude]="10.3207886" [longitude]="123.90250049999997"> <agm-marker [latitude]="10.3207886 [longitude]="123.90250049999997 ...

Displaying a div component in React and Typescript upon clicking an element

I've been working on a to-do list project using React and TypeScript. In order to display my completed tasks, I have added a "done" button to the DOM that triggers a function when clicked. Initially, I attempted to use a useState hook in the function ...

Conceal or remove disabled years in Angular Material datepicker

I previously disabled years prior to 2018, but now I would like to hide or delete them. The year selection range currently starts from 1998, but it should begin at 2018 instead. Is there a way to display only 3-4 years instead of the current 24-year rang ...

Is it possible to utilize an XML format for translation files instead of JSON in React Native?

I'm in the process of creating a react native application using the react i18next library. For translations, I've utilized XML format in android for native development. In react native, is it possible to use XML format for translation files inste ...

Unable to download react native

Can anyone help me with this issue? Whenever I try to install something in React Native, Visual Studio Code displays the following error message. Is this a serious problem? pm ERR! code E404 npm ERR! 404 Not Found - GET https://registry.npmjs.org/@types% ...

Uncovering the value of a Nested JSON object in a JSON file with Typescript

As a beginner in Typescript, I am currently working on parsing a nested JSON file containing segments and products. My goal is to extract the value of the product and display it on the console. Here is the structure of the JSON File : { "int" ...

Filter error - Unable to retrieve property 'toLowerCase' from null value

When filtering the input against the cached query result, I convert both the user input value and database values to lowercase for comparison. result = this.cachedResults.filter(f => f.prj.toLowerCase().indexOf((this.sV).toLowerCase()) !== -1); This ...

What is the correct method for importing React in TypeScript (.tsx) to ensure optimal integration?

Within our TSX/React web application, we employ two distinct import styles for the react module: import * as React from "react"; as well as import React from "react" As far as I know, both methods function without any noticeable differences. Is there a ...

When using RXJS, the method BehaviorSubject.next() does not automatically notify subscribers

In my project, I have a service set up like this: @Injectable({ providedIn: 'root' }) export class MyService { private mySubject = new BehaviorSubject({}); public currentData = this.mySubject.asObservable(); updateData(data: any) { ...

Tips for executing several JavaScript files using the Node.js command line?

I am looking to launch 4 scripts using Node.js. myapp -script1.js -script2.js -script3.js -app.js -package.json .... .... I attempted to run them with the following commands: node script1.js && node script2.js && node scrip ...

Having difficulty accessing an element within ng-template during the unit test writing process with Jasmine

I am encountering an issue when trying to access a button inside an ng-container in my testing environment. Despite manually setting the value of the ngIf condition to true, the elements inside are not being rendered. Here is what I have attempted so far: ...

An issue occurred: the channel has been terminated within the ChildProcess.target.send function at internal/child_process.js, line 562, character

My Angular 5 application is giving me an error message that says "ERROR in Error: channel closed". This occurs after running the application. The issue seems to persist even though it works fine for a few minutes after executing ng serve. Has anyone else e ...

The Go programming language's counterpart to the npm command "npm install -g

I am looking to install a compiled Golang program so that I can run it with a bash command from any location on my computer. Is there an equivalent of the npm global installation for Golang? For example, in nodejs, we use "npm install -g express" to global ...

Unable to deploy/remove npm package with version 0.0.1

I am encountering issues with the version 0.0.1 in my package, preventing me from installing it... $ npm install nodejs npm http GET https://registry.npmjs.org/nodejs npm http 304 https://registry.npmjs.org/nodejs npm WARN engine <a href="/cdn-cgi/l/em ...

Building TypeScript Model Classes

Greetings! As a newcomer to TypeScript with a background in both C# and JavaScript, I am on a quest to create class models resembling those found in C#. Here's my attempt so far: export class DonutChartModel { dimension: number; innerRadius: ...

Github Actions and Yarn encountered an issue with the resolution of json5@npm:^2.2.2 resulting in npm version 1.0.2 being used instead

While everything runs smoothly locally, encountering failures with yarn install on GitHub Actions has been quite puzzling for me. I've rigorously ensured that both Node and Yarn versions are aligned with my project requirements. - name: Checko ...