상세 컨텐츠

본문 제목

React App Tutorial For Mac

카테고리 없음

by carstaurasul1984 2021. 3. 11. 17:25

본문

Create React App. Set up a modern web app by running one command. Less to Learn. You don't need to learn and configure many build tools. Instant reloads help you focus on development. When it's time to deploy, your bundles are optimized automatically. Only One Dependency. I am trying to set up react app using create-react-app command on windows pc. I already used it on my mac computer, and it works well. But I encounter a problem. Here my steps on command line. Feb 13, 2020  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 to ensure that npx always uses the latest version. (npx comes with npm 5.2+. It's not an issue with Create React App—it seems like you can't run any global Node commands. (Mac OS X) 0. Create-react-app yields 'the request of a dependency is an expression' in express/lib/view.js. Create-react-app not working. Zsh: command not found: create-react-app on ubuntu 17.04. Jul 09, 2020  Install React Native (and its dependencies) The first thing we need to do is to install React Native framework on our system, together with all its required dependencies to build and run Android apps. Unfortunately, we cannot build and run iOS apps, as they do require a MacOS (or, to better say, a OSX machine). This is more a Apple cruel restriction than a React Native limitation, though: the. Jun 02, 2019  $ npx create-react-app client $ cd client $ yarn start If the application opened this screen that means you’ve done the things right. The default port is the number 3000, but we’ve already set.

-->

In this tutorial, you'll learn how to containerize a .NET Core application with Docker. Containers have many features and benefits, such as being an immutable infrastructure, providing a portable architecture, and enabling scalability. The image can be used to create containers for your local development environment, private cloud, or public cloud.

In this tutorial, you:

  • Create and publish a simple .NET Core app
  • Create and configure a Dockerfile for .NET Core
  • Build a Docker image
  • Create and run a Docker container

You'll understand the Docker container build and deploy tasks for a .NET Core application. The Docker platform uses the Docker engine to quickly build and package apps as Docker images. These images are written in the Dockerfile format to be deployed and run in a layered container.

Note

This tutorial is not for ASP.NET Core apps. If you're using ASP.NET Core, see the Learn how to containerize an ASP.NET Core application tutorial.

Prerequisites

Install the following prerequisites:

  • .NET Core 3.1 SDK
    If you have .NET Core installed, use the dotnet --info command to determine which SDK you're using.
  • A temporary working folder for the Dockerfile and .NET Core example app. In this tutorial, the name docker-working is used as the working folder.

Create .NET Core app

You need a .NET Core app that the Docker container will run. Open your terminal, create a working folder if you haven't already, and enter it. In the working folder, run the following command to create a new project in a subdirectory named app:

Your folder tree will look like the following:

The dotnet new command creates a new folder named App and generates a 'Hello World' console application. Change directories and navigate into the App folder, from your terminal session. Use the dotnet run command to start the app. The application will run, and print Hello World! below the command:

The default template creates an app that prints to the terminal and then immediately terminates. For this tutorial, you'll use an app that loops indefinitely. Open the Program.cs file in a text editor.

Tip

If you're using Visual Studio Code, from the previous terminal session type the following command:

This will open the App folder that contains the project in Visual Studio Code.

The Program.cs should look like the following C# code:

Replace the file with the following code that counts numbers every second:

Save the file and test the program again with dotnet run. Remember that this app runs indefinitely. Use the cancel command Ctrl+C to stop it. The following is an example output:

If you pass a number on the command line to the app, it will only count up to that amount and then exit. Try it with dotnet run -- 5 to count to five.

Important

Any parameters after -- are not passed to the dotnet run command and instead are passed to your application.

Publish .NET Core app

Before adding the .NET Core app to the Docker image, first it must be published. It is best to have the container run the published version of the app. To publish the app, run the following command:

This command compiles your app to the publish folder. The path to the publish folder from the working folder should be .AppbinReleasenetcoreapp3.1publish

From the App folder, get a directory listing of the publish folder to verify that the NetCore.Docker.dll file was created.

Use the ls command to get a directory listing and verify that the NetCore.Docker.dll file was created.

Create the Dockerfile

The Dockerfile file is used by the docker build command to create a container image. This file is a text file named Dockerfile that doesn't have an extension.

Create a file named Dockerfile in directory containing the .csproj and open it in a text editor. This tutorial will use the ASP.NET Core runtime image (which contains the .NET Core runtime image) and corresponds with the .NET Core console application.

App

Note

The ASP.NET Core runtime image is used intentionally here, although the mcr.microsoft.com/dotnet/core/runtime:3.1 image could have been used.

Mac

The FROM keyword requires a fully qualified Docker container image name. The Microsoft Container Registry (MCR, mcr.microsoft.com) is a syndicate of Docker Hub - which hosts publicly accessible containers. The dotnet/core segment is the container repository, where as the aspnet segment is the container image name. The image is tagged with 3.1, which is used for versioning. Thus, mcr.microsoft.com/dotnet/core/aspnet:3.1 is the .NET Core 3.1 runtime. Make sure that you pull the runtime version that matches the runtime targeted by your SDK. For example, the app created in the previous section used the .NET Core 3.1 SDK and the base image referred to in the Dockerfile is tagged with 3.1.

Save the Dockerfile file. The directory structure of the working folder should look like the following. Some of the deeper-level files and folders have been omitted to save space in the article:

From your terminal, run the following command:

Docker will process each line in the Dockerfile. The . in the docker build command tells Docker to use the current folder to find a Dockerfile. This command builds the image and creates a local repository named counter-image that points to that image. After this command finishes, run docker images to see a list of images installed:

Notice that the two images share the same IMAGE ID value. The value is the same between both images because the only command in the Dockerfile was to base the new image on an existing image. Let's add three commands to the Dockerfile. Each command creates a new image layer with the final command representing the counter-image repository entry points to.

The COPY command tells Docker to copy the specified folder on your computer to a folder in the container. In this example, the publish folder is copied to a folder named App in the container.

The WORKDIR command changes the current directory inside of the container to App.

The next command, ENTRYPOINT, tells Docker to configure the container to run as an executable. When the container starts, the ENTRYPOINT command runs. When this command ends, the container will automatically stop.

From your terminal, run docker build -t counter-image -f Dockerfile . and when that command finishes, run docker images.

Each command in the Dockerfile generated a layer and created an IMAGE ID. The final IMAGE ID (yours will be different) is cd11c3df9b19 and next you'll create a container based on this image.

Create a container

Now that you have an image that contains your app, you can create a container. You can create a container in two ways. First, create a new container that is stopped.

The docker create command from above will create a container based on the counter-image image. The output of that command shows you the CONTAINER ID (yours will be different) of the created container. To see a list of all containers, use the docker ps -a command:

Manage the container

The container was created with a specific name core-counter, this name is used to manage the container. The following example uses the docker start command to start the container, and then uses the docker ps command to only show containers that are running:

Similarly, the docker stop command will stop the container. The following example uses the docker stop command to stop the container, and then uses the docker ps command to show that no containers are running:

Connect to a container

After a container is running, you can connect to it to see the output. Use the docker start and docker attach commands to start the container and peek at the output stream. In this example, the Ctrl+C keystroke is used to detach from the running container. This keystroke will end the process in the container unless otherwise specified, which would stop the container. The --sig-proxy=false parameter ensures that Ctrl+C will not stop the process in the container.

After you detach from the container, reattach to verify that it's still running and counting.

Delete a container

For the purposes of this article you don't want containers just hanging around doing nothing. Delete the container you previously created. If the container is running, stop it.

The following example lists all containers. It then uses the docker rm command to delete the container, and then checks a second time for any running containers.

Single run

Docker provides the docker run command to create and run the container as a single command. This command eliminates the need to run docker create and then docker start. You can also set this command to automatically delete the container when the container stops. For example, use docker run -it --rm to do two things, first, automatically use the current terminal to connect to the container, and then when the container finishes, remove it:

The container also passes parameters into the execution of the .NET Core app. To instruct the .NET Core app to count only to 3 pass in 3.

With docker run -it, the Ctrl+C command will stop process that is running in the container, which in turn, stops the container. Since the --rm parameter was provided, the container is automatically deleted when the process is stopped. Verify that it doesn't exist:

Change the ENTRYPOINT

The docker run command also lets you modify the ENTRYPOINT command from the Dockerfile and run something else, but only for that container. For example, use the following command to run bash or cmd.exe. Edit the command as necessary.

In this example, ENTRYPOINT is changed to cmd.exe. Ctrl+C is pressed to end the process and stop the container.

In this example, ENTRYPOINT is changed to bash. The exit command is run which ends the process and stop the container.

Essential commands

Docker has many different commands that create, manage, and interact with containers and images. These Docker commands are essential to managing your containers:

Clean up resources

During this tutorial, you created containers and images. If you want, delete these resources. Use the following commands to

  1. List all containers

  2. Stop containers that are running by their name.

  3. Delete the container

Next, delete any images that you no longer want on your machine. Delete the image created by your Dockerfile and then delete the .NET Core image the Dockerfile was based on. You can use the IMAGE ID or the REPOSITORY:TAG formatted string.

App

Use the docker images command to see a list of images installed.

Tip

Image files can be large. Typically, you would remove temporary containers you created while testing and developing your app. You usually keep the base images with the runtime installed if you plan on building other images based on that runtime.

Next steps

-->

Visual Studio allows you to easily create a Node.js project and experience IntelliSense and other built-in features that support Node.js. In this tutorial for Visual Studio, you create a Node.js web application project from a Visual Studio template. Then, you create a simple app using React.

In this tutorial, you learn how to:

  • Create a Node.js project
  • Add npm packages
  • Add React code to your app
  • Transpile JSX
  • Attach the debugger

Before you begin

Here's a quick FAQ to introduce you to some key concepts.

What is Node.js?

Node.js is a server-side JavaScript runtime environment that executes JavaScript server-side.

What is npm?

npm is the default package manager for the Node.js. The package manager makes it easier for programmers to publish and share source code of Node.js libraries and is designed to simplify installation, updating, and uninstallation of libraries.

What is React?

React is a front-end framework to create a UI.

What is JSX?

JSX is a JavaScript syntax extension, typically used with React to describe UI elements. JSX code must be transpiled to plain JavaScript before it can run in a browser.

What is webpack?

webpack bundles JavaScript files so they can run in a browser. It can also transform or package other resources and assets. It is often used to specify a compiler, such as Babel or TypeScript, to transpile JSX or TypeScript code to plain JavaScript.

Prerequisites

  • You must have Visual Studio installed and the Node.js development workload.

    If you haven't already installed Visual Studio 2019, go to the Visual Studio downloads page to install it for free.

    If you haven't already installed Visual Studio 2017, go to the Visual Studio downloads page to install it for free.

    If you need to install the workload but already have Visual Studio, go to Tools > Get Tools and Features..., which opens the Visual Studio Installer. Choose the Node.js development workload, then choose Modify.

  • You must have the Node.js runtime installed.

    This tutorial was tested with version 12.6.2.

    If you don't have it installed, we recommend you install the LTS version from the Node.js website for best compatibility with outside frameworks and libraries. Node.js is built for 32-bit and 64-bit architectures. The Node.js tools in Visual Studio, included in the Node.js workload, support both versions. Only one is required and the Node.js installer only supports one being installed at a time.

    In general, Visual Studio automatically detects the installed Node.js runtime. If it does not detect an installed runtime, you can configure your project to reference the installed runtime in the properties page (after you create a project, right-click the project node, choose Properties, and set the Node.exe path). You can use a global installation of Node.js or you can specify the path to a local interpreter in each of your Node.js projects.

Create a project

First, create a Node.js web application project.

  1. Open Visual Studio.

  2. Create a new project.

    Press Esc to close the start window. Type Ctrl + Q to open the search box, type Node.js, then choose Blank Node.js Web Application - JavaScript. (Although this tutorial uses the TypeScript compiler, the steps require that you start with the JavaScript template.)

    In the dialog box that appears, choose Create.

    From the top menu bar, choose File > New > Project. In the left pane of the New Project dialog box, expand JavaScript, then choose Node.js. In the middle pane, choose Blank Node.js Web Application, type the name NodejsWebAppBlank, then choose OK.

    If you don't see the Blank Node.js Web Application project template, you must add the Node.js development workload. For detailed instructions, see the Prerequisites.

    Visual Studio creates the new solution and opens your project.

    (1) Highlighted in bold is your project, using the name you gave in the New Project dialog box. In the file system, this project is represented by a .njsproj file in your project folder. You can set properties and environment variables associated with the project by right-clicking the project and choosing Properties. You can do round-tripping with other development tools, because the project file does not make custom changes to the Node.js project source.

    (2) At the top level is a solution, which by default has the same name as your project. A solution, represented by a .sln file on disk, is a container for one or more related projects.

    (3) The npm node shows any installed npm packages. You can right-click the npm node to search for and install npm packages using a dialog box or install and update packages using the settings in package.json and right-click options in the npm node.

    (4) package.json is a file used by npm to manage package dependencies and package versions for locally-installed packages. For more information, see Manage npm packages.

    (5) Project files such as server.js show up under the project node. server.js is the project startup file and that is why it shows up in bold. You can set the startup file by right-clicking a file in the project and selecting Set as Node.js startup file.

Add npm packages

This app requires a number of npm modules to run correctly.

  • react
  • react-dom
  • express
  • path
  • ts-loader
  • typescript
  • webpack
  • webpack-cli
  1. In Solution Explorer (right pane), right-click the npm node in the project and choose Install New npm Packages.

    In the Install New npm Packages dialog box, you can choose to install the most current package version or specify a version. If you choose to install the current version of these packages, but run into unexpected errors later, you may want to install the exact package versions described later in these steps.

  2. In the Install New npm Packages dialog box, search for the react package, and select Install Package to install it.

    Select the Output window to see progress on installing the package (select Npm in the Show output from field). When installed, the package appears under the npm node.

    The project's package.json file is updated with the new package information including the package version.

  3. Instead of using the UI to search for and add the rest of the packages one at a time, paste the following code into package.json. To do this, add a dependencies section with this code:

    If there is already a dependencies section in your version of the blank template, just replace it with the preceding JSON code. For more information on use of this file, see package.json configuration.

  4. Save the changes.

  5. Right-click npm node in your project and choose Install npm Packages.

    This command runs the npm install command directly.

    In the lower pane, select the Output window to see progress on installing the packages. Installation may take a few minutes and you may not see results immediately. To see the output, make sure that you select Npm in the Show output from field in the Output window.

    Here are the npm modules as they appear in Solution Explorer after they are installed.

    Note

    If you prefer to install npm packages using the command line, right-click the project node and choose Open Command Prompt Here. Use standard Node.js commands to install packages.

Add project files

In these steps, you add four new files to your project.

  • app.tsx
  • webpack-config.js
  • index.html
  • tsconfig.json

For this simple app, you add the new project files in the project root. (In most apps, you typically add the files to subfolders and adjust relative path references accordingly.)

  1. In Solution Explorer, right-click the project NodejsWebAppBlank and choose Add > New Item.

  2. In the Add New Item dialog box, choose TypeScript JSX file, type the name app.tsx, and select Add or OK.

  3. Repeat these steps to add webpack-config.js. Instead of a TypeScript JSX file, choose JavaScript file.

  4. Repeat the same steps to add index.html to the project. Instead of a JavaScript file, choose HTML file.

  5. Repeat the same steps to add tsconfig.json to the project. Instead of a JavaScript file, choose TypeScript JSON Configuration file.

Add app code

  1. Open server.js and replace the existing code with the following code:

    The preceding code uses Express to start Node.js as your web application server. This code sets the port to the port number configured in the project properties (by default, the port is configured to 1337 in the properties). To open the project properties, right-click the project in Solution Explorer and choose Properties.

  2. Open app.tsx and add the following code:

    The preceding code uses JSX syntax and React to display a simple message.

  3. Open index.html and replace the body section with the following code:

    This HTML page loads app-bundle.js, which contains the JSX and React code transpiled to plain JavaScript. Currently, app-bundle.js is an empty file. In the next section, you configure options to transpile the code.

Configure webpack and TypeScript compiler options

In the previous steps, you added webpack-config.js to the project. Next, you add webpack configuration code. You will add a simple webpack configuration that specifies an input file (app.tsx) and an output file (app-bundle.js) for bundling and transpiling JSX to plain JavaScript. For transpiling, you also configure some TypeScript compiler options. This code is a basic configuration that is intended as an introduction to webpack and the TypeScript compiler.

  1. In Solution Explorer, open webpack-config.js and add the following code.

    The webpack configuration code instructs webpack to use the TypeScript loader to transpile the JSX.

  2. Open tsconfig.json and replace the default code with the following code, which specifies the TypeScript compiler options:

    app.tsx is specified as the source file.

Transpile the JSX

  1. In Solution Explorer, right-click the project node and choose Open Command Prompt Here.

  2. In the command prompt, type the following command:

    node_modules.binwebpack app.tsx --config webpack-config.js

    The command prompt window shows the result.

    If you see any errors instead of the preceding output, you must resolve them before your app will work. If your npm package versions are different than the versions shown in this tutorial, that can be a source of errors. One way to fix errors is to use the exact versions shown in the earlier steps. Also, if one or more of these package versions has been deprecated and results in an error, you may need to install a more recent version to fix errors. For information on using package.json to control npm package versions, see package.json configuration.

  3. In Solution Explorer, right-click the project node and choose Add > Existing Folder, then choose the dist folder and choose Select Folder.

    Visual Studio adds the dist folder to the project, which contains app-bundle.js and app-bundle.js.map.

  4. Open app-bundle.js to see the transpiled JavaScript code.

  5. If prompted to reload externally modified files, select Yes to All.

Each time you make changes to app.tsx, you must rerun the webpack command. To automate this step, add a build script to transpile the JSX.

Add a build script to transpile the JSX

Starting in Visual Studio 2019, a build script is required. Instead of transpiling JSX at the command line (as shown in the preceding section), you can transpile JSX when building from Visual Studio.

  • Open package.json and add the following section after the dependencies section:

Run the app

React App Tutorial For Mac Pro

  1. Select either Web Server (Google Chrome) or Web Server (Microsoft Edge) as the current debug target.

    If Chrome is available on your machine, but does not show up as an option, choose Browse With from the debug target dropdown list, and select Chrome as the default browser target (choose Set as Default).

  2. To run the app, press F5 (Debug > Start Debugging) or the green arrow button.

    A Node.js console window opens that shows the port on which the debugger is listening.

    Visual Studio starts the app by launching the startup file, server.js.

  3. Close the browser window.

  4. Close the console window.

Set a breakpoint and run the app

  1. In server.js, click in the gutter to the left of the staticPath declaration to set a breakpoint:

    Breakpoints are the most basic and essential feature of reliable debugging. A breakpoint indicates where Visual Studio should suspend your running code so you can take a look at the values of variables, or the behavior of memory, or whether or not a branch of code is getting run.

  2. To run the app, press F5 (Debug > Start Debugging).

    The debugger pauses at the breakpoint you set (the current statement is marked in yellow). Now, you can inspect your app state by hovering over variables that are currently in scope, using debugger windows like the Locals and Watch windows.

  3. Press F5 to continue the app.

  4. If you want to use the Chrome Developer Tools or F12 Tools for Microsoft Edge, press F12. You can use these tools to examine the DOM and interact with the app using the JavaScript Console.

  5. Close the web browser and the console.

Set and hit a breakpoint in the client-side React code

React Tutorial Pdf

In the preceding section, you attached the debugger to server-side Node.js code. To attach the debugger from Visual Studio and hit breakpoints in client-side React code, the debugger needs help to identify the correct process. Here is one way to enable this.

Prepare the browser for debugging

For this scenario, use either Microsoft Edge (Chromium), currently named Microsoft Edge Beta in the IDE, or Chrome.

  1. Close all windows for the target browser.

    Other browser instances can prevent the browser from opening with debugging enabled. (Browser extensions may be running and preventing full debug mode, so you may need to open Task Manager to find unexpected instances of Chrome.)

    For Microsoft Edge (Chromium), also shut down all instances of Chrome. Because both browsers share the chromium code base, this gives the best results.

  2. Start your browser with debugging enabled.

    Starting in Visual Studio 2019, you can set the --remote-debugging-port=9222 flag at browser launch by selecting Browse With... > from the Debug toolbar, then choosing Add, and then setting the flag in the Arguments field. Use a different friendly name for the browser such as Edge with Debugging or Chrome with Debugging. For details, see the Release Notes.

    Alternatively, open the Run command from the Windows Start button (right-click and choose Run), and enter the following command:

    msedge --remote-debugging-port=9222

    or,

    chrome.exe --remote-debugging-port=9222

    Open the Run command from the Windows Start button (right-click and choose Run), and enter the following command:

    chrome.exe --remote-debugging-port=9222

    This starts your browser with debugging enabled.

    The app is not yet running, so you get an empty browser page.

Attach the debugger to client-side script

React Tutorial Pdf Download Free

  1. Switch to Visual Studio and then set a breakpoint in your source code, either app-bundle.js or app.tsx.

    For app-bundle.js, set the breakpoint in the render() function as shown in the following illustration:

    To find the render() function in the transpiled app-bundle.js file, use Ctrl+F (Edit > Find and Replace > Quick Find).

    For app.tsx, set the breakpoint inside the render() function, on the return statement.

  2. If you are setting the breakpoint in the .tsx file (rather than app-bundle.js), you need to update webpack-config.js. Replace the following code:

    with this code:

    This is a development-only setting to enable debugging in Visual Studio. This setting allows you to override the generated references in the source map file, app-bundle.js.map, when building the app. By default, webpack references in the source map file include the webpack:/// prefix, which prevents Visual Studio from finding the source file, app.tsx. Specifically, when you make this change, the reference to the source file, app.tsx, gets changed from webpack:///./app.tsx to ./app.tsx, which enables debugging.

  3. Select your target browser as the debug target in Visual Studio, then press Ctrl+F5 (Debug > Start Without Debugging) to run the app in the browser.

    If you created a browser configuration with a friendly name, choose that as your debug target.

    The app opens in a new browser tab.

  4. Choose Debug > Attach to Process.

    Tip

    Starting in Visual Studio 2017, once you attach to the process the first time by following these steps, you can quickly reattach to the same process by choosing Debug > Reattach to Process.

  5. In the Attach to Process dialog box, get a filtered list of browser instances that you can attach to.

    In Visual Studio 2019, choose the correct debugger for your target browser, JavaScript (Chrome) or JavaScript (Microsoft Edge - Chromium) in the Attach to field, type chrome or edge in the filter box to filter the search results.

    In Visual Studio 2017, choose Webkit code in the Attach to field, type chrome in the filter box to filter the search results.

  6. Select the browser process with the correct host port (localhost in this example), and select Attach.

    The port (1337) may also appear in the Title field to help you select the correct browser instance.

    The following example shows how this looks for the Microsoft Edge (Chromium) browser.

    You know the debugger has attached correctly when the DOM Explorer and the JavaScript Console open in Visual Studio. These debugging tools are similar to Chrome Developer Tools and F12 Tools for Microsoft Edge.

    Tip

    If the debugger does not attach and you see the message 'Unable to attach to the process. An operation is not legal in the current state.', use the Task Manager to close all instances of the target browser before starting the browser in debugging mode. Browser extensions may be running and preventing full debug mode.

  7. Because the code with the breakpoint already executed, refresh your browser page to hit the breakpoint.

    While paused in the debugger, you can examine your app state by hovering over variables and using debugger windows. You can advance the debugger by stepping through code (F5, F10, and F11). For more information on basic debugging features, see First look at the debugger.

    You may hit the breakpoint in either app-bundle.js or its mapped location in app.tsx, depending on which steps you followed previously, along with your environment and browser state. Either way, you can step through code and examine variables.

    • If you need to break into code in app.tsx and are unable to do it, use Attach to Process as described in the previous steps to attach the debugger. Make sure you that your environment is set up correctly:

      • You closed all browser instances, including Chrome extensions (using the Task Manager), so that you can run the browser in debug mode. Make sure you start the browser in debug mode.

      • Make sure that your source map file includes a reference to ./app.tsx and not webpack:///./app.tsx, which prevents the Visual Studio debugger from locating app.tsx.Alternatively, if you need to break into code in app.tsx and are unable to do it, try using the debugger; statement in app.tsx, or set breakpoints in the Chrome Developer Tools (or F12 Tools for Microsoft Edge) instead.

    • If you need to break into code in app-bundle.js and are unable to do it, remove the source map file, app-bundle.js.map.

React Full Tutorial

Next steps