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

Created on 9 Apr 2019  路  20Comments  路  Source: nrwl/nx

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

bug

Most helpful comment

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!

All 20 comments

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!

I am having the same issue. However, I am not using Nx to generate files, just the Angular CLI:

Versions:
Angular CLI: 7.2.4
Node: 10.14.2
OS: darwin x64
Angular: 7.2.11

This is the Error:
Project 'hdms-common' does not support the 'build' target.
Error: Project 'hdms-common' does not support the 'build' target.
at BuildCommand.initialize (/Users/fernandasilva/Documents/Hilti/holng/dev/layout/hdms/hdms-core/node_modules/@angular/cli/models/architect-command.js:53:19)

@heapifyman it would be awesome could you submit a PR fixing the README.md?

@vsavkin I would but there are two problems:

  1. I cannot seem to find where the README.md in a generated lib is coming from or where it is generated
  2. Would changing README.md in the generated lib actually help? Because --publishable should have been used already in the ng g lib command to generate the lib. The other READMEs do not mention anything about ng g lib as far as I can see. Maybe the tutorial page "07-share-code.md" could be updated to include a section about creating a publishable lib?

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

I'm really sorry, but I'm a bit confused about the issue. I cannot find the readme that tells you to build your lib. Is the problem that some docs told you to run ng build lib or that the docs didn't specify what can be built and what cannot?

If you follow the steps to reproduce you will find a file myws/libs/mylib/README.md after step 5.

That README.md says in line 13:

Run `ng build mylib` to build the project. The build artifacts will be stored in the `dist/` directory.

library should consists of two words. e.g mylib-lib

Why? The nx docs do not mention that. See:

  • https://nx.dev/tutorial/07-share-code
    > Run the following generator to create a library: ng g lib data
  • https://nx.dev/tutorial/08-create-libs
    > Run ng g lib ui

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!

I'm hitting this error with two apps and no additional libraries. I created a mono repo using the following command:
create-nx-workspace nx-ws --npm-scope=nxws

Which style sheet format? scss
Create what? workspace with single Angular app

It generated the nx-ws workspace with an nx-ws application. I "cd nx-ws", and "ng build" with no errors. Then "ng serve -o" opens the app in Chrome as expected (Welcome to nx-ws!).

I killed the serve and closed the browser, then I created a second application using the following command:
ng g application myapp --prefix=myapp --style=scss --routing --skipGit=true

Which directory? myapps
Which test runner? jest
Which e2e test runner? cypress
Which tag? myapp

It generated myapp in nx-ws/apps/myapps as expected. I then tried "ng build myapp" and I got the error:

Project 'myapp' does not support the 'build' target.
Error: Project 'myapp' does not support the 'build' target.
at BuildCommand.initialize (C:\Angular\NxMonoTest\nx-ws\node_modules\@angular\cli\models\architect-command.js:53:19)
at
at process._tickCallback (internal/process/next_tick.js:189:7)
at Function.Module.runMain (module.js:696:11)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3

I'm somewhat new to Nx, but I don't think I'm doing anything improper here. I'm using Angular CLI 7.3.1, Node 8.15.0, and recently installed Nx schematics.

Update:
Sorry, I was incorrect. The error I got was legitimate. The first time I looked at angular.json I failed to notice the folder name prefixed to my application name. The "ng build myapp" failed because there was no myapp project, but "ng build myapps-myapp" succeeded (changing the project name to myapp in angular,json worked as well). I left my post in case any other Nx newbs run across this.

can I still set the "--publishable" flag later?

I get the same error, but mine occurs because I am trying to build a scoped library with the prefix @my-org my-lib. I would think that running an npm init --scope=@my-org would configure this to be usable for building scoped libraries.

Try execute ng build LIB_NAME in the workspace directory, not in the projects directory.

I took a look at this and it appears this was an issue in @nrwl/[email protected]. 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.

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.

Hello
What is a usecase for a lib that we cannot build (non-publishable libraries)?

Hey @khalilou88 馃憢.

Sure, in a monorepo scenario, creating a "publishable" library is more the exception than the standard approach. Libaries within a monorepo (such as in Nx) are meant to be directly imported by the apps that live in that same repository as well. This lowers the friction of having to build & publish them, version them etc.

Using a publishable library really makes sense when you want to not only share it with apps within your monorepo, but maybe you want to make the library also available to others outside the monorepo.

You can check out our free video course on Youtube if you'd like, specifically this video which shows how to create a library and consume it from within the app

Thanks @juristr a lot for your explanation. I thought that to test a lib you need to build it first (my mistake).

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kmkatsma picture kmkatsma  路  3Comments

Svancara picture Svancara  路  3Comments

jon301 picture jon301  路  3Comments

zpydee picture zpydee  路  3Comments

MichaelWarneke picture MichaelWarneke  路  3Comments