'serve' is not recognized as an internal or external command to run react app
Asked Answered
N

2

8

serve has been installed globally using npm install -g serve command and It works locally, but deploying to a Windows server gives the following error:

"serve" is not recognized as an internal or external command

How to fix this error? Also, what is the use of the server.js file in a react project, and how does it help with deployment?

npm serve is installed globally please click here to see the image enter image description here

Navar answered 23/4, 2019 at 16:49 Comment(0)
P
15

I know that running npx serve -s build should work. I had the same problem as you. The npx command works for me. If you have npx problems, check your version of nodejs. I'm running 10.16.2 (so that we're on the same page). https://www.npmjs.com/package/serve

The rest of your question is relative to the rest of your set up. I don't have a server.js file of my own (there are some node_module server.js files, is that what you mean)?

As I understand a create-react-app, npm run start will allow you to run your application locally. I don't need serve -s build for that.

I used an amplify create react app. For an amplify application, I just run amplify publish and my application's build directory is sent to an S3 bucket for me. If you don't have that configuration, and you want the quick and dirty answer... just take the contents of your build directory in your react application and drop those files on your web server. That should get you 90% of the way there (mind the default page that renders).

Post answered 13/8, 2019 at 12:41 Comment(2)
why do npx installs every time I run npx serve -s build command?Burney
that works for me also with npx, may be because the app is created with npx commandHinch
W
0

Serving React Files

Basic Exapmle:-

const express = require('express');
const path = require('path');
const app = express();

app.use(express.static(path.join(__dirname, 'build')));

app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

app.listen(9000);

For your base path in the domain serve the index.html you fetched from your build process.

If you need more info :- https://create-react-app.dev/docs/deployment

Weig answered 13/8, 2019 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.