How to install typescript + jest with create-react-app? - reactjs

I want to install typescript and jest in a create-react-app-based app. I feel that since this is such a common installation choice there must be at least one "everything just works" set of configuration steps to follow.
I initially ran npx create-react-app my-project --template typescript. That was great for a while. I wrote several thousand lines of code with that. And then one day I decided I wanted to add some mocks to a spec file with code like this:
import jest from "jest";
jest.mock('./somemodule');
...but the "jest" instance is undefined. So I followed directions in different articles to install further devDependencies. But these seem to conflict with dependencies inside of create-react-app, suggesting that I need to focus on setting up my project correctly the "Create-react-app Way" according to its expectations.
Rather than burden StackOverflow with the details of my build and package management issues, I figure I'll just ask the simpler question - what is the correct way to set up create-react-app+typescript+jest in a way where it doesn't have a bunch of irritating, random problems?
And then after I've followed this advice, if I still have problems, I might ask a second, separate SO question with specific details.

The command below should create a new React project supporting Typescript and Jest without need of further modification.
npx create-react-app my-app --template typescript
Details about the above command can be found here: https://create-react-app.dev/docs/adding-typescript/
The above command will set up a new project. But if, like me, you have an existing create-react-app project with issues like:
the Jest module is undefined or doesn't seem to have expected functions
your npm run start and npm run build fail due to conflicting dependencies
or you just want to get to a "standard" package.json configuration without all the hassle of copying your source into a new project.
...you can use the process below to fix/upgrade your package.json:
Use the same npx create-react-app my-app --template typescript command above to create a separate working ("Good") project with all the right dependencies. This project will just be used as a reference.
Compare the package.json of the Good project to the non-working ("Bad") project, and make sure the Bad package.json has the same modules and version#s as the Good package.json. You probably don't have to delete any modules in the Bad package.json that aren't in the Good package.json.
rm -rf node-modules in the Bad project.
rm package-lock.json in the Bad project.
npm install in the Bad project.
Self-answering for posterity.

Related

Problem with babel and tailwindcss in Next.js: Cannot find module 'xwind/babel'

I installed tailwindcss inside of my Next.js application. This was the error message that I received
error - ./node_modules/next/dist/client/dev/amp-dev.js
Error: Cannot find module 'xwind/babel'
This is how I installed the Next.js application:
npx create-next-app -e with-tailwindcss ./
These are the dependencies I installed:
npm install graphql graphql-request html-react-parser moment react-multi-carousel sass
Happened to me as well just few minutes ago. Not sure if that is the same case for you. It created for me components folder, .babelrc file and js files in pages folder. Not sure if that is your case, but that's what happened to me. In case just follow with solution below.
Solution
Remove .bablerc file and components folder along with js files in pages folder.
More details
This is strange because if you look at the repository of Next.js example with-tailwindcss. It doesn't have those. Not sure how that happened. We can elaborate more in the comments.
Also plugin for babel xwind/babel does have dependency check to allow only tailwindcss version <2. There is an issue for that. In my opinion this repo is unmaintained and will either get forked and replaced as a main for npm package or something similar.
The create-next-app is installing with-tailwind-emotion template instead of with-tailwind for some reason.
For now, a good way is to create a normal typescript template with create-next-app and add tailwind manually.
So your steps would be:
Step 1:
without typescript:
npx create-next-app ./
or with typescript:
npx create-next-app --ts ./
Step 2:
Docs to install tailwind with next.js:
https://tailwindcss.com/docs/guides/nextjs

NextJS create-next-app not working properly

I have installed NextJS using both methods npm install next react react-dom and npx create-next-app appname multiple times, but the directories are supposed to look like:
pages, api(_app.js, index.js), public, styles, .next, node_modules
But in MY project they look like:
components, pages (index.js only), static, .next, node_modules
I saw multiple installation procedures but they all give the same project directories (latter) TO ME, and the former directories in their tutorials. I cannot follow any tutorials on nextjs due to this!
I found the answer on this GitHub issue !
npm i -g create-next-app
If it says files already exist, try adding --force to the command above. It worked for me, I don't know how, but it did!

create-react-app with -–template typescript not creating .tsx files

I decided to use React JS with TypeScript. Doing a simple hello world project. Referring two books, and they both suggest the following. Brand new project, not upgrading or any of those scenarios. Starting fresh.
npx create-react-app try-react -–template typescript
That works fine.
But, the files, they still have the .js extension. In the book, one of them has a screenshot, it is already with .tsx extension.
I have looked at this link, https://create-react-app.dev/docs/adding-typescript/. It says,
If you've previously installed create-react-app globally via npm install -g create-react-app, we recommend you uninstall the package using npm uninstall -g create-react-app or yarn global remove create-react-app to ensure that npx always uses the latest version.
Global installs of create-react-app are no longer supported.
So, I removed the global install. tried again.
Same thing. no files with .tsx extension. The book author does not mention anything about having to rename these files manually.
So, my question is,
is the template not supposed to automatically set all extensions to .tsx?
is the only way to do this is to manually change from .js to .tsx?
do we always do this, and that is indeed the norm. Perhaps, the book author did do it manually but forgot to include it in the instructions?
I had the same problem. What solved it for me:
npm install cra-template-typescript
For some reason the template was not there for me.
In my case what solved it was installing the typescript template globally
npm install -g cra-template-typescript
Once I did that, I ran
npx create-react-app client-app --template typescript
Ant it worked. All script files where .tsx
Providing an independent answer because I don't have the reputation to comment on or edit #ChrisPabst's answer.
I'm also using the David Choi book, and stumbled across this question for the same reason as the parent...I couldn't get --template typescript to generate typescript files. ChrisPabst's answer got me to look more closely at the difference between commands pasted from the book, and commands directly entered, and sure enough, here's a paste from Chapter 5:
npx create-react-app class-components -–template typescript
Zoom in on the template hyphens, re-pasted here:
-–
Now I type in the hyphens directly:
--
Now let's re-paste the hyphens from the question text itself:
-–
It definitely looks like the first set, not the second.
EDIT: If you get the ASCII values of the above pasted characters (e.g. fire up a python interpreter, and then type ord("–") for each character, the pasted text will give you
45 and 8211
while entering it yourself is 45 and 45.)
tl;dr : If you're using the David Choi book "Full-Stack React, TypeScript, and Node", all of the advice above about cra-react-template is a complete red herring. Paste the command, but before you press enter, go back and type in the double-hyphens yourself. A typescript template will be generated correctly.
For completeness, I believe the book in question is Full-Stack React, TypeScript, and Node by David Choi. I too am learning React from this book, and I also ran into this problem. This issue was not actually in the environment, but in the command itself (which you can see in the original question).
If you are reading the book online, and copy the command to set up a new React app, you get
npx create-react-app hooks-components –template typescript
But the book for some reason has a non-ascii dash instead of the correct --. So the template arg is ignored.
What I kept missing was this error.
npm ERR! arg Argument starts with non-ascii dash, this is probably invalid: –template
So yes it is supposed to work; No you don't have to do anything extra.
You need to try two things:
npm uninstall -g create-react-app
or
yarn global remove create-react-app
This should work as of today.

i18n message extraction in CRA using TypeScript

I'm trying to get i18n message extracted (defined by react-intl's defineMessages) to work properly in a CRA using TypeScript.
I've got an existing react app bootstrapped by CRA with a couple of hundrets of lines of code. So rewriting the application w/o TypeScript isn't an option.
Here's what i've tried so far:
First Attempt
Following this guide in order to get it to work.
When closely following the guide (although react-intl-cra is deprecated) the language files will generate properly.
However if you create the app using create-react-app react-intl-example --typescript and change the script to
"extract:messages": "react-intl-cra 'src/**/*.{js,jsx,ts,tsx}' -o 'src/i18n/messages/messages.json'"
it will break with a compiler error.
Second Attempt
Since react-intl-cra was refering to a react-app-rewired solution, I've tried adding it alongside customize-cra and babel-plugin-react-intl to a freshly generated CRA (using TS). However no luck there as well and after some short period of research I found that it's officially not supported.
Third attempt:
Adding extract-react-intl-messages to my project and running:
$ npx extract-messages -l=en,ja -o app/translations -d en --flat false 'src/**/!(*.test).tsx'
failed with an error as well.
I ran out of ideas, hence I came here to ask. From what I've seen TypeScript has been well advertised in the last couple of years and I don't think I have to justify that React is still pretty hyped. Moreover I can't imagine, that i18n is an uncommon concern in application development.
However I wasn't able to find any up-to-date guide or anything useful on npmjs.com.
TL;DR;
What I need:
Extract messages from defineMessages from react-intl into json files
Must work in a CRA using --typescript
Should not utilize npm run eject
What I've tried:
react-intl-cra
react-app-rewired + customize-cra + babel-plugin-react-intl
extract-react-intl-messages
It's explained here: https://github.com/akameco/extract-react-intl-messages#typescript
Basically you need to
npm install --save-dev #babel/core #babel/preset-typescript #babel/preset-react
and add
#babel/preset-typescript
to your .babelrc:
{
"presets": [
"#babel/preset-react",
"#babel/preset-typescript"
],
}
Then you can run
npm run extract-messages 'src/**/*.[jt]sx'

How to edit config files in react-create-app?

I want to use some third-party packages upon react, which are:
https://ant.design/ - a design system
https://github.com/vazco/uniforms - for generating and validating forms.
I am using create-react-app for as a boilerplate
After installing all packages (with yarn) and running yart start I am getting this error:
How can I add #babel/plugin-proposal-class-properties without running yarn eject to modify .babelrc file?
Or is there any other solution for this problem?
It looks like the only way is to eject. CRA has purposely avoided too much customization. See https://github.com/facebook/create-react-app/issues/167

Resources