NX: How to create a NX workspace in an existing empty git repository instead of creating it under a workspace name?
Asked Answered
W

4

8

I have an existing repository. I have cleared all the unwanted files as I wanted to start a new NX-integrated project. But, I want to continue using the same repository as there are some files which I would prefer to be present in the root directory.

If I run npx create-nx-workspace@latest --preset=next --packageManager=yarn, it will ask a bunch of questions and create the project under the workspace name. I want the files to reside directly under the existing current directory rather than creating a new directory with the workspace name under the current directory.

For example, instead of

my-project
--nx-workspace-name
----package.json
----lib

I want the files to be under

my-project
--package.json
--lib

I went through all the create work space flags but didn't find a relevant flag which could resolve this issue. At present, I copy all the files and place them in the root directory. Then, I delete the folder with the workspace name.

An example would be to use something like the --flat flag while creating the NX react component which creates the files in the same directory without nesting the files under any new directory.

Is it possible to avoid this extra step?

Wreck answered 14/12, 2022 at 16:28 Comment(0)
H
1

I had the same issue and it seems create-nx-workspace command do not have any build-in flag to ignore hidden ".git" directory. I tried to use "--skipGit=true" flag but still it complains about hidden ".git" directory. What worked for me is to do things from scratch by using local git repo and then connecting it to remote git repo.

Steps:

  1. Empty your existing repository on remote/server and make sure there are no files or folders in the "main" branch and there are no other branches. Delete other branches as it might cause inconsistency.
  2. Delete your existing git repository folder. For e.g. say your existing repository name was "my-org-repo" then delete "my-org-repo" folder
  3. Go to the parent directory in terminal/cmd inside which you want to create the new nx-workspace folder and run "create-nx-workspace" command
  4. "create-nx-workspace" initializes the new git repo for you if you need to initialize manually and provide specific branch name you can pass the "skipGit" flag
  5. Now cd inside newly created directory cd my-org-repo and run git remote add origin [your-git-repo-url]
  6. Check if the remote is successfully added by running git remote -v. It should show something like origin [your-git-repo-url] (fetch) origin [your-git-repo-url] (push)
  7. Push your local repo changes to remote repo branch by running git push --set-upstream origin main. Replace main with your branch name in case it's different
  8. Now you have new repo setup with Nx at root level
Helfand answered 9/1, 2023 at 10:3 Comment(0)
G
1

I ran into the same situation and I just moved the resulting files one directory up.

Glad answered 24/5, 2023 at 9:13 Comment(0)
A
1

I think you need to install nx in an existing repo/workspace. Run the following command in your root folder:

npx nx@latest init

Check out more here: https://nx.dev/getting-started/installation

Altonaltona answered 28/1, 2024 at 4:41 Comment(1)
they probably want an entire new workspace, not repurposing an existing oneUsherette
Z
0

(No) Solution:

Looks like this cannot be done at the moment so I opened an issue here.

Workaround:

Step 1.

Clone your project (let's say it's called urproject)

git clone urproject

Step 2.

Enter the cloned directory.

cd urproject

Step 3.

Create the workspace via the wizard as normal. FOR THE DIRECTORY INPUT ENTER ANY NAME (like urproject)

npx nx create-nx-workspace

Step 4.

Move the created project from the created subdirectory to the current one. It's important to match the .gitignore and other dotfiles as well.

mv ./urproject/* /urproject/.* .

Step 5.

Remove the temporary directory created by the wizard.

rm -rf ./urproject
Zeringue answered 2/3, 2024 at 16:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.