0

So my structure is the angular project is like this project

  • project/my-lib
  • src/app
  • package.json

I have created lib called my-lib it is used in app so i have it as dependency in package.json file

"dependencies": {
"my-lib": "file:dist/my-lib",
}

When i run npm install in the root of the project i get

"npm ERR! Could not install from "..\dist\my-lib" as it does not contain a package.json file.

Here is example project on git https://github.com/IvanGavlik/AngularTest. You will get same exception when you run npm install

Also when i try to build lib

ng build my-lib

I get

An unhandled exception occurred: Cannot find module '@angular-devkit/build-ng-packagr/package.json'

Note I am using angular 9 and i do not want to publish my libaray

  • Does your lib have package.json? – Bojan Kogoj Dec 02 '20 at 09:25
  • yes. It is like this. { "name": "my-lib", "version": "0.0.1", "peerDependencies": { "@angular/common": "^9.0.7", "@angular/core": "^9.0.7", "tslib": "^1.10.0" } } and has a lot of peerDependencies – Ivan Gavlik Dec 02 '20 at 09:29
  • You probably don't point to library correctly, but from structure you wrote it's hard to find what you did wrong. Did you build your library first? If you want monorepo maybe take a look at https://nx.dev/angular – Bojan Kogoj Dec 02 '20 at 10:04
  • Here is example project on git https://github.com/IvanGavlik/AngularTest. We I try to build it I get: Cannot find module '@angular-devkit/build-ng-packagr/package.json' – Ivan Gavlik Dec 02 '20 at 10:05
  • Hi, It looks like you want to do something very similar to this question. Check out the accepted answer which details steps on using local packages in this way. https://stackoverflow.com/questions/55560791/build-and-use-npm-package-locally/55560928#55560928 – James Dec 02 '20 at 12:40
  • ¸problem whit this solution (https://stackoverflow.com/questions/55560791/build-and-use-npm-package-locally/55560928#55560928) is when i first time run a build of lib i need install dependencies first, if lib have a lot of dependencies it will take a lot of time – Ivan Gavlik Dec 03 '20 at 01:52

3 Answers3

0

Î found to solution. In package.json change the path to the lib dependency form

 "my-lib": "file:dist/my-lib",

to

 "my-lib": "file:projects/my-lib",
0

You already have paths configuration in the tsconfig.json :

"paths": {
      "my-lib": [
        "dist/my-lib/my-lib",
        "dist/my-lib"
      ]
    }

You should be able to import from my-lib once the lib built :

import { MyLibService } from 'my-lib';

As you are not planning to publish the library, you can skip the build step and change the tsconfig.json path to point to source :

"paths": {
      "my-lib": [
        "projects/my-lib/src"
      ]
    }
ibenjelloun
  • 7,425
  • 2
  • 29
  • 53
  • What shold i put in the package.json file in dependencies "my-lib": "file:projects/my-lib", – Ivan Gavlik Dec 02 '20 at 10:35
  • or the version number "my-lib": "0.0.1" – Ivan Gavlik Dec 02 '20 at 10:46
  • `package.json` is only for external dependencies to download with npm, if you have a local library you don't have to add it to `package.json`. – ibenjelloun Dec 02 '20 at 13:35
  • what about build, is build of app include build of my-lib ? – Ivan Gavlik Dec 02 '20 at 14:25
  • Yes, like if it was part of the application sources. Also while developping, code changes in library will trigger browser refresh, which is very handy while coding. – ibenjelloun Dec 02 '20 at 14:31
  • ok, but imports can become big mess, I will need to write something like this import { MyLibService } from 'projects/my-lib/src/lib/my-lib.service'; i would like them to be import { MyLibService } from '/my-lib/MyLibService; You can see example here https://github.com/IvanGavlik/AngularTest. – Ivan Gavlik Dec 03 '20 at 01:47
  • You just need to export what you what to be available from out side the library in the `public-api.ts` https://github.com/IvanGavlik/AngularTest/blob/main/projects/my-lib/src/public-api.ts then import `import { MyLibService } from 'my-lib'` where `my-lib` is the key of the path defined in the `tsconfig.json` file. – ibenjelloun Dec 03 '20 at 07:45
0

Make sure @angular-devkit/build-ng-packagr on your node_modules, or run below steps.

  1. Remove this line
"dependencies": {
    "my-lib": "file:dist/my-lib", // <-
}
  1. run
npm i
ng build my-lib
npm i ./dist/my-lib
  • problem whit this is when i first time run a build of lib i need install dependencies first, if lib have a lot of dependencies it will take a lot of time – Ivan Gavlik Dec 03 '20 at 01:53