Sådan går du fra bunden til Create-React-App på Windows

En meningsfuld guide til opsætning af et webudviklingsmiljø på Windows 10

Jeg har været en professionel webudvikler siden marts 2018 og brugte både MacO'er og Windows på det tidspunkt. Mit foretrukne operativsystem, der skal bruges, er Windows uden anden grund end jeg foretrækker tastaturlayoutet.

Med Windows / Linux vil jeg gerne have min Ctrl-nøgle som nøglen til brug til kopipasta-operationer, og jeg kan bruge min venstre pink i stedet for min tommelfinger. Lad os alligevel lade den svage begrundelse være, da det ikke er formålet med dette indlæg!

Hvis du ikke har en Windows-maskine, er dette indlæg sandsynligvis ikke noget for dig. Hvis dette er forskelligt fra det, du bruger, gør det det ikke dårligt.

Lad os se, hvad Ken har at sige om det:

Så jeg er omkring 4 måneder i at bruge Windows på arbejde på fuld tid, og det er slet ikke dårligt.

- PATAGUCCI MANE (@ken_wheeler) 20. december 2018

Dette er opdateringen på min guide fra starten af ​​året om opsætning af et webudviklingsmiljø på en Windows-maskine.

Denne vejledning dækker installation af Ubuntu, men du kan bruge nogle af de andre varianter af Linux, der er tilgængelige i Windows-butikken, Debian-opsætningen vil være meget lig Ubuntu.

Formålet med dette indlæg

At gå fra en ny forekomst af Windows 10 til noget, du kan udvikle webapps med.

Disse instruktioner er til Windows 10 Fall Creators Update og nyere.

Hvad vi skal dække:

  • Installer WSL
  • Aktivér WSL på din maskine
  • opdater, opgrader og fjern automatisk
  • Installer node (med n)
  • Installer Visual Studio-kode
  • Installer Windows Git
  • Installer en terminal (Hyper)
  • Installer Fish Shell!
  • Installer Oh My Fish
  • Fisketemaer med OMF
  • Konfigurer
  • Git konfiguration
  • Opret React-app
  • Brug SSH med GitHub
  • WSL SSH Config
  • Windows Git Bash SSH Config
  • Skift WSL-version
  • Pak dig op!

Installer WSL

Du kan installere Ubuntu fra Microsoft Store, som vil være den første halvdel af installationen, den anden vil være, når du åbner appen.

Aktivér WSL på din maskine

Den officielle vejledning dækker det meget godt, men jeg vil tilføje genvejstasterne her, hvis du ikke vil klikke rundt med musen.

Før det, hvis du ikke har valgt PowerShell som din standardkommandoprompt, kan du vælge den fra proceslinjens indstillingsside i sektionen Personalisering i Indstillingerne, jeg foreslår at gøre det nu:

Mens vi er ved det, skal vi også aktivere skjulte mapper og tilføje filtypenavne til kendte filtyper i Windows File Explorer.

Åbn Windows File Explorer med Windows-tasten + e, og vælg derefter Vis fra båndet. Klik derefter på knappen Indstillinger yderst til højre, dette åbner dialogboksen Mappeindstillinger, herfra vil vi vælge fanen Vis og vælge Radio-knappen til skjulte filer og mapper med 'Vis skjulte filer, mapper og drev', fjern markeringen af ​​indstillingen for 'Skjul udvidelser til kendte filtyper' også. Klik på Anvend og Ok.

Årsagen til, at vi gør dette, er, så vi kan se .gitmappen i projektstrukturer, den er også nødvendig for filer som .envfiler, der bruges til miljøkonfiguration.

Brug Windows-tasten + x, dette er det samme som at højreklikke på Windows-ikonet på skrivebordet, dette åbner hurtiglinkmenuen. Herfra skal du vælge indstillingen Windows PowerShell (Admin), du kan gøre dette ved at trykke på a på tastaturet. Så Windows-nøgle + x derefter a, åbner prompt for brugerkontokontrol (Admin), forudsat at du har administratorrettigheder på din maskine, skal du klikke på ja for at fortsætte.

Kopier koden her og indsæt den i PowerShell-vinduet, Ctrl + v fungerer i PowerShell, tryk på enter for at udføre kommandoen.

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Du bliver bedt om at genstarte din maskine efter dette, hvilket du skal gøre.

Tjek linket for en komplet liste over Windows-tastaturgenveje.

Efter genstart kan du åbne Ubuntu-programmet fra startmenuen, og den anden installation (af WSL på dit system) skal ske. Vent til dette er afsluttet, så bliver du bedt om at oprette en bruger og en adgangskode til kontoen. Du bliver nødt til at huske den adgangskode, der er oprettet til brugeren, da du bliver bedt om at bruge sudoprivilegier.

opdater, opgrader og fjern automatisk

På tidspunktet for skrivning af dette er den version, jeg har linket til Ubuntu, 18.04.1

Den linkede app installerer den seneste stabile Ubuntu-udgivelse på Windows.

Du kan kontrollere, hvilken version af Ubuntu du har installeret med:

lsb_release -a

Hvis du vil bruge en bestemt LTS-version af Ubuntu, kan du få dem fra Windows Store her:

  • Ubuntu 16.04 LTS
  • Ubuntu 18.04 LTS

Nu skal vi fortsætte en opdatering og opgradere alle tingene, de tre kommandoer her opdaterer og opgraderer al den forudinstallerede software, der følger med Ubuntu-installationen.

Koden nedenfor er tre kommandoer, der er forbundet sammen med &&. Den -yflaget er at auto acceptere ændringerne, der vil blive udført med opgraderinger. Automatisk fjernelse fjerner ubrugte pakker, der er tilbage fra opgraderinger.

Kopier nedenstående kommandoer til din Ubuntu-terminal, og kør kommandoen:

sudo apt update && sudo apt -y upgrade && sudo apt autoremove
For at gå fra 16.04 til 18.04, prøv do-release-upgradei terminalen. advares, det tager betydeligt længere tid end bare at slette den nuværende installation af Ubuntu og starte igen.

Nu hvor basen er installeret og opdateret, skal vi installere den build-essentielle pakke for at kompilere og opbygge andre pakker, de følgende pakker, som vi skal installere, har alle brug for det.

sudo apt install -y build-essential

Herfra og videre foreslår jeg at bruge to bash-vinduer, den ene med sudo aktiveret den anden med standardtilladelser.

dvs. åbne en terminal og gå ikke ind sudoder, brug i stedet den terminal, der har fået den tilladelse.

Årsagen til dette er, at jeg har fundet ud af, at hvis du installerer node som sudo, skal du hver gang du vil køre en npm installkommando give sudo-tilladelser, og det kan alle blive lidt kedeligt. Og du burde virkelig ikke installere npm-pakker med sudo-tilladelser.

Installer node (med n)

Let’s get our run-time on! If you use Node.js you will eventually have situations where you need to be able to switch Node versions, for this you may have heard to nvm (Node Version Manager) which by the way you can still use in WSL.

The reason for me opting for n over nvm is that in the past I experienced slow bash startup times whilst using nvm. Take a look at this WSL GitHub issue detailing it and the specific comment for the pros of using n.

Ok let’s install n, with n-install, the command doesn’t start with sudo so use it in the bash window you have with no sudo privileges.?

curl -L //git.io/n-install | bash

This will install the latest version of Node for us, follow the prompt on the terminal screen to reload bash:

# mine looks like this. /home/scott/.bashrc

Now check the versions of Node and npm we have installed with node -v && npm -v in the terminal.

Install Visual Studio Code

Install VSCode? What? This post is to set up WSL? Ok, so we’re going to presume that this is going from nothing to being able to web dev, so we’re going to need a text editor, and there’s nothing out there at the moment that comes close to VSCode, if you already have a text editor installed then skip to the next part.

Install the Windows version of VSCode from the Download section once installed we can enable WSL in the settings, the quickest way to do this is to open the integrated terminal in VSCode with the shortcut keys Ctrl+’ ? that’s and apostrophe. You will be prompted to select your default terminal, select WSL Bash.

Install Windows Git

There’s a bit of stumbling block getting Git support for VSCode, it’s well documented in the various issues mentioned in the linked issue.

There are workarounds as well with things like WSLGit which has its own set of issues, this all stems from users (me included) not wanting to have to install another binary for Git.

I have tried several variants when it comes to using Git with VSCode and the path of least resistance was biting the bullet and installing that additional binary, there’s an overhead with having to maintain and configure SSH keys for both Windows and WSL Git but it’s a one time setup.

Until the VSCode team incorporate WSLGit into VSCode I think this is the best option.

Install from git-scm.com the link will start downloading the install binary you can then open it and go through the install, you can keep clicking next through the install, I have selected a few options that I would like enabled.

For now, that is all we need to do, when it comes to authenticating with with GitHub using SSH we will use the Git Bash command line to configure the Windows side of things.

Install a Terminal (Hyper)

Now we have bash on Windows it’s time to install a nice Terminal app, because, let’s face it, the standard one is a bit basic.

Enter Hyper an electron based terminal app that is super themeable and configurable.

Download and install hyper for Windows, this will be the bare-bones version, it’ll look something like this:

You may notice that, this is the Windows Command prompt too — don’t worry, we’re going to configure that right now.

Open up the .hyper.js file located in the root of your user folder, or from Hyper itself use the shortcut key Ctrl+, to pop open the settings.

If the settings file (.hyper.js) opens in Notepad then you can set the default to be VSCode. In File Explorer (Use Windows key+e to open it) navigate to the file, it'll be in your User folder, right-click the file and select Properties, then Change for 'Opens with:' and select VSCode from the list.

Now we can set WSL as the shell path in Hyper, open the .hyper.jssettings file and search (Ctrl+f) for bash, we want to add the path to the WSL shell into the shell property defined in the file.

// amend shell path// WSL Bashshell: 'C:\\Windows\\System32\\bash.exe',

We can also change the appearance of Hyper from here by specifying font size and font family and also predefined themes, let’s quickly add in the hyper-adventure-time theme into the plugins section.

Open another Hyper tab with Ctrl+Shift+t, this will show the bash terminal for WSL now.

Quick orientation with the Hyper terminal keyboard shortcuts:

  • New tab = Ctrl+Shift+t
  • Close current tab = Ctrl+Shift+w
  • Cycle through tabs right = Ctrl+Shift+right arrow
  • Cycle through tabs left = Ctrl+Shift+left arrow

Now I’m going to configure some additional properties for Hyper, and change the theme to something a bit more subtle.

I purchased Dank Mono, if you want a similar font as OSS check out FiraCode.

Here’s what I changed:

module.exports = { config: { fontSize: 18, fontFamily: '"Dank Mono",...', cursorShape: 'BEAM', cursorBlink: true, copyOnSelect: true, plugins: ['hyper-altair'] }}

Install Fish Shell!

Ok, time to install the Fish Shell! This is a completely optional step, I like to use fish for the nice intellisense you get when navigating file structures, there are also some neat themes you can get with Oh My Fish

sudo apt -y install fish

Install Oh My Fish

Now we can install Oh My Fish (OMF) and get a nice terminal theme going on too, remember we talked about running commands with the correct permissions? Well this is one of those occasions, open a new tab first then paste in the following:

curl -L //get.oh-my.fish | fish

Fish themes with OMF

After installing OMF you can pick a theme, here you may notice that the text doesn’t fit in the screen on Hyper, a quick way to reset it is to maximise the window then back again, I did this with the Windows key+arrow up to maximise the Windows key+arrow down to restore it.

After installing omf I chose the theme agnoster, installed with omf install agnoster you can list out what is available and what you have already installed by selecting omf theme let's change it once more to the one theme, adjust the size of the window first as things are getting a bit cramped.

Have a play around there’s loads there, I prefer the one theme because you can see what version of node you’re running on, over on the far right. Here you can also see the intellisense for fish where it shows agnoster as a previous selection, if I wanted to select agnoster I could tab to complete the rest of the command.

Configure

So, now we have a swanky looking new terminal and a bare-bones VSCode install it’s time to get things moving.

I have a cheat-sheets repo detailing a lot of the configuration we’re about to go through here, if you find something useful along the way please drop a PR with the suggestion.

First up, in place of typing out fish each time I open a new terminal, and not replacing bash which you can do, but I prefer to use an alias. So what we're going to do is open up the .bashrc file and edit it.

From a new terminal type in nano ~/.bashrc, nano is a bash text editor. Scroll (or Page) down to the bottom of the file and add in an alias for fish, alias f=fish then to exit nano Ctrl+x and confirm changes with a y. Reload the .bashrc file like we did after using n to install Node . /home/scott/.bashrc but your username in place of mine ?, now we can use f instead of fish! Well done you saved yourself typing three extra characters!!!

Git config

Before we crack on with spinning up a Create React App instance we’re going to need to configure Git, via the .gitconfig file.

We’re going to use nano to add some settings for Git, both in WSL and Windows, this is the extra config I mentioned earlier, apologies!

First up we’ll config our bash environment and then move onto Git bash on Windows.

# open up the .gitconfig file in WSL bash# nano will create one if it doesn't existnano ~/.gitconfig

It’s nearly identical config for both environments apart from Windows uses the credential manager helper and WSL will need the cache helper.

# needed in Git Bash for Windows[credential] helper = manager# needed in WSL[credential] helper = cache[color] ui = true
[user] name = yourGitHubUserName email = [email protected]
[core] editor = code --wait

Make your changes then Ctrl+x to exit and confirm with y, that’s the first part of the Git config done.

In Git Bash for Windows do the same in the terminal, use nano to create a .gitconfig file and add the setting to it.

Create React App

Ok, we shall get create react app going so we can get Git configured with GitHub using SSH and start making changes to commit to a GitHub repo.

We’re going to use npx to make a starter React project with Create React App. If you want to have your projects somewhere else now is the time to navigate to that directory.

From the terminal enter:

# this will create a starter react project# called create-react-appnpx create-react-app create-react-app

This is creating the project in my home (~) folder, I've had to speed the gif up to 20x faster, it does take a while.?

Wheeeeeeeeeeeeeeeeeeeee!

So, now that we have spun up a React app to work on we should make a change and then add it to GitHub.

Navigate to the create-react-app folder in the terminal and open the project. You can open VSCode with the code . command, the . denotes the current folder in the terminal.

This will open VSCode at the root of the create-react-app project, make sure everything is working as expected by running the start script in npm.

npm run start

If you want to see what scripts are available in the current project you can also use npm run which will list defined npm commands from the package.json file.

Der åbnes et vindue, der viser den grundlæggende landingsside for Create React App, vi tilføjer en h1 til App.jsmodulet og forpligter den via Source Control-menuen i VSCode.

Vær opmærksom på, at dette er en igangværende guide til et udviklingsmiljø, jeg vil ikke beskrive udviklingen i React

Mens terminalen er optaget af at starte oprette-reager-siden, kan vi åbne en anden fane i Hyper med Ctrl + Shift + t og åbne VSCode med code .kommandoen, navigere til App.jskomponenten og tilføje et <h1> -tag til det lige ovenståendee the t; tag med

Hej Reager!

i det.

Now the landing page should show the newly added h1, we can confirm that the file is changed in Git by doing a git status from the terminal. Then we can commit it from either the terminal or the IDE (VSCode) my preference is to use VSCode as the visual diffing of files is great.

Commit the changes with Ctrl+Enter if you have not selected the file you want to commit you will get a dialogue telling you there are no staged changes to commit, you can select the files (in this case only the App.jsfile) by clicking the plus next to the file. Hitting Ctrl+Enter again will commit the changes with a message, you can check that there are no changes to commit with git status from the terminal or visually from the Source Control section in VSCode.

Ok, time to add our super sweet code changes to a repository on GitHub for the world to see!

Next up we’re going to go through adding the local changes we’ve made to a GitHub repository. If you haven’t got a GitHub account and are following along now may be a good time to set one up.

Use SSH with GitHub

First up, we’re going to make a repository on GitHub and push the changes we have made to it, here we’re going to click the + button in the top right of the GitHub home page, enter a repository name and GitHub will give you some defaults to choose from. As we have already made change locally we can ignore the first several commands but we do need the command to add the remote:

git remote add origin [email protected]:spences10/cra.git

And to push the changes up to GitHub git push -u origin master but we're going to get an error, as we haven't set up SSH.

WSL SSH Config

Åbn en ny bash-fane i Hyper, og indtast, ls -al ~/.sshkontroller at der ikke er nogen rsafiler, før du gør dette.

Du vil bemærke, at vi gør dette i bash snarere end fisk.

Hvis der ikke er noget der, skal du generere nye nøgler med:

# add your email address ?ssh-keygen -t rsa -b 4096 -C [email protected] 

Nu ls -al ~/.sshvises to ekstra filer:

# there should be a private and public key-pairid_rsaid_rsa.pub

Start SSH-agenten:

eval "$(ssh-agent -s)"

Tilføj RSA-nøgle til SSH med:

ssh-add ~/.ssh/id_rsa

Nu er det tid til at tilføje den offentlige SSH-nøgle til GitHub, i WSL skal vi kopiere den offentlige SSH-nøgle med cat ~/.ssh/id_rsa.pubi Hyper. Så kan vi kopiere fra Hyper til en ny SSH-nøgle.

Endelig godkend med GitHub:

ssh -T [email protected]

Windows Git Bash SSH Config

For Windows Git Bash skal vi kopiere vores SSH-nøgler, som vi lige har genereret i WSL over til Windows Git Bash, der er nogle subtile forskelle, når vi godkender med Windows Git Bash.

Fra Hyper skal du kopiere SSH-filerne fra Linux til Windows:

cp ~/.ssh/* /c/Users/spenc/.ssh/

Start SSH-agenten:

eval `ssh-agent -s`

Tilføj nøglen til SSH-agent:

ssh-add ~/.ssh/id_rsa

Godkend:

ssh -T [email protected]

Now the additional set up for SSH is done we should be able to go back to Hyper and push our change to GitHub.

Also, you may have noticed that the SSH Key ? token went from black to green on the settings screen there this indicates that you have authenticated.

Change WSL version

If you want to use a different version of Ubuntu, Debian or any of the other available distributions of Linux from the Windows store with Hyper you will need to change the default version from PowerShell:

# list available versionswslconfig /l# set defaultwslconfig /setdefault Debian

Wrap up!

Det er det! Vi er gået fra bunden til et velfungerende webudviklingsmiljø. Der er flere andre personlige ting, jeg nu vil tilføje til min installation nu, der er aliasser for både Git og Fish shell. Hvis du er interesseret i dem, har jeg et snydeark til fisk med en liste over alle mine aliaser, jeg bruger, det samme til mit, .gitconfigdu kan finde det i min dotfiles repo

Tak for læsningen ?

Dette blev oprindeligt indsendt på min Gatsby-blog, du kan se det her, se venligst på mit andet indhold, hvis du nød dette.

Følg mig på Twitter eller spørg mig om noget på GitHub.

Du kan læse andre artikler som denne på min blog.