'Cannot find module 'nx/src/config/workspaces' on creating a React app on existing Nx project

I am getting the following error and I add the react app to the already existing Nx project on my system:

Require stack:
- C:\Users\HP\OneDrive\Documents\amagi-qtc\node_modules\@nrwl\devkit\index.js
- C:\Users\HP\OneDrive\Documents\amagi-qtc\node_modules\@nrwl\react\src\utils\lint.js
- C:\Users\HP\OneDrive\Documents\amagi-qtc\node_modules\@nrwl\react\src\generators\application\application.js
- C:\Users\HP\OneDrive\Documents\amagi-qtc\node_modules\nx\src\shared\workspace.js
- C:\Users\HP\OneDrive\Documents\amagi-qtc\node_modules\nx\src\cli\init-local.js
- C:\Users\HP\OneDrive\Documents\amagi-qtc\node_modules\nx\bin\nx.js
- C:\Users\HP\AppData\Roaming\npm\node_modules\@nrwl\cli\node_modules\nx\bin\nx.js
- C:\Users\HP\AppData\Roaming\npm\node_modules\@nrwl\cli\bin\nx.js

I did to install react in a nx project

yarn add -D @nrwl/react

But when I run this command:

nx g @nrwl/react:app my-new-app

It gives me the above error.



Solution 1:[1]

The issue could have happened due to the project not having the latest NX version packages.

Ensure the nx cli is installed in your local project, yarn add -D @nrwl/cli.

Run nx migrate latest from your project to do the upgrades, it will update your package.json file.

You can review the changes made to package.json and run yarn to install them.

Finally, you have to run the migrations by running nx migrate --run-migrations.

Reference: https://nx.dev/using-nx/updating-nx#updating-nx

Solution 2:[2]

        String a = "Hello World!";
        int n = 7;
        n = n + a.substring(0, n).split(" ").length - 1;
        a = a.substring(0, n);
        System.out.println(a);

Solution 3:[3]

For java 9+:

String test(int cnt, String string) {
            AtomicInteger n = new AtomicInteger(cnt);
            return string
                    .chars()
                    .boxed()
                    .peek(value -> {
                        if (!Character.isWhitespace(value)) {
                            n.decrementAndGet();
                        }
                    })
                    .takeWhile(value -> n.get() >= 0)
                    .map(Character::toString)
                    .collect(Collectors.joining());
    
        }

EDIT: For java 8:

 String test(int cnt, String string) {
    AtomicInteger n = new AtomicInteger(cnt);
    return string
            .chars()
            .boxed()
            .peek(value -> {
                if (!Character.isWhitespace(value)) {
                    n.decrementAndGet();
                }
            })
            .filter(value -> n.get() >= 0)
            .map(ch->String.valueOf((char) ch.intValue()))
            .collect(Collectors.joining());

}

but this will go through every character of the string regardless of the provided index. It seems there is no effective way to dynamically limit the output of the stream in java 8.

Solution 4:[4]

One solution I can think of:

// Pseudo-code
string // i.e: "Hello World"
n // i.e = 7
counter1, counter2
for character in string {
    if counter1 == n {
        break;
    }
    if character is not `space` {
        ++counter1;
    } else {
        ++counter2;
    }
}
return string.subString(0, counter1+counter2);

Solution 5:[5]

One other pragmatic solution which is more flexible for edge cases -> what if the last index is also a whitespace e.g.

String ori = "Hello   World";
    int cCount = 6;
    
    String endSub = ori.substring(cCount);
    Stream<Character> endSubStream = endSub.chars().mapToObj(c -> (char) c);
    int whitespacesEnd = (int)endSubStream.filter(p -> Character.isWhitespace(p.charValue())).count();
    
    String beginSub = ori.substring(0, cCount);
    Stream<Character> beginSubStream = beginSub.chars().mapToObj(c -> (char) c);
    int whitespaces = (int)beginSubStream.filter(p -> Character.isWhitespace(p.charValue())).count();
    
    String text = ori.substring(0, cCount + whitespaces + whitespacesEnd);
  System.out.println(text);

https://www.online-java.com/D4qarKf5Cl

Solution 6:[6]

This should solve your requirement.

String stringToBeProcessed = "Hello World! to you";
int count = 0;
int limit = 7;
StringBuilder sb = new StringBuilder();
for(Character c : stringToBeProcessed.toCharArray()){
   if(!Character.isSpaceChar(c)){
       count = count + 1;
   }
   if(count > limit){
       break;
   } else {
       sb.append(c);
   }
}
System.out.println(sb);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kaoskeya
Solution 2 dangerousmanleesanghyeon
Solution 3
Solution 4 Tung
Solution 5 Matthias Reisner
Solution 6 Mohaideen Samsudeen