Thursday, August 29, 2019

sh: react-scripts: command not found

sh: react-scripts: command not found
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! syscall spawn

It could be that you are running a script from Visual Studio Code's NPM-Scripts sidebar. To fix that, add the following configuration in settings.json file under .vscode directory. If .vscode directory nor settings.json is not existing, just create those two.

    "npm.packageManager": "yarn"

Restart VS Code, then click the start script from NPM Scripts sidebar.

If you don't want to create .vscode directory and settings.json file, there's a global settings where npm.packageManager setting could be applied. This can be set by going to the menu under File > Preferences > Settings. (Code > Preferences > Settings on macOS). On User tab, type package manager. Change the Npm: Package Manager to yarn.

Restart VS Code.

Alternatively, it can be set directly on global settings.json file by opening the Command Palette. Command Palette can be accessed in the menu under View > Command Palette (keyboard shortcut: Control+Shift+P on Windows, Command+Shift+P on Mac). Then type settings

Choose Preferences: Open Settings (JSON).

Then add "npm.packageManager", then choose "yarn":

Restart VS Code

Thursday, August 15, 2019

Error: Invariant failed: You should not use Link outside a Router

As of the time of this writing, the latest version for connected-react-router is 6.5.2, and the latest version for react-router and react-router-dom is 5.0.1. However, I got the errors that says Link should not be used outside a Router when using those versions.

Cause of the error:

The example of connected-react-router (even if the latest version is 6.5.2) uses connected-react-router 6.0.0 though:

That example uses react-router and react-router-dom 4.3.1.

To fix the error, just use the same version from the example. Pin the version of connected-react-router to 6.0.0, and both react-router, react-router-dom to ^4.3.1.

"dependencies": {
    "connected-react-router": "6.0.0",
    "history": "^4.9.0",
    "react": "^16.9.0",
    "react-dom": "^16.9.0",
    "react-redux": "^7.1.0",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "3.1.1",
    "redux": "^4.0.4"

Then delete yarn.lock and node_modules:
$ rm yarn.lock
$ rm -rf node_modules

Then reinstall:
$ yarn

Then add prop-types:
$ yarn add prop-types

As of the time of this writing, I got ^15.7.2 for prop-types.

That's all.

Wednesday, August 14, 2019

npm install -g without sudo

On my machine, ~/.npmrc file is not existing. So I created one, and then add the following:


And in ~/.bash_profile (which is also not existing on my machine, so I created one), I added this line:

export PATH="$HOME/.npm/bin:$PATH"

Restarted terminal, npm install -g now works even without the sudo

Followed these steps:

Thursday, August 8, 2019

All things being equal, they are not equal

I put all my shared files on external drive with exFAT file system. Found out later that the default cluster size (128 KB) that macOS allocates for exFAT drives is atrociously wasteful for npm-based projects. The external drive is permanently plugged to the back of iMac's Thunderbolt 3 port.


For 128KB cluster size partition, a 3.5GB npm-based project consumes 81GB of space, yes that's 81GB not 8.1GB. For 4K cluster size partition, a 3.5GB npm-based project consumes 5.44GB only.

I resized the exFAT drive's cluster size to 4 KB, there is a problem when the computer boots though. The exFAT drive affects the boot speed of the computer. Do note that the computer is booting from internal drive (APFS), not from external exFAT drive.

From cold boot to Apple logo, the exFAT drive affects the boot speed of the computer to almost ten minutes.

Here are the other boot speed from various exFAT cluster sizes:

8 KB = 2 minutes 50 seconds
16 KB = 50 seconds
32 KB = 16 seconds
64 KB = 8 seconds
128 KB = 7 seconds

For other file systems cluster sizes:

4 KB APFS = 5 seconds
4 KB NTFS = 5 seconds

For both file systems, they don't affect the boot speed of the computer even if they are permanently plugged to Thunderbolt port.

Ultimately I decided to use NTFS and used third-party software on macOS that can read and write to NTFS drive.

Further space saved by deleting node_modules from npm-based projects by using Yarn Plug'n'Play:

Saturday, August 3, 2019

Could not read from remote repository

Got this error when pushing code to GitHub:

Developers-iMac:experiment-react-route-based-lazy-load dev$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '' to the list of known hosts. Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

One solution that works:

Developers-iMac:experiment-react-route-based-lazy-load dev$ git remote set-url origin
Developers-iMac:experiment-react-route-based-lazy-load dev$ git push -u origin masterEnumerating objects: 28, done.
Counting objects: 100% (28/28), done.
Delta compression using up to 8 threads
Compressing objects: 100% (28/28), done.
Writing objects: 100% (28/28), 302.35 KiB | 12.09 MiB/s, done.
Total 28 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Got the solution from: