nx: "ng build" for library fails with "does not support the 'build' target"

Expected Behavior

  • ng build <library-name> should succeed.
  • build artifacts should be stored in the dist/ directory as described in the README

Current Behavior

ng build <library-name> fails with: Project '<library-name>' does not support the 'build' target.

Failure Information (for bugs)

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. install @angular/cli: npm i -g @angular/cli
  2. create nx workspace: npx create-nx-workspace myws - accept all the defaults by pressing enter, select “Empty workspace” when asked.
  3. cd myws
  4. create an angular project in workspace: ng g app myapp - accept all the defaults by pressing enter.
  5. create a library in workspace: ng g lib mylib - accept all the defaults by pressing enter.
  6. try to build library as is mentioned in generated README.md: ng build mylib

Context

Please provide any relevant information about your setup:

  • version of Nx used: “@nrwl/builders”: “7.8.0”
  • version of Angular CLI used: 7.3.8
  • angular.json configuration: default generated by npx
  • version of Angular DevKit used: “@angular-devkit/build-angular”: “~0.13.0”
  • 3rd-party libraries and their versions: none
  • and most importantly - a use-case that fails: see steps to reproduce

A minimal reproduce scenario using allows us to quickly confirm a bug (or point out coding problem) as well as confirm that we are fixing the right problem.

Failure Logs

Project ‘mylib’ does not support the ‘build’ target. Error: Project ‘mylib’ does not support the ‘build’ target. at BuildCommand.initialize (/home/heapifyman/Downloads/myws/node_modules/@angular/cli/models/architect-command.js:53:19) at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:52:5) at Function.Module.runMain (internal/modules/cjs/loader.js:871:11) at internal/main/run_main_module.js:21:11

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 23 (8 by maintainers)

Commits related to this issue

Most upvoted comments

I have this same error when i create a library. In my case is just the case sensitive library name under angular.json! My lib has a hiffen like my-Lib. Inside package json, this name is changed to myLib. I see this message is shown in any case that the project/lib is not found!! I tried with mylib, myerror and acquire the same error.

Hoppe, I helped!

You can create a buildable library if you use the --publishable flag. The bug here, is that the README.md has incorrect information though. Thanks for reporting!

Landed here as I used :

nx g @nrwl/workspace:lib lib1

When I should have used

nx g @nrwl/node:library  lib1 --publishable --importPath @project/lib1

Just in case any one comes across this issue… I’m working in an nx workspace and trying to run ng build on a lib that wasn’t set as “publishable” from the start… I added a package.json and ng-package.json to the root of the lib and tried to run ng build and got the error above.

Error in my case was due to the fact that in angular.json for the “projects” entry for the lib that I was trying to build it didn’t have a “build” section under the “architect” section of the lib. (I’m assuming that --publishable adds this section for you…)

After adding the “build” section with “builder”: “@angular-devkit/build-ng-packagr:build” and an “options” section that pointed “tsConfig” to the path of tsconfig.lib.json of the lib and “project” to the path of ng-package.json of the lib. It worked.

Hope that helps someone that gets the same issue.

please verify the name of your library, maybe when you crated it, you name it libraryName, try ng build libraryName

You cannot automatically transform a library into publishable right now, but you can add the needed build target with the needed ng-package.json file. We could add a schematic for that. If someone wants to implement it, we can help!

As far I as can see, we no longer tell you use “ng build” for non-publishable libraries. So I’m going to close this issue. If the behavior provided by the latest version of nx is confusing, please reopen the issue and we will see how we can address it.

I took a look at this and it appears this was an issue in @nrwl/schematics@7.8.0. The README in question though wasn’t coming from Nx, rather it was being generated by the Angular CLI here.

Not sure exactly where, but this seems to have been fixed in the latest Nx, with one caveat. If you want a publishable library, you need to specify the --publishable flag when generating the library with ng g @nrwl/angular:library mylib --publishable. This will allow you to build the lib for distribution.

@circy asked if you can set the --publishable flag later, and from what I’ve gathered you can make a lib publishable after the fact, but you have to add the ng-package.json to the lib’s directory yourself, then make sure ng-packagr package is installed in the workspace’s dev dependencies.