How to create a React app without npm (2024)

React is a JavaScript library and technically it does not require any setup or installation. You can write React code direct in an HTML document, open HTML in a web browser, and it will work. Node Package Manager (npm) is needed for React development to automate the packaging and testing of React apps. Think of it as a tool to help speed up the development.

In this article and code sample, we will create a simple HTML file and write our React code in the HTML and run it by opening the HTML file in a Web browser, without any external library or tool. We did not want to go through the hassle of npm or anything else.

Here are the steps required to create and run a React app without npm.

Step 1. Create a simple HTML file

Create a simple HTML file with the following HTML and save it as Index.html in any folder you have direct access to. We will open this html file direct in the browser.

<html>

<head>

<title>Let’sReactwithnpm</title>

</head>

<body>

</body>

</html>

The above HTML file has a head, title, and body, the simplest form of an HTML file.

Step 2. Import React library

To make React work direct in an HTML document, we need to import the React library in HTML. React library is defined in two .js files. The files differ for development and production as you can see below.

The following script imports React library. Copy and paste this code in the <head /> tag of the HTML.

<!–LoadReactLibraries–>

<!–Note:whendeploying,replace“development.js”with“production.min.js”.–>

<scriptsrc=“https://unpkg.com/react@17/umd/react.development.js”crossorigin></script>

<scriptsrc=“https://unpkg.com/react-dom@17/umd/react-dom.development.js”crossorigin></script>

The final HTML document now looks like this:

<html>

<head>

<title>Let’sReactwithnpm</title>

<!–LoadReactLibraries–>

<!–Note:whendeploying,replace“development.js”with“production.min.js”.–>

<scriptsrc=“https://unpkg.com/react@17/umd/react.development.js”crossorigin></script>

<scriptsrc=“https://unpkg.com/react-dom@17/umd/react-dom.development.js”crossorigin></script>

</head>

<body>

</body>

</html>

Step 3. Placeholder for React Component

Once React library is imported, we can use React syntaxes in our code. React uses components to represent UI. Think of a component as a user control that has code to represent visual interfaces and data.

To place a component on a page, we need a placeholder where that component will load. We add a <div /> tag inside the <body /> tag of the page and give it an id = “root”. This is the position where our React component will render.

<body>

<divid=“root”></div>

</body>

Step 4. Create a React Component

As you may already know, the UI in React is created using components. A component in React is declared as a class. Here is a simple component that displays simple text “react without npm..”.

classHelloClassextendsReact.Component

{

render()

{

returnReact.createElement(‘div’,null,‘Reactwithoutnpm’);

}

}

In the above code, React.createElement is react function that creates an element, a <div /> in this case and displays text inside the div.

Step 5. Call React Component

The final step in the process is to call the React component from JavaScript. The following code React.DOM.render() is responsible for rendering a React component. In this code, the first parameter is the component class name. The render method also takes a root element where the component is rendered. In out case, we render component inside the div id = ‘root’.

ReactDOM.render(

React.createElement(HelloClass,null,null),

document.getElementById(‘root’)

);

Step 6. Complete code

Here is the complete HTML document.

<html>

<head>

<title>React’sReact</title>

<!–LoadReact.–>

<!–Note:whendeploying,replace“development.js”with“production.min.js”.–>

<scriptsrc=“https://unpkg.com/react@17/umd/react.development.js”crossorigin></script>

<scriptsrc=“https://unpkg.com/react-dom@17/umd/react-dom.development.js”crossorigin></script>

</head>

<body>

<divid=“root”></div>

<!–ThisisembeddedJavaScript.Youcanevenplacethisinseparate.jsfile–>

<script>

window.onload=function()

{

classHelloClassextendsReact.Component

{

render()

{

returnReact.createElement(‘div’,null,‘Reactwithoutnpm..’);

}

}

ReactDOM.render(

React.createElement(HelloClass,null,null),

document.getElementById(‘root’)

);

};

</script>

</body>

</html>

Step 7. Run React code

To run the above code, create a text file in any text editor such as Notepad or Visual Studio Code, save it as Index.html or other name and open html file in a Web browser.

The output will look like this in the browser.

How to create a React app without npm (1)

Summary

This step by step tutorial with code example showed how to create a simple React application direct in HTML without npm or other tools.

Some related articles to learn more about React
How to install React.js with create-react-app?
What Is React?
How to run and build a React app?
Props in React

How to create a React app without npm (2024)

FAQs

Can we use ReactJS without npm? ›

That's why I began to be interested in ReactJS. However, they don't have a node. js server infrastructure (and that's why AngularJS suits to them, only one browser is sufficient) so it's impossible to run it with something like "npm start". SO !

Is there any other way to create React app? ›

The React team primarily recommends these solutions: If you're learning React or creating a new single-page app, use Create React App. If you're building a server-rendered website with Node.js, try Next.js. If you're building a static content-oriented website, try Gatsby.

Do you need npm to run React? ›

To use React in production, you need npm which is included with Node.js. To get an overview of what React is, you can write React code directly in HTML.

Can I use node js without npm? ›

If you just want to write learning or hobby programs in javascript like a single file that would forget after awhile, it makes sense not to use npm. Yes. The Node installer ships with npm, but the actual download for Node only contains the Node runtime, not npm (which can be downloaded separately).

Can React be used without a Node? ›

Some people mistakenly assume that Node is required in order to use React. It is not. You don't need Node to run a React project.

What is the fastest way to create a React app? ›

Vite is ideal for making client-rendered React projects that run exclusively in the browser. The great thing about Vite is, as its name indicates, it's much faster than virtually every alternative. Where Vite really shines is how quickly it runs in development.

What is the best React app generator? ›

Gatsby is the popular static site generator used in the official ReactJS website. Gatsby was released in 2015 and is free and open-source. It collaborates well with the React ecosystem and uses GraphQL, a rich plugin for 3rd party services and API.

Why does React need npm? ›

ReactJS depends on Node and npm (Node Package Manager) to notify the native side (iOS/Android) of the packages that you need to use in your app; then, it can add all the needed dependencies.

How to run create-react-app? ›

To run your React project, we need to open the command line. Go to the View option in VSCode and then click on Terminal. When we run create-react-app, we just have to run npm start in the app directory to start serving the development server. We will see a new browser opening with localhost:3000.

Do we need to install npm? ›

NPM is extremely useful, but, when you install it, you install it globally. It comes with Node JS, so when you install Node JS, you should have npm installed(type npm -v to see the version and whether npm is installed). "npm init" creates a package.

Do I need to import React to create React app? ›

This means that you no longer need to import the React module explicitly, unless you want to use some of its properties or methods directly (e.g. React. Component). It simplifies the code by removing the need for unnecessary imports. It makes the code more readable by reducing the clutter caused by unnecessary imports.

Can we create react app without JSX? ›

JSX is not a requirement for using React.

Using React without JSX is especially convenient when you don't want to set up compilation in your build environment.

Do I have to run create react app every time? ›

If you are concerned with the size of it, you do not need to run create-react-app every time. You can make a react project yourself quite easily and by doing so you have much more control and understanding of your project. I recommend looking up how to set up a react project from scratch using the MERN stack.

Why do we need npm for React? ›

ReactJS depends on Node and npm (Node Package Manager) to notify the native side (iOS/Android) of the packages that you need to use in your app; then, it can add all the needed dependencies.

Is Node.js required for ReactJS? ›

ReactJS and NodeJS are both JavaScript technologies. But the uses of NodeJS and ReactJS are entirely different. NodeJS is a framework of JavaScript which is mainly used for working with the backend of our application or building the backend using JavaScript, whereas ReactJS is a JavaScript front-end library.

Can we use ReactJS without JSX? ›

JSX is not a requirement for using React.

Using React without JSX is especially convenient when you don't want to set up compilation in your build environment.

Can I use React without a framework? ›

However, you can fully build a new app or site with React, Meta also recommends using a framework. React can revolutionize your perspective on the designs you encounter and the applications you create.

Top Articles
Do We Really Need Web3?
How to Convert a String to Uppercase in SQL
Victor Spizzirri Linkedin
Walgreens Boots Alliance, Inc. (WBA) Stock Price, News, Quote & History - Yahoo Finance
Winston Salem Nc Craigslist
Belle Meade Barbershop | Uncle Classic Barbershop | Nashville Barbers
Affidea ExpressCare - Affidea Ireland
Black Gelato Strain Allbud
Is Csl Plasma Open On 4Th Of July
Nation Hearing Near Me
Lesson 1 Homework 5.5 Answer Key
Indiana Immediate Care.webpay.md
How Many Slices Are In A Large Pizza? | Number Of Pizzas To Order For Your Next Party
Saw X | Rotten Tomatoes
Hartland Liquidation Oconomowoc
Mail.zsthost Change Password
SXSW Film & TV Alumni Releases – July & August 2024
Haunted Mansion Showtimes Near Millstone 14
Convert 2024.33 Usd
Iu Spring Break 2024
CANNABIS ONLINE DISPENSARY Promo Code — $100 Off 2024
Army Oubs
Panolian Batesville Ms Obituaries 2022
Marion City Wide Garage Sale 2023
Mybiglots Net Associates
Uncovering The Mystery Behind Crazyjamjam Fanfix Leaked
Apartments / Housing For Rent near Lake Placid, FL - craigslist
Victory for Belron® company Carglass® Germany and ATU as European Court of Justice defends a fair and level playing field in the automotive aftermarket
Urbfsdreamgirl
208000 Yen To Usd
This Is How We Roll (Remix) - Florida Georgia Line, Jason Derulo, Luke Bryan - NhacCuaTui
Desirulez.tv
M3Gan Showtimes Near Cinemark North Hills And Xd
Great Clips On Alameda
Devotion Showtimes Near Mjr Universal Grand Cinema 16
Ticketmaster Lion King Chicago
State Legislatures Icivics Answer Key
Jail View Sumter
Mixer grinder buying guide: Everything you need to know before choosing between a traditional and bullet mixer grinder
Htb Forums
This 85-year-old mom co-signed her daughter's student loan years ago. Now she fears the lender may take her house
Lcwc 911 Live Incident List Live Status
Gamestop Store Manager Pay
Garland County Mugshots Today
Ups Customer Center Locations
Barber Gym Quantico Hours
Is TinyZone TV Safe?
ESPN's New Standalone Streaming Service Will Be Available Through Disney+ In 2025
Where Is Darla-Jean Stanton Now
The Significance Of The Haitian Revolution Was That It Weegy
683 Job Calls
Room For Easels And Canvas Crossword Clue
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated:

Views: 6810

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.