NPM Package: Accessing resources from within the package

My Current Setup

I have recently developed and published an npm package in typescript. This package contains references to font files located within a folder named public/fonts internally.

Now, I am in the process of installing this package into an Angular application.

Upon installation, the file structure within the node_modules directory looks like this:

 node_modules/
   mypackage/
     dist/
       index.d.ts
       index.js
     public/
       fonts/
     LICENSE
     README.md
     package.json
     tsconfig.json

In the index.js file, I reference the font files as follows:

'Roboto': {
   normal: 'public/fonts/Roboto-Regular.ttf',
   bold: 'public/fonts/Roboto-Bold.ttf',
   italics: 'public/fonts/Roboto-Italic.ttf',
   bolditalics: 'public/fonts/Roboto-BoldItalic.ttf'
}

Initially, during the package development stage, there were no errors reported.

Current Issue

However, after successfully installing and importing the package into my application, I encountered the following error while using it:

Error: ENOENT: no such file or directory, open 'public/fonts/Roboto-Bold.ttf'

Observation

I noticed that if I place the public folder at the root level of the application utilizing the package, everything works as expected. It seems that the package is searching for public/fonts/ at the application's root level rather than within the package itself.

Do you have any suggestions on how to reference these files from within the package without encountering errors during development?

Additionally, I also encountered another error related to my package's tsconfig.json:

[ts] No inputs were found in ...../mypackage/tsconfig.json. Specified 'include' paths were [**/*] and 'exclude' paths were [./dist]
.

To resolve this issue, I had to add a blank .ts file at the root of the package. Is there a better alternative solution available?

Edit (including my package's tsconfig.json)

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "strict": true,
    "noImplicitAny": false,
    "lib": [
      "es2015"
    ]
  }
}

Answer №1

When creating your read me file, be sure to instruct users on how to add asset configurations to the angular json file as outlined in Angular CLI's stories asset configuration:

This extended configuration allows you to copy assets from outside your project. For example, you can copy assets from a node package:

"assets": [
  { 
    "glob": "**/*", 
    "input": "./node_modules/some-package/images", 
    "output": "/some-package/" 
  }
]

In your specific case, it would look like this:

"assets": [
  { 
    "glob": "**/*", 
    "input": "../node_modules/mypackage/public", 
    "output": "/public/" 
  }
]

The path to node_modules should be relative to your app root.

With NG6 onwards, you can utilize the ng add command from Angular CLI to install npm modules and update the angular json file accordingly. You will need to implement schematics logic for ng add in your module; examples can be found at: Angular material, primeng schematics, etc...

Answer №2

If you encounter the message '[ts] No inputs were found in ...../mypackage/tsconfig.json. Specified 'include' paths were [**/*] and 'exclude' paths were [./dist]', it's important to review your project file layout. I once faced this issue myself when I mistakenly placed the dist directory intended for outDir (as specified in my ts.config) within the src directory. After correcting the placement of the files in my project structure, the problem was successfully resolved.

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

Unable to access the correct item from local storage while a user is authenticated

I am facing an issue with retrieving the userid from local storage when a user is authenticated or logged in. The user id is not being fetched immediately upon logging in, and even when navigating from one page to another, it remains unavailable until I re ...

Issue detected in the console: Angular and Express code linked with status code 200 displaying an error

I am attempting to delete data along with an image connected to that data via an image Id using a get route (since the delete route didn't work out for me). The data is successfully being deleted, but I keep receiving a 200 OK response followed by an ...

Issue encountered while trying to iterate through an observable: Object does not have the capability to utilize the 'forEach' property or method

I am currently following the pattern outlined in the hero.service.ts file, which can be found at this link: https://angular.io/docs/ts/latest/guide/server-communication.html The Observable documentation I referenced is available here: When examining my c ...

Can you clarify the semver meaning of the >= operator and what is the highest version it permits?

It's commonly known that when using symbols like ^, it represents anything up to the next major version, and ~ means only patches. However, there seems to be a lack of clarity regarding what the maximum version is when utilizing >=. So, how should ...

At what point are routed components initialized?

Here is a route setup I am working with: path: ':id', component: ViewBookPageComponent }, After adding this route, an error keeps popping up: Error: Cannot read property 'id' of null I haven't included a null check in the compo ...

The function of type 'PromiseConstructor' is not executable. Should 'new' be added? React TypeScript

.then causing issues in TypeScript. interface Props { type: string; user: object; setUserAuth: Promise<any>; } const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { e.preventDefault(); if (type === "signup" ...

Issue with ScrollBar functionality in IE

Hello, I am experiencing an issue with the vertical scrollbar on the addUser Page. When I try to select multiple values in the dropdown menu, the page length increases but the scrollbar does not function correctly. This problem only seems to occur in Inter ...

Ways to invoke a component function from another component

Hello all, I am currently learning Angular and I have encountered an issue where I need to call a function from one component in another component. Most of the solutions I found online involve scenarios where the components are either child or sibling comp ...

"Using Angular and TypeScript to dynamically show or hide tabs based on the selected language on a website

When switching the language on the website, I want to display or hide a specific tab. If the language is set to German, then show the tab; if any other language is selected, hide it. Here's my code: ngOnInit(): void { this.translate.onLangChange.s ...

Jest may come across test suites, but it discreetly disregards the individual tests

Having encountered an issue with Jest testing in a Nuxt/Vue v2 project, I found that after making some changes, the tests were no longer running. The unit tests were either passing or failing initially, but suddenly stopped running altogether. ----------|- ...

Warning: The TypeScript version in use may not support all features. The current language level is set to XX in Visual Studio 2019

After installing VS 2019, I noticed that Microsoft.TypeScript.MSBuild 4.2.3 was added. On my Windows 10 OS, I also installed it using NPM in the following way: However, upon opening VS 2019, I encountered the warning below: TypeScript 3.4 feature Curre ...

Ensuring the precise alignment of a singular component in Angular 8 while keeping the styles of its child elements unchanged

Is there a way to centralize div elements within a component without impacting the styles of those specific divs? How can I achieve this? I'm currently working with Angular 8. Using the text-align method affects the alignment of the text inside the d ...

What are the reasons for deprecating bindToController in Typescript?

When I am creating an AngularJS directive using TypeScript, I typically use the bindToController property to bind parameters to the controller for easy access. export class MyDirective implements IDirective { controller = MyController; controllerA ...

Angular BehaviorSubject is not refreshing quickly enough

After following a tutorial on creating full Angular + JWT Authentication, I encountered some issues when testing the project. In order to notify the AuthGuard that I am connected and can proceed to the next page upon logging in, I needed to send the API re ...

How is the filename for the generated javascript output determined by the Scala.js sbt plugin?

Within this particular project: https://github.com/tribbloid/scalajs-cli-demo I have established a scalajs project by utilizing a combination of npm and sbt. Within the npm package file, I specify sbt as a prepublish script: "scripts": { "prepubli ...

Managing a single Angular project with multiple apps that share common features: A guide

Imagine having multiple Angular 'apps' with similar features and functions, where one product needs to be customized for different clients with slightly varying needs. The challenge lies in developing within a single code base. Using feature mod ...

Port 4200 is currently being utilized. Please utilize the '--port' option to designate an alternative port

Hi there, I am currently running an Angular 2 app with Express as the backend and encountering the following issue: [3] Port 4200 is already in use. Use '--port' to specify a different port. [3] [nodemon] app crashed - waiting for file changes b ...

Alias route for `src` in Ionic 3

I have set up a custom webpack configuration for Ionic 3 in order to use src as a path alias (meaning I can import from src/module/file): resolve: { alias: { 'src': path.resolve('./src') } } However, after updating to Ionic ap ...

Utilize the <wbr> tag within FormattedMessage and assign it as a value while coding with TypeScript

Trying out the optional word break tag <wbr> in a message within <FormattedMessage id="some:message" />. Context Some words or texts are too lengthy for certain parent elements on smaller mobile screens, and we have a column layout t ...

Storing Global Types in Angular: A Better Approach

Imagine I possess certain universally applicable enums, interfaces, or extensive classes. For example: export interface LogMessage { code: number; message: string; } Where would be the optimal location to store them? What is considered the best pr ...