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)
For me, I had to add
under
optionsinproject.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.jsonin the buildable libraries. Added apackage.jsonwithnameandversionand everything worked fine.Facing same issue.
For v14.7.5, it works for me even if I only added an empty object for
pluginsConfige.g.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.
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
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,with a tsconfig path alias like
@acme/shared/my-lib. This library is a publishable library, so I had it’spackage.jsonname property changed to@acme/my-lib.So the mismatch between
workspace alias:
@acme/shared/my-liband package.json name:@acme/my-libmade building other libraries depending on it fail with the
File is not under 'rootDir' '<...>'. 'rootDir' is expected to contain all source fileserror.@sir-captainmorgan21 Yes the solution is working for me.
I clone the repo then I add
analyzeSourceFilesandtargetDefaultsand runnx resetand now it’s compile.Thanks.
For me, this issue can be closed
Just for information, I had to add this to the
nx.jsonfile.nx.json@wizardnet972 and anyone else who’s facing this issue:
Solution:
nx migrate latestnx.json, intargetDefaultsyou can addNo 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.
This was the fix for me also. A little disconcerting as the fix seems very unintuitive.
In my case, just running
nx resetdid the trickIn my case, I do some refactoring between packages and VSCode automatically add moved file to
tsconfig.lib.json.When I remove it - issue gone
@codewaseem , @itsrennyman you can vote in the @wizardnet972 post to promote the issue