Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Manuela Cardenas
Manuela Cardenas
1,575 Points

Run react practice project in editor

When working on a practice project in my text editor, what command do I need to run in my terminal to view the project in the browser?

8 Answers

Adam Beer
Adam Beer
11,314 Points

You solved this problem? Because I found one way how can u solve this. You can use Babel and Webpack. Check below my file structure.

- public
  - - css
    - - - app.css
  - - img
    - - - ..images
  - - js
  - - index.html
- src
  - - index.js
- .babelrc
- package.json
- server.js
- webpack.config.js

.babelrc

{
    "presets": [
        "react", 
        "es2015"
    ]
}

package.json

{
  "name": "react",
  "version": "1.0.0",
  "description": "helper",
  "main": "server.js",
  "dependencies": {
    "css-loader": "^1.0.1",
    "express": "^4.16.4",
    "nodemon": "^1.18.5",
    "react": "^16.4.2",
    "react-dom": "^16.4.2",
    "style-loader": "^0.23.1"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start-dev": "nodemon server.js",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.0.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^4.23.1",
    "webpack-cli": "^3.1.2"
  }
}

server.js

const express = require("express");
const app = express();
const port = 5000;

app.use(express.static(__dirname + '/public'));

app.listen(port, () => 
    console.log(`Server started on port ${port}`)
);

webpack.config.js

module.exports = {
    entry: './src/index.js',
    output: {
        path: __dirname + '/public/js',
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                use: [ 'style-loader', 'css-loader' ]
            }
        ]
    }
}

index.js

import React from 'react';
import ReactDOM from 'react-dom';

const planets = [
  {
    id: '1',
    name: 'Mercury',
    diameter: '3,031.67 mi',
    moons: 'none',
    desc: 'Mercury is the closest planet to the Sun. Due to its proximity, it\'s not easily seen except during twilight. For every two orbits of the Sun, Mercury completes three rotations about its axis. Up until 1965 it was thought that the same side of Mercury constantly faced the Sun.',
    url: 'img/mercury.jpg' 
  },
  {
    id: '2',
    name: 'Venus',
    diameter: '7,521 mi',
    moons: 'none',
    desc: 'Venus is the second planet from the Sun and is the second brightest object in the night sky after the Moon. Venus is the second largest terrestrial planet and is sometimes referred to as the Earth’s sister planet due the their similar size and mass.',
    url: 'img/venus.jpg' 
  },
  {
    id: '3',
    name: 'Earth',
    diameter: '7,917.5 mi',
    moons: '1',
    desc: 'Earth is the third planet from the Sun and is the largest of the terrestrial planets. The Earth is the only planet in our solar system not to be named after a Greek or Roman deity. The Earth was formed approximately 4.54 billion years ago and is the only known planet to support life.',
    url: 'img/earth.jpg' 
  },
  {
    id: '4',
    name: 'Mars',
    diameter: '4,212 mi',
    moons: '2',
    desc: 'The fourth planet from the Sun and the second smallest planet in the solar system. Mars is often described as the "Red Planet" due to its reddish appearance. It\'s a terrestrial planet with a thin atmosphere composed primarily of carbon dioxide.',
    url: 'img/mars.jpg'
  },
  {
    id: '5',
    name: 'Jupiter',
    diameter: '86,881.4 mi',
    moons: '79',
    desc: 'The planet Jupiter is the fifth planet out from the Sun, and is two and a half times more massive than all the other planets in the solar system combined. It is made primarily of gases and is therefore known as a "gas giant".',
    url: 'img/jupiter.jpg' 
  },
  {
    id: '6',
    name: 'Saturn',
    diameter: '72,367.4 mi',
    moons: '62',
    desc: 'Saturn is the sixth planet from the Sun and the most distant that can be seen with the naked eye. Saturn is the second largest planet and is best known for its fabulous ring system that was first observed in 1610 by the astronomer Galileo Galilei.',
    url: 'img/saturn.jpg'
  },
  {
    id: '7',
    name: 'Uranus',
    diameter: '31,518 mi',
    moons: '27',
    desc: 'Uranus is the seventh planet from the Sun. While being visible to the naked eye, it was not recognised as a planet due to its dimness and slow orbit. Uranus became the first planet discovered with the use of a telescope.',
    url: 'img/uranus.jpg' 
  },
  {
    id: '8',
    name: 'Neptune',
    diameter: '30,599 mi',
    moons: '14',
    desc: 'Neptune is the eighth planet from the Sun making it the most distant in the solar system. This gas giant planet may have formed much closer to the Sun in early solar system history before migrating to its present position.',
    url: 'img/neptune.jpg' 
  },
];

// =============================================================
//   WRITE YOUR CODE BELOW
// =============================================================

// 1: Create a 'Planet' component that renders a planet card


// 2: Create a container component that iterates over the planets array 
//    and renders a 'Planet' component for each object in the array 


// 3: Render the container component to the DOM

So your first step, npm install.

Your second step is, npm run build.

And finally, your third step is, nodemon or node server.js and check your code on localhost:5000.

Hope this help for you!

Reminder: If you build your code with webpack, but the code doesn't good, the building will not succeed. It works only when your code works well

Adam Beer
Adam Beer
11,314 Points

Sorry, I forgot my index.html file

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>The React Planets</title>
    <link rel="stylesheet" href="css/app.css" />
  </head>

  <body>
    <div id="root"></div>

    <script src="js/bundle.js"></script>
  </body>
</html>
Adam Beer
Adam Beer
11,314 Points

You enter into your project folder and write into keyword: npm start

Manuela Cardenas
Manuela Cardenas
1,575 Points

I ran npm install and then npm start, but receive a bunch of errors...

Adam Beer
Adam Beer
11,314 Points

You installed the dependencies what you use in your project? Please show your console response.

Manuela Cardenas
Manuela Cardenas
1,575 Points

I downloaded this zip file for the project, it did not say any dependencies to install in particular which is why I was confused as to how to run the program from the beginning..

Error in console is saying there is not package.json, so did I miss a step?

this is error from console:

npm ERR! path /Users/manuelacardenas/Desktop/Treehouse/practice-react-component-rendering/package.json npm ERR! code ENOENT npm ERR! errno -2 npm ERR! syscall open npm ERR! enoent ENOENT: no such file or directory, open '/Users/manuelacardenas/Desktop/Treehouse/practice-react-component-rendering/package.json' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent

npm ERR! A complete log of this run can be found in: npm ERR! /Users/manuelacardenas/.npm/_logs/2018-10-30T19_43_35_352Z-debug.log

Adam Beer
Adam Beer
11,314 Points

Maybe yes, but I don't think so. Please mark where are you.

Manuela Cardenas
Manuela Cardenas
1,575 Points

I'm sorry can you clarify? What do you mean by mark where I am?

Adam Beer
Adam Beer
11,314 Points

You said: "I downloaded this zip file for the project". So please tell me which course you are doing, and then I can check what's the wrong.

Manuela Cardenas
Manuela Cardenas
1,575 Points

oh, ok thank you.. Im working on the " Practice React Component Rendering "

Adam Beer
Adam Beer
11,314 Points

Yeah, I found it. The problem is, you don't use package.json file, and node_modules because you didn't need it. So open your index.html file with your browser, and that is all :) I think now your file is working.

Adam Beer
Adam Beer
11,314 Points

Are you beginner in React? If you say, "Yes, I am", then please check this link: Learn-React. Hope this help to you :)

Manuela Cardenas
Manuela Cardenas
1,575 Points

That was the first thing I tried... I did inspect the page this time though and received this error:

You might need to use a local HTTP server (instead of file://): https://fb.me/react-devtools-faq babel.min.js:24 You are using the in-browser Babel transformer. Be sure to precompile your scripts for production - https://babeljs.io/docs/setup/ u @ babel.min.js:24 babel.min.js:24 Failed to load file:///Users/manuelacardenas/Desktop/Treehouse/practice-react-component-rendering/start/app.js: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

Adam Beer
Adam Beer
11,314 Points

I understand. I'm not home, if I go home tomorrow I check your problem with my IDE. But if you can solve it then write here :)