r/react • • May 12 '24

Help Wanted Why is Node.js needed?

Hello, I'm learning React and on all the introduction videos I've watched none of them explain why node.js is installed. Can someone please explain to me why most beginner FE React tutorials require me to install node?

0 Upvotes

24 comments sorted by

28

u/EmployeeFinal Hook Based May 12 '24

You technically can run your code directly in the browser. But modern development has a lot of tools to help, and these tools needs to prepare and build the code for browsers. Node is the most popular runtime to run these tools.

4

u/PMK_Phala May 12 '24

you're help is much appreciated Thanks

32

u/Right-Chocolate-5038 May 12 '24

Before node.js the browser (v8 engine) was the only environment javascript could run in, meaning client side only.

With node.js you can run javascript code in the terminal, outside the browser (server side).

0

u/[deleted] May 12 '24

[deleted]

4

u/[deleted] May 12 '24

And to expand on this response, this is what makes the cli command to initialize a new react app possible, pulling packages from the npm repository (Node Package Manager) like the now-deprecated create-react-app repository or vite, etc.

11

u/Krytom May 12 '24

Installing the package manager (npm) is probably one of the main reasons.

1

u/cimmic May 12 '24

Actually, true. Almost all my JS projects are 100% client side but npm is all over my terminal.

8

u/TicketOk7972 May 12 '24

To run the dev server and build the production bundle.

5

u/_nathata Hook Based May 12 '24

Node is necessary for dependency management (npm and related) and build and dev server

17

u/Lumpy_Pin_4679 May 12 '24

I’m curious about something - is Google not a thing anymore? This is something that can easily be searched. Is the preference just to have some random person, who might not have a clue what they’re saying, explain it to you? Why not just search and learn?

8

u/AlliedSnail May 12 '24

Google is useful because of questions like this. And duplicate questions help generate a wealth of information that enriched the topic further. Most people reading here probably learned something new that they never would have googled themselves

-3

u/Lumpy_Pin_4679 May 13 '24

Uhhhh ok? This wasn’t posted for any of those reasons and none of that answers my question so not sure what your point is.

3

u/AlliedSnail May 15 '24

I Googled ‘why is node js needed Reddit’ and this discussion is in the top five results. Do you understand now?

2

u/yeonw May 13 '24

Why you have to be so mean ? Isn't this groups for discussions? If a newbie as a doubt we are supposed to help. With this attitude I don't think it will take you anywhere.

2

u/TheRNGuy May 16 '24

But that wouldn't create discussion.

1

u/Lumpy_Pin_4679 May 17 '24

Hmmmm…you’re right. Ok, thanks!

1

u/LikeWhattttlol May 13 '24

I cant get npm run dev to work can someone help me

1

u/yeonw May 13 '24

Check if your package.json has the dev declared

1

u/[deleted] May 13 '24

1.open your package.json file 2.under scripts, search for something like "dev":"node filename.js"

3.if it don't exist add this line "dev":"node filename.js"

you can replace dev with any other name

now head to terminal and type:

npm run dev or npm run (put here the other name you just wrote inplace of dev for example say it "start" , so your final command will be npm run start)

1

u/LikeWhattttlol May 13 '24

When I get to my laptop I’ll try it thank you

1

u/LikeWhattttlol May 13 '24

The response I’m getting is

[email protected] dev nodefilename.js

sh: nodefile.js: command not found

🤬

1

u/controlIsAnIllusion7 May 13 '24

To be able to run JavaScript outside of the browser.

1

u/TheRNGuy May 16 '24

So you can use JSX, database, fullstack frameworks like Remix, and other stuff that require server.