nx: File is not under 'rootDir' 'packages'. 'rootDir' is expected to contain all source files in npm preset

Current Behavior

Got an error when I run yarn nx run foo:build

Compiling TypeScript files for project "foo"...
packages/core/src/index.ts:1:15 - error TS6059: File '/Users/wizardnet972/repo/nx-npm-preset/packages/core/src/lib/core.ts' is not under 'rootDir' 'packages/foo'. 'rootDir' is expected to contain all source files.

1 export * from './lib/core';
                ~~~~~~~~~~~~
packages/foo/src/lib/foo.ts:1:22 - error TS6059: File '/Users/wizardnet972/repo/nx-npm-preset/packages/core/src/index.ts' is not under 'rootDir' 'packages/foo'. 'rootDir' is expected to contain all source files.

1 import { core } from '@nx-npm-preset/core';
                       ~~~~~~~~~~~~~~~~~~~~~

Expected Behavior

nx should build foo and core

Steps to Reproduce

Clone: git clone https://github.com/wizardnet972/nx-npm-preset.git Run:

yarn install
yarn nx run foo:build

See the error in Current Behavior

– OR – Run those commands:

npx create-nx-workspace nx-npm-preset
✔ What to create in the new workspace · npm
✔ Use Nx Cloud? (It's free and doesn't require registration.) · No

yarn add @nrwl/js @nrwl/node -W

yarn nx g @nrwl/js:lib --name core --buildable
yarn nx g @nrwl/js:lib --name foo --buildable

Replace the content of /packages/foo/src/lib/foo.ts with: (https://github.com/wizardnet972/nx-npm-preset/blob/main/packages/foo/src/lib/foo.ts)

import { core } from '@nx-npm-preset/core';

export function foo(): string {
  core();
  return 'foo';
}

Run:

yarn nx run foo:build

See the error in Current Behavior

Failure Logs

See the error in Current Behavior

Environment


 >  NX   Report complete - copy this into the issue template

   Node : 16.15.1
   OS   : darwin arm64
   yarn : 1.22.19

   nx : 14.3.5
   @nrwl/angular : Not Found
   @nrwl/cypress : Not Found
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.3.5
   @nrwl/eslint-plugin-nx : 14.3.5
   @nrwl/express : Not Found
   @nrwl/jest : 14.3.5
   @nrwl/js : 14.3.5
   @nrwl/linter : 14.3.5
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : 14.3.5
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : Not Found
   @nrwl/web : Not Found
   @nrwl/workspace : 14.3.5
   typescript : 4.7.3
   ---------------------------------------
   Community plugins:

✨  Done in 0.83s.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 44
  • Comments: 30 (7 by maintainers)

Most upvoted comments

For me, I had to add

 "rootDir": "."

under options in project.json. Otherwise, it will use the root of the application.

https://nx.dev/packages/js/executors/tsc#rootdir

For me it was the missing package.json in the buildable libraries. Added a package.json with name and version and everything worked fine.

Facing same issue.

Solution for me here: https://github.com/nrwl/nx/issues/10785#issuecomment-1159017136

For v14.7.5, it works for me even if I only added an empty object for pluginsConfig e.g.

// nx.json
{
  // ...
  "pluginsConfig": {}
}

Very weird. Not sure why.

Also, it would be nice to have a proper solution, rather than having to manually add this in every time we create a new repo

Facing same issue too.

For me, I had to add

 "rootDir": "."

under options in project.json. Otherwise, it will use the root of the application.

https://nx.dev/packages/js/executors/tsc#rootdir

This did it for me. I was having this issue with clean newly-created libraries in a monorepo https://github.com/jcguarinpenaranda/test-monorepo-submodules

For me it was the missing package.json in the buildable libraries. Added a package.json with name and version and everything worked fine.

Exactly what worked for me just now!

Also you have to make sure the name in package.json doesn’t contain more than one “/”

In my situation this was the problem:

Using Nx v15.8.9.

I had a library called shared-my-lib,

- apps
- libs
  - shared
     - my-lib

with a tsconfig path alias like @acme/shared/my-lib. This library is a publishable library, so I had it’s package.json name property changed to @acme/my-lib.

So the mismatch between

workspace alias: @acme/shared/my-lib and package.json name: @acme/my-lib

made building other libraries depending on it fail with the File is not under 'rootDir' '<...>'. 'rootDir' is expected to contain all source files error.

@sir-captainmorgan21 Yes the solution is working for me.

I clone the repo then I add analyzeSourceFiles and targetDefaults and run nx reset and now it’s compile.

Thanks.

For me, this issue can be closed

Just for information, I had to add this to the nx.json file.

nx.json

{
  "targetDefaults": {
    // ...
    "build": {
      "dependsOn": ["^build"]
    },
    // ...
  },
  // ...
  "pluginsConfig": {
    // ...
    "@nx/js": {
      "analyzeSourceFiles": true
    }
    // ...
  }
}

@wizardnet972 and anyone else who’s facing this issue:

Solution:

  1. nx migrate latest
  2. In nx.json, in targetDefaults you can add
"targetDefaults": {
...
    "build": {
      "dependsOn": ["^build"]
    },
...
}

No need for anything else. Here’s the original repo posted here, with solution. Let me know if this helps, or if you think we could close the issue.

For me, I had to add

 "rootDir": "."

under options in project.json. Otherwise, it will use the root of the application. https://nx.dev/packages/js/executors/tsc#rootdir

This did it for me. I was having this issue with clean newly-created libraries in a monorepo https://github.com/jcguarinpenaranda/test-monorepo-submodules

This was the fix for me also. A little disconcerting as the fix seems very unintuitive.

@sir-captainmorgan21 Yes the solution is working for me.

I clone the repo then I add analyzeSourceFiles and targetDefaults and run nx reset and now it’s compile.

Thanks.

For me, this issue can be closed

In my case, just running nx reset did the trick

In my case, I do some refactoring between packages and VSCode automatically add moved file totsconfig.lib.json.

When I remove it - issue gone

Снимок экрана 2022-09-06 в 09 57 31

@codewaseem , @itsrennyman you can vote in the @wizardnet972 post to promote the issue