I have created & published an npm package(in typescript) which internally refers to few font files residing in a public/fonts
folder within it.
I'm then installing this package in an Angular application.
The file structure within node_modules
after the package is installed is as follows:
node_modules/
mypackage/
dist/
index.d.ts
index.js
public/
fonts/
LICENSE
README.md
package.json
tsconfig.json
I am referring to the font file from index.js
as:
'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'
}
The above didn't throw an error while developing the package.
But now after I installed, imported and while using the package it throws the following error:
Error: ENOENT: no such file or directory, open 'public/fonts/Roboto-Bold.ttf'
If I place the public folder at the root of the application which is using the package, it works as expected. Which means the package is looking for public/fonts/
at the root level of the application rather than the one within the package.
Any idea how to refer to those files within the package also making sure it doesn't throw an error while developing the package.
Additionally I also get the below error at my package's tsconfig.json
:
[ts] No inputs were found in ...../mypackage/tsconfig.json. Specified 'include' paths were [**/*] and 'exclude' paths were [./dist]
.
For the above issue, adding a blank .ts file at the root of the package fixes it but is there any alternative ?
Edit (adding my package's tsconfig.json
)
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true,
"noImplicitAny": false,
"lib": [
"es2015"
]
}
}
"dependencies" : Packages required by your application in production. "devDependencies" : Packages that are only needed for local development and testing.
Your package. json holds important information about the project. It contains human-readable metadata about the project (like the project name and description) as well as functional metadata like the package version number and a list of dependencies required by the application.
By default, npm install will install all modules listed as dependencies in package.json .
The dependencies in your project's package. json allow the project to install the versions of the modules it depends on. By running an install command inside a project, you can install all of the dependencies listed in the project's package.
At your read me file you can guide users to add assets configuration to angular json file as explained at Angular CLI stories asset configuration:
You can use this extended configuration to copy assets from outside your project. For instance, you can copy assets from a node package:
"assets": [
{
"glob": "**/*",
"input": "./node_modules/some-package/images",
"output": "/some-package/"
}
]
so, in your case it should be:
"assets": [
{
"glob": "**/*",
"input": "../node_modules/mypackage/public",
"output": "/public/"
}
]
path to node_modules
should be relative to your app root.
Starting from NG6, you can use ng add command from Angular CLI which will install your npm module and make needed update at angular json file. You will need to implement schematics logic for ng add
in your module, you can find examples like: Angular material, primeng schematics, etc...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With