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
How To Claim 150,000 Shiba Inu Token To Your Trust Wallet (Airdrop)
How to Hide Security Cameras: Best Outdoor & Indoor Ideas - Reolink Blog
Compare Foods Wilson Nc
Fat Hog Prices Today
Kansas City Kansas Public Schools Educational Audiology Externship in Kansas City, KS for KCK public Schools
Restaurer Triple Vitrage
Wisconsin Women's Volleyball Team Leaked Pictures
From Algeria to Uzbekistan-These Are the Top Baby Names Around the World
Encore Atlanta Cheer Competition
MADRID BALANZA, MªJ., y VIZCAÍNO SÁNCHEZ, J., 2008, "Collares de época bizantina procedentes de la necrópolis oriental de Carthago Spartaria", Verdolay, nº10, p.173-196.
Lantana Blocc Compton Crips
Industry Talk: Im Gespräch mit den Machern von Magicseaweed
Gmail Psu
Huge Boobs Images
London Ups Store
Puretalkusa.com/Amac
Winco Employee Handbook 2022
Encyclopaedia Metallum - WikiMili, The Best Wikipedia Reader
Panola County Busted Newspaper
Everything To Know About N Scale Model Trains - My Hobby Models
Mandy Rose - WWE News, Rumors, & Updates
Powerschool Mcvsd
What Is a Yurt Tent?
R/Airforcerecruits
Farm Equipment Innovations
Usa Massage Reviews
Infinite Campus Asd20
Mississippi Craigslist
Insidious 5 Showtimes Near Cinemark Southland Center And Xd
Franklin Villafuerte Osorio
Kaiserhrconnect
Frommer's Belgium, Holland and Luxembourg (Frommer's Complete Guides) - PDF Free Download
Nsu Occupational Therapy Prerequisites
Royals op zondag - "Een advertentie voor Center Parcs" of wat moeten we denken van de laatste video van prinses Kate?
Metro 72 Hour Extension 2022
Jennifer Reimold Ex Husband Scott Porter
Google Chrome-webbrowser
Merkantilismus – Staatslexikon
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
Cocorahs South Dakota
Frigidaire Fdsh450Laf Installation Manual
Gotrax Scooter Error Code E2
Atu Bookstore Ozark
Stosh's Kolaches Photos
Vagicaine Walgreens
3500 Orchard Place
Joblink Maine
Aloha Kitchen Florence Menu
Theater X Orange Heights Florida
Osrs Vorkath Combat Achievements
Download Twitter Video (X), Photo, GIF - Twitter Downloader
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.