NodeJS close server and start again - node.js

I am brand new to React and NodeJS. I have NodeJS latest version installed on my mac as well as React. I can start and run the app using npm start I am following tutorials and it was fine and I could see my app in the browser. The problem begins when I need to finish for the day and start again the next day and I do not know how to start the server again and app and to continue the work. npm start does not work. Unfortunately tutorials only show how to start the app for the very first time, but they don't show what do you need to do to interrupt your work, shut down computer and continue the following day. What steps do I need to take in to continue my work the following day?

regarding your question the things you want to make sure that whatever project you are working on (nodejs or react) that you cd into the project folder before you run any commands as these scripts you are running "npm run start" etc.. are based on what scripts are written inside the package.json.
so if you use create-react-app for reactjs the "npm run start" is a default script that comes with CRA and will run the app for you.
for express you can also check the scripts and there will be something similar but you can do "node server.js" (or app.js depends on how you called the file you initialize the server) and it will run the server.
in summary:
make sure to cd into the correct directory
check package.json for scripts if you are not sure what they are
also run "npm install" as you might have some missing dependencies
enjoy.
If you have any more questions will edit my response to answer those as well, have a nice day ;)

Just look for error messages in the Terminal when you run npm start. Try to understand them and fix them.
You can also install Nodemon (npm i nodemon) and run nodemon {filename}.js to start the server. {filename} is the name of the file in which you're starting the server.

Related

Node.js is causing error: Missing script: "start"?

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.
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.
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

reactjs app not run after serve and start

I just installed node.js on my centos 7 server, then install react
it successfully created my new react app, after that, I run:
npm start
and or serve -s build
but none of these run in my browser.
This site can’t be reached
how can I solve this?
If you have sever inside can you try with start debugging of your application
Stop npm, if you're using yarn
Start the project with command above, inside your project home folder
yarn start
Take some time to learn commands below:

NestJS server dies when console is closed?

I created folder for project, copy package.json and run npm install, build locally nestjs project and copy dist into server. Then ran in console node dist/main.js. For testing I used a base NestJS project (nest new ...) which only return "Hello word" on 3000 port.
Server work fine, but after close of console will stop.
I think problem on VPS (Ubuntu) settings or may be need to add some parameters to NestJS.
Why it not work constantly?
Thanks in advance for any advice.
I resolved problem with npm forever - https://www.npmjs.com/package/forever
install it globally - npm install forever -g
then got to project dir and run - forever start dist/main.js
Seems all work fine.

Nodemon and babel-node restart multiple times

I'm using nodemon with babel-node in development environment.
I'm working on Windows 7 and every time I change my code, nodemon restart multiple times.
I searched on google and even in stackoverflow and github, the temporary solution is just: re-install nodemon: npm i -g nodemon. But after some minutes, the problem come back :(
I tried add --delay sometime and even re-install node.js but it does not work
How can I fix that? This problem slow down my work :(
if you mean restart by this
then here is your answer
we use nodemon for this work
nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected. nodemon does not require any additional changes to your code or method of development.
have a look at nodemon docs
if not then
look at this answer

sailsjs live update during serverside editing

It's annoying to have to restart the sails server when you change something, is there any way to make sailsjs do what meteor does where when you save a serverside file it automatically updates the clientside?
That's a pretty awesome feature, and I love sails but that feature is pretty cool.
Nodemon is a helpful development tool that watches the files in the directory that it was started in, and if anything changes are detected, your node.js application will automatically restart.
To install nodemon (you may need to use sudo)
$ npm install -g nodemon
Sails.js continually writes to the .tmp folder, and as a result you will find that nodemon will continually restart the server. To resolve this issue, simply ignore this folder by creating a .nodemonignore file with this single line, noting you can place any other files/folders you wish to ignore on separate lines
.tmp/*
To run your Sails.js application through nodemon
$ nodemon app
For more information, be sure to check out nodemon on npmjs.org
If you monitor nodemon --ignore 'tmp/*' --ext js,ejs . you will still get the infinite reload problem. Apparently Sails is constantly writing the ejs files.

Resources