nx: Collection "@nrwl/workspace" cannot be resolved.

Prerequisites

Expected Behavior

Have the following command working:

npx create-nx-workspace workspace --cli=angular --npm-scope=scope --preset=empty --skipGit=true --style=scss

Current Behavior / Failure Information (for bugs)

An unhandled exception occurred: Collection "@nrwl/workspace" cannot be resolved.

Steps to Reproduce

~/workspaces $ mkdir tmp
~/workspaces $ cd tmp/
~/workspaces/tmp $ npx create-nx-workspace workspace --cli=angular --npm-scope=scope --preset=empty --skipGit=true --style=scss

Context

~/workspaces/tmp $ yarn --version
1.22.4
~/workspaces/tmp $ node --version
v14.1.0
~/workspaces/tmp $ npm --version
6.14.4
~/workspaces/tmp $ uname -a
Linux nx--tools--dev--nx--latest-gkr8k 4.19.107 #1 SMP Thu Mar 26 11:33:10 PDT 2020 x86_64 Linux
~/workspaces/tmp $ cat /proc/version
Linux version 4.19.107 (jenkins@jenkins) (gcc version 7.4.0 (Buildroot 2019.02.10)) #1 SMP Thu Mar 26 11:33:10 PDT 2020
~/workspaces/tmp $ lsb_release -a
sh: lsb_release: not found
~/workspaces/tmp $ cat /etc/*release
3.11.6
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.11.6
PRETTY_NAME="Alpine Linux v3.11"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"

Failure Logs

npx: installed 198 in 34.148s
Creating a sandbox with Nx...
warning @angular/cli > universal-analytics > request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
warning " > @nrwl/workspace@9.2.3" has incorrect peer dependency "prettier@^1.19.1".
new workspace "--npm-scope=scope" "--skipGit=true" --preset="empty" --interactive=false --collection=@nrwl/workspace
An unhandled exception occurred: Collection "@nrwl/workspace" cannot be resolved.
See "/tmp/ng-ahJOFF/angular-errors.log" for further details.
(node:847) UnhandledPromiseRejectionWarning: Error: Command failed: "/tmp/tmp-84746viJAJvJms6/node_modules/.bin/ng" new workspace "--npm-scope=scope" "--skipGit=true" --preset="empty" --interactive=false --collection=@nrwl/workspace
    at checkExecSyncError (child_process.js:611:11)
    at Object.execSync (child_process.js:647:15)
    at createApp (/home/node/.npm/_npx/847/lib/node_modules/create-nx-workspace/bin/create-nx-workspace.js:343:21)
    at /home/node/.npm/_npx/847/lib/node_modules/create-nx-workspace/bin/create-nx-workspace.js:76:21
(Use `node --trace-warnings ...` to show where the warning was created)
(node:847) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:847) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Other

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 13
  • Comments: 43 (6 by maintainers)

Most upvoted comments

I have run into this error while trying to follow the Nx migration guide to bring a react app into Nx: https://nx.dev/react/migration/overview

I was able to run npx create-nx-workspace@latest and create an empty workspace. The next suggested command fails nx generate @nrwl/react:application my-application with “Collection “@nrwl/react” cannot be resolved.”

Running npm install @nrwl/react seems to have resolved the problem. Perhaps consider adding this to the tutorial if nx generate @nrwl/react:application is unable to install its own dependencies.

same error for me when using nx g @nrwl/angular:app (Collection “@nrwl/angular” cannot be resolved) any tips?

Did you install the package ‘@nrwl/angular’?

Thanks a lot. it worked. i wonder why there is no mentions on this for empty workspaces.

same error for me when using nx g @nrwl/angular:app (Collection “@nrwl/angular” cannot be resolved) any tips?

Did you install the package ‘@nrwl/angular’?

Omg (!). I just noted that nx@8 said something different than nx@9

I think the problem was because I asked for an empty workspace and it comes without @nrwl/web - ok… After npm i @nrwl/web I could nx generate @nrwl/web:app app1

In that case - I’d suggest making the error-message clearer, and add a call-to-action to npm i the missing package. If you want to be ninjas - then notify the user that the package is missing and ask Y/N if she would like to install it and continue.

The current message Collection "@nrwl/web" cannot be resolved from nx@9 sent me to the wrong places - as an nx beginner I don’t know what it means by Collection. I also could not find an arch overview of that thing or a glossary of concepts I can use to try to make sense of all that new things.

I even tried install VSCode and use the VSCode plugin - but it got me the exact same, wrapped with the usual IDE golden-cage experience.

However - nx@8 said Could not find module "@nrwl/web" from "/eval-nx". which was barely close enough to the familiar Could not find module "@nrwl/web".

Should we close the issue?

I tried replicating in a docker and was unsuccessful. Can you post your Dockerfile please?

This is mine:

Dockerfile:

FROM node:alpine

RUN npx create-nx-workspace@latest my-ws --preset=empty --cli=nx

Logs:

❯ sudo docker build ./ --no-cache                                                                                                                                                          ─╯
Sending build context to Docker daemon  127.9MB
Step 1/2 : FROM node:alpine
 ---> 87c43f8d8077
Step 2/2 : RUN npx create-nx-workspace@latest my-ws --preset=empty --cli=nx
 ---> Running in 0850e438d77d
npx: installed 198 in 11.393s
Creating a sandbox with Nx...
new my-ws --preset="empty" --interactive=false --collection=@nrwl/workspace
- Installing packages...
✔ Packages installed successfully.
/bin/sh: git: not found
CREATE my-ws/nx.json (460 bytes)
CREATE my-ws/tsconfig.json (509 bytes)
CREATE my-ws/README.md (2532 bytes)
CREATE my-ws/.editorconfig (245 bytes)
CREATE my-ws/.gitignore (503 bytes)
CREATE my-ws/.prettierignore (74 bytes)
CREATE my-ws/.prettierrc (26 bytes)
CREATE my-ws/workspace.json (1130 bytes)
CREATE my-ws/package.json (1097 bytes)
CREATE my-ws/apps/.gitkeep (1 bytes)
CREATE my-ws/libs/.gitkeep (0 bytes)
CREATE my-ws/tools/tsconfig.tools.json (218 bytes)
CREATE my-ws/tools/schematics/.gitkeep (0 bytes)
CREATE my-ws/.vscode/extensions.json (109 bytes)

———————————————————————————————————————————————


>  NX   NOTE  Nx CLI is not installed globally.

  This means that you might have to use "yarn nx" or "npm nx" to execute commands in the workspace.
  Run "yarn global add @nrwl/cli" or "npm install -g @nrwl/cli" to be able to execute command directly.

Removing intermediate container 0850e438d77d
 ---> cadb1bef78d3
Successfully built cadb1bef78d3

@FrozenPandaz @levani132 - I can confirm that my output is identical as well

  1. npx create-nx-workspace workspace
  2. choose empty workspace
  3. choose nx cli
 ✘  ~/dev/node  npx create-nx-workspace workspace
npx: installed 198 in 13.673s
? What to create in the new workspace empty             [an empty workspace]
? CLI to power the Nx workspace       Nx           [Extensible CLI for JavaScript and TypeScript applications]
Creating a sandbox with Nx...
[---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------] 0/364
new workspace --preset="empty" --interactive=false --collection=@nrwl/workspace
Collection "@nrwl/workspace" cannot be resolved.
(node:25837) UnhandledPromiseRejectionWarning: Error: Command failed: "/var/folders/g0/c7qwk52n1k5b20vy4sy9gdqm0000gn/T/tmp-25837YINyxEjsQZxz/node_modules/.bin/tao" new workspace --preset="empty" --interactive=false --collection=@nrwl/workspace
    at checkExecSyncError (child_process.js:621:11)
    at Object.execSync (child_process.js:658:15)
    at createApp (/Users/foo/.npm/_npx/25837/lib/node_modules/create-nx-workspace/bin/create-nx-workspace.js:343:21)
    at /Users/foo/.npm/_npx/25837/lib/node_modules/create-nx-workspace/bin/create-nx-workspace.js:76:21
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
(node:25837) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:25837) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

cat /var/folders/g0/c7qwk52n1k5b20vy4sy9gdqm0000gn/T/tmp-25837YINyxEjsQZxz/package.json

{"dependencies":{"@nrwl/workspace":"9.3.0","@nrwl/tao":"9.3.0","typescript":"~3.8.3","prettier":"2.0.4"},"license":"MIT"}%

Thank you for sharing your workaround @gund. On my side, I don’t want to have the ng CLI to be installed globally, same for installing @nrwl/workspace globally, I would like to stay on locally installed package to prevent permissions issues (and other architecture issues that will come with it).

One of the main issues with enhancing this specific error message, is that this is not code that’s specific to Nx, it comes from the schematics library that is used under the hood.

There might be a way to wrap it with a try/catch, but even then it’s not clear which plugin they want to install (could be @nrwl/angular, @nrwl/nest, @nrwl/node, @nrwl/react,@nrwl/web etc).

What might be helpful though is printing a banner after installing an empty workspace.

P.S: - I got here through #2987, which was closed as a duplicate of this one

Thank you all for your answers, time and help.

So, the hell. I investigated in order to set up a small project outside of mine to reproduce the issue but I didn’t reach to.

Finally, I can tell that the only thing that differs between one of my closest “working/not working” cases is the way the container is managed. In my final setup, the image is built by Kaniko and run inside a K8S cluster where the Nx workspace is created in a mounted volume, provisioned by NFS. To investigate further I now need to integrate this docker orchestration mechanism in the minimal example I began and I will not have time, neither need to: as a workaround I now create the workspace inside a not mounted volume and then copy it in the volume (like this: it works… 🤯).

My project was working since a while and as the unexpected error seems to come from Nx, I posted here. It seems that it is more complex than it seemed to so please forgive me for the lost of time induced on your side… You one more time proved that the Nx team/community is awesome ❤️ .

@hadrien-toma I’m happy to take a look if you’re able to isolate it in Docker 😃

I’m on the same versions of npm and node as @collegeworking and don’t run into the issue.

node: 14.2.0 npm: 16.14.4

When somebody runs into this issue can you print the contents of the package.json in the tmp directory that is printed in this log?

(node:847) UnhandledPromiseRejectionWarning: Error: Command failed: "/tmp/tmp-84746viJAJvJms6/node_modules/.bin/ng" new

/tmp/tmp-84746viJAJvJms6/package.json

{
  "dependencies": {
    "@nrwl/workspace": "9.3.0",
    "@angular/cli": "~9.1.0",
    "typescript": "~3.8.3",
    "prettier": "2.0.4"
  },
  "license": "MIT"
}

This is how tmp package.json looks like when I run into this issue.

Seems to also be the case here https://github.com/nrwl/nx/issues/2987

While NX cli is still broken I was able to create new NX workspace with Angular CLI directly:

ng new selectable --collection=@nrwl/workspace

NOTE: I installed package @nrwl/workspace via NPM before executing the command above.