Failed to update react-scripts - eslint

I have a react application that was created with create-react-app and is using react-scripts version 3.4.2. The app works great but when I run eslint against it I get many invalid no-unused-vars errors. By invalid I mean that when I go to the file that variable is actually being used.
Based on this answer it seems the issue is related to #typescript-eslint/parser and #typescript-eslint/eslint-plugin. So I went ahead and executed npm list #typescript-eslint/parser #typescript-eslint/eslint-plugin on my app and this is what I get:
myapp#0.1.0 /Users/diego/myapp/client
└─┬ react-scripts#3.4.2
├─┬ #typescript-eslint/eslint-plugin#2.34.0
│ └── #typescript-eslint/parser#2.34.0 deduped
├── #typescript-eslint/parser#2.34.0
└─┬ eslint-config-react-app#5.2.1
├── #typescript-eslint/eslint-plugin#2.34.0 deduped
└── #typescript-eslint/parser#2.34.0 deduped
As seen all occurrences of the two potentially conflictive libraries are included by react-scripts app and that is why I am trying to update it.
I tried updating to 4.0.3 and when I do that eslint starts to work fine (those incorrect no-unused-vars are gone) but my app is unresponsive (I click on different buttons and/or links and nothing happen). The only error I see in the console after the update is this one:
Uncaught ReferenceError: process is not defined
at Object.4043 (<anonymous>:2:13168)
at r (<anonymous>:2:306599)
at Object.8048 (<anonymous>:2:9496)
at r (<anonymous>:2:306599)
at Object.8641 (<anonymous>:2:1379)
I was searching this error and I found this question. The error is the same maybe not what the developer describes but reading through the answers I see many of them point to react-scripts as the root cause and that maches my scenario.
I tried some of the solutions in that question, in particular method 1 from this answer (I did not try method 2 because I am not sure if adding craco is what I want) and the solution from this other answer too but they do not work.
I also tried updating to react-scripts version 5 but I get A LOT of errors.
Is someone facing this issue or at least has some clue that could help me?
Thanks in advance.

The error message that you shared seems to be related to a bug with react-error-overlay.
react-scripts 4.0.3. uses react-error-overlay 6.0.9. However, 6.0.10 is marked as a patch, so npm uses 6.0.10 instead of 6.0.9, but 6.0.10 is not compatible with 6.0.9.
On the other hand, react-scripts 5 does not use react-error-overlay, but might be causing breaking changes with your other packages, hence the other errors.
You can find more information about the react-error-overlay bug in these issues on the CRA repository:
Hot Reload Fails, DOM adds an additional iframe containing entire
contents of the html DOM element #11880
Is this the bug of react-error-overlay? #11773
v5 Regression react-error-overlay build - Uncaught ReferenceError:
process is not defined #11771
Here's a possible solution with react-scripts 4.0.3.:
In your project's package.json file:
In dependencies, ​set the react-scripts version to 4.0.3.
Under dependencies, in resolutions, add react-error-overlay 6.0.9.
In devDependencies, add react-error-overlay 6.0.9.
"dependencies": {
...
​ "react-scripts": "4.0.3",
...
},
"resolutions": {
​"react-error-overlay": "6.0.9"
},
...
"devDependencies": {
...
"react-error-overlay": "6.0.9",
...
}
Delete your project's node_modules folder and package-lock.json file.
Run npm install.
Run npm install react-error-overlay#6.0.9.

Related

Using eslint-plugin-import in a monorepo not reporting errors when it should?

If I have the following monorepo set up
packages/app1 has is-even in it's package.json
packages/app2 does not have is-even in its package.json
packages/app2 tries to use is-even
Then currently, I don't get any warning from something like eslint-plugin-import, when preferably, I would like an error because if I publish app2, then any user that tries to install it from NPM will receive errors because it does not properly specify that it needs is-even as a dependency
Reproducible case here with a minimal monorepo https://github.com/cmdcolin/yarn_workspaces_eslint_plugin_import
This was fixed by adding
extends:
- eslint:recommended
- plugin:import/recommended
rules:
import/no-extraneous-dependencies: error
This makes it detect the error properly, e.g. this message is expected and good now
yarn run v1.22.15
$ eslint .
/home/cdiesh/test/packages/app2/src/index.js
1:1 error 'is-even' should be listed in the project's dependencies. Run 'npm i -S is-even' to add it import/no-extraneous-dependencies
✖ 1 problem (1 error, 0 warnings)

React testing: "TypeError: MutationObserver is not a constructor"

The Problem
I'm trying to test a new release of an internal component library, which recently upgraded some dependencies and now uses Jest 26 internally. That library also exports some testing helpers.
In another codebase that utilizes the aforementioned component library, I'm getting the following when running certain unit tests:
TypeError: MutationObserver is not a constructor
at /<path_to_repo>/node_modules/#testing-library/dom/dist/wait-for.js:78:18
This may or may not be related to the fact that the testing helpers exported by the component library relied on Jest 26. (It's hard to tell if these helpers are being used -- we've got some very big "unit" tests.)
Relevant package versions (in the consuming codebase):
react-scripts 3.4.4
Jest 24.9.0
testing-library/dom 7.26.3
testing-library/jest-dom 5.10.0
testing-library/react ^10.0.0
Running yarn list jsdom yields the following:
├─ jest-environment-jsdom-fourteen#1.0.1
│ └─ jsdom#14.1.0
├─ jest-environment-jsdom#24.9.0
│ └─ jsdom#11.12.0
└─ jsdom#16.4.0
I know that upgrading react-scripts and Jest would likely fix this issue, but I'm trying to find a way that doesn't involve that. (We ideally don't want this to be a prerequisite for upgrading to our new component library version.)
Attempted Solutions
Forcing a Newer jsdom
First I tried adding
"resolutions": {
"jsdom": "^14.0.0"
},
to my package.json. This did make yarn list jsdom output only jsdom#14.1.0, but I still got the same TypeError. Resolving to ^15.0.0 resulted in the same error, and resolving to ^16.0.0 gave me the following error:
TypeError: this.dom.runVMScript is not a function
at JSDOMEnvironment.runScript (node_modules/jest-environment-jsdom/build/index.js:187:23)
Using jest-environment-jsdom-*
I tried following this thread by installing jest-environment-jsom-sixteen. Altering my test script in package.json and running yarn test --showConfig yields a bunch of info including the following line:
"testEnvironment": "/<path_to_repo>/node_modules/jest-environment-jsdom-sixteen/lib/index.js",
If I add a console.log(navigator.userAgent) to my test, I also get
Mozilla/5.0 (darwin) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/16.4.0
However, I still get the same TypeError: MutationObserver is not a constructor.
I also tried jest-environment-jsdom-fourteen and jest-environment-jsdom-fifteen, and tried while forcing jsdom to ^14.0.0. Same result.
Using mutationobserver-shim
I ran yarn add -D mutationobserver-shim and imported it (with import 'mutationobserver-shim') in my jestSetup.ts (which is what globalSetup in my Jest configuration points to), but that resulted in the following error:
ReferenceError: window is not defined
at Object.<anonymous> (/<path_to_repo>/node_modules/mutationobserver-shim/dist/mutationobserver.min.js:12:1)
Adding the import instead to my test file yielded the same TypeError as above, and adding a console.log(global.MutationObserver) to my test file yielded undefined.
A Not-So-Great Solution
After a lot of trial and error, I discovered that if I 1) have jest-environment-jsdom-sixteen installed and tell Jest to use it and 2) add import 'mutationobserver-shim' in my test file, and the tests will pass. However, if I put add the import to my Jest globalSetup file, it doesn't work. I get the same ReferenceError: window is not defined error as before.
My Question
How can I get rid of both errors without requiring users of our component library to add an import statement to all of their test files?
A polyfill should be applied in setupFilesAfterEnv, because this is where the jsdom environment is instantiated and window becomes available. window isn't supposed to be available in globalSetup because it doesn't run in a test scope.
In case this is an unejected create-react-app project, the setup file that corresponds to setupFilesAfterEnv is src/setupTests.ts (src/setupTests.js).
That the newer jsdom version causes an error means it's incompatible with the old Jest version (24). They should be ejected and upgraded, or newer CRA (react-scripts#4) with Jest 26 support has to be used.

yarn warning " > bootstrap#4.x.x" has unmet peer dependency "jquery#1.9.1 - 3"

When we run yarn in a project with no node_modules directory, we get the following warning message during the dependency installs:
warning " > bootstrap#4.4.1" has unmet peer dependency "jquery#1.9.1 - 3".
warning " > bootstrap#4.4.1" has unmet peer dependency "popper.js#^1.16.0".
However, it appears that bootstrap and react-bootstrap works fine without doing anything to fix these 2 warnings. package.json also do not contain entries for these 2 packages.
Question: Why does Bootstrap and related files appear to continue working despite having unmet peer dependencies?
Isn't the whole point of yarn and npm be to manage these dependencies?
What is the proper way to resolve these warnings?
Part of packages.json
{
...
"dependencies": {
"bootstrap": "^4.4.1",
"react-bootstrap": "^1.0.0",
...
}
}
jquery and popper.js are used by Bootstrap for their Javascript components (dropdown, modal etc). However: If you only use the CSS part - you do not need these. That is probably why they are listed as "peer-dependencies".
There is a Pull Request to mute theses warnings. They are just warnings afterall.
react-bootstrap does not need/replaces jquery but it uses popper in another Version (#popperjs/core) for tooltip placement. That is why everything is working - even though Bootstrap is missing some "peer-dependencies". Libraries like (react|vue|ng)-bootstrap usually only use the CSS part of Bootstrap and rewrite the Javascript part entirely.
What is the proper way to resolve these warnings?
To my understanding: You will have to add them as dependencies in your project - even though you do not actually need them. Or... Ignore the warnings.

Running into "couldn't infer parser" error using vue-cli

I'm getting an error repeatedly when trying to build a new webpack project using vue-cli. I'm following along with the docs on the latest build (3.0.0-beta.11), also tried with an earlier version, which wasn't beta.
When I run yarn serve it attempts to start the dev server and build the project but fails here:
error in ./src/App.vue?vue&type=template&id=7ba5bd90
Module build failed: Error: No parser and no file path given, couldn't infer a parser.
at normalize (/Users/cory/Code/chickadee/my-project/node_modules/prettier/index.js:7051:13)
at formatWithCursor (/Users/cory/Code/chickadee/my-project/node_modules/prettier/index.js:10370:12)
at /Users/cory/Code/chickadee/my-project/node_modules/prettier/index.js:31115:15
at Object.format (/Users/cory/Code/chickadee/my-project/node_modules/prettier/index.js:31134:12)
at actuallyCompile (/Users/cory/Code/chickadee/my-project/node_modules/#vue/component-compiler-utils/dist/compileTemplate.js:93:29)
at compileTemplate (/Users/cory/Code/chickadee/my-project/node_modules/#vue/component-compiler-utils/dist/compileTemplate.js:26:16)
at Object.module.exports (/Users/cory/Code/chickadee/my-project/node_modules/vue-loader/lib/loaders/templateLoader.js:42:20)
# ./src/App.vue?vue&type=template&id=7ba5bd90 1:0-194 1:0-194
# ./src/App.vue
# ./src/main.js
# multi (webpack)-dev-server/client/index.js (webpack)/hot/dev-server.js ./src/main.js
About my setup
Mac OS
I'm running node v8.5.0
packages are installed with yarn
The Things I've Attempted
Different versions of vue-cli to generate a new project. The projects generate and install modules.
Tried removing the prettier module, but the error still seems to come up.
Tried reinstalling all modules.
What else might I try to get past this error?
Removing the current node_modules folder from the project, adding "prettier": "^1.12.1" to package.json and running npm install solved the issue.
Another option is to run npm install prettier#1.12.1 without removeing the node_modules folder before
Update:
For some users, verion 1.12.1 did not work
#Kivin proposed another solution that can be found here: vue webpack template missing parser
Known issue and it will be fixed in the next version of vue-cli
In prettier 1.13.0, default parser was removed with a minor version(used to be babylon)
Issue: https://github.com/vuejs/component-compiler-utils/issues/14
Issue at prettier repo: https://github.com/prettier/prettier/issues/4567
Sorry, we committed the age-old semver sin- we knew this was a breaking change, but because it would only affect a subset of our users, we didn't bump the major version, because we didn't want to create friction for our users to upgrade.
To get the old behavior, add parser: "babylon". You may also want to lock prettier to a specific version in your package.json.
Running npm install prettier#1.12.1 solved it for me. Thanks lsxliron.
Right now, I tried all the options..downloading and updgrading prettier...but none workied. Until I studied what happened carefully.
Apparently, the prettier team removed the default parser which was babylon and in so doing...broke the internet.
Just kidding.
Issue repo
The simplest solution, according to them would be to simply add the parser back.
This has been picked up by the Vue team and its expected to be shipped with the latest fix release.
If you are using Vue Loader/Yarn, dont even bother to try all the suggestions...I tried them all.
What fixed it for me was...go to
node_modules\vue-loader\lib\template-compiler ...open index.js and look for
// prettify render fn
if (!isProduction) {
code = prettier.format(code, { semi: false})
}
and change the lines to:
// prettify render fn
if (!isProduction) {
code = prettier.format(code, { semi: false, parser: 'babylon' })
}
Thats it!
Then once the issue got fixed, everything will just be rolled back and you will still be fine.
Try this...it will save you countless minutes of searches....
As already pointed out by the various resonses, you may need to rollback the version of the prettier package.
in your package.json file, you may need to force npm to use a version (i.e. remove the hat ^)
mine looks something like this
"devDependencies": {
"prettier": "1.12.1",
"typescript": "^2.6.1",
"vue": "^2.5.16",
"vue-styleguidist": "^1.4.4",
"vue-webpack-loaders": "^1.0.6",
"webpack": "^3.1.0"

Typescript is invalid

I upgraded angular to 4 and angular cli to 1.03.
After I run the command npm ls typescript I get:
+-- #angular/cli#1.0.3
| `-- typescript#2.2.2
`-- typescript#2.2.2 invalid
npm ERR! invalid: typescript#2.2.2 C:\development\projects-git\mydoc\static-web\node_modules\typescript
What's the difference between the first and the second typescript, why is the second one invalid/how do I fix it?
I think the error above is the reason why I get the error below so I hope there is a fix for both so I can run my application again.
ERROR in AppModule is not an NgModule
ERROR in ./src/main.ts
Module build failed: TypeError: Cannot read property 'newLine' of undefined
at Object.getNewLineCharacter (..\static-web\node_modules\typescript\lib\typescript.js:9580:20)
at Object.createCompilerHost (..\static-web\node_modules\typescript\lib\typescript.js:66674:26)
at Object.ngcLoader (..\static-web\node_modules\#ngtools\webpack\src\loader.js:202:31)
# multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts
I had a similar issue where i even had two different typescript versions listed when i did npm ls typescript.
What's the difference between the first and the second typescript
The first one is listed as a dependency of #angular/cli, the second one is a dependency of your own project.
why is the second one invalid/how do I fix it?
In my case, i had typescript defined under both, dependencies and devDependencies section in my package.json.
I removed it from dependencies and the duplicate output disappeared.

Resources