0

I just started studying front-end development and I'm struggling with a node.js error. Typing 'npm start' in my VSCode terminal used to work fine for simple tutorial projects with just an index.html, script.js, and style.css file. (without a package.json file)

However after trying out React for the first time, 'npm start' now doesn't work anymore in my other non-React projects. At first it was giving me an error that it was missing the package.json (which it didn't need before?) but after trying to fix it with help of googling I now got to a point where it's giving me the error: Missing script: "start".

How can I run node without creating package.json files for every small tutorial project I've made previously, or without turning them into React apps? Also why is this happening? Did installing React-native create dependencies of some sort?

Thanks in advance!

I already tried reinstalling node.js and tried different versions. Also tried deleting package-lock.json. It still works for React apps, just not with simpler native javascript apps.

2 Answers 2

1

A package.json file is required if you want to install any packages or run scripts in your terminal. In your package.json file, make sure you have added scripts property. This is an example of how you can use it:

{
  ...
  "scripts": {
    "start": "react-scripts start"
  }
}

Remove ... from the snippet if you're copying, this has been added to indicate that there are one or more fields in this JSON file.

After you have added this to your package file, you will be able to run the start script by typing npm run start in the terminal or if you use Yarn: yarn start.

Edit: You said that running npm start in your React project is running fine, but on your simpler projects with only a simple HTML, CSS and JS file is not working when using the script.

You are probably confusing npm start with node file.js. Where node file.js doesn't require a package to be in your project to run a JavaScript file, using npm start requires you to have a JSON file present in your project folder with the JSON code as in my answer.

So long story short: Using npm start requires package.json with the script property available. While node file.js doesn't require you to have this file in your project.

Sign up to request clarification or add additional context in comments.

5 Comments

Most of my simple tutorial projects on native javascript don't have a package.json file and it used to run fine without. Any idea on how to fix it without adding the json file?
That is the difference between a simple project and a project written with React. Your simple project probably doesn't have any packages that it's depending on, while React is depending on dozens of other dependencies to run. Therefore you cannot start a project in React without having the package.json file in your project folder. :)
'npm start' is actually working fine in my React projects. It just stopped working in my simpler projects which don't have any dependencies. That's why I'm a little confused haha.
@EddieEdwin I have edited my answer, maybe you'll be less confused after :)
Ah you are right I think I used npm start so many times while working on React that I mixed them up lol. I just figured out I was using live-server for simpler non-React apps. Thanks!
0

if you are using react-native you can do the following

  • First you have to build your project with the command

    npx react-native run-android , npx react-native run-ios

  • Once your project has build successfully and app is installed on your device then you your development server is started already. for some reason if your server is closed then you can run it with the command given below.

  • adb reverse tcp:8081 tcp:8081 this will send a signal to your device and after this run npx react-native start

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.