0

im new to node.js and have struggled to find information about deploying apps.

Say that I have written a small one file node.js app and would like to deploy it onto a prod server. If I deploy the app.js file alone I cant use "node app.js" to run as the app is missing package.json and the node modules. But deploying all these files/folders to the server seems like the wrong way to do it as it would take ages to transfer all the node_modules.

The only resources that I can find online use localhost node server which doesn't really show what I need. I'm using MobaX to deploy to a specific IP if this is useful.

Sorry if this is a stupid question, most of my experience is in front-end so haven't had the opportunity to play around with this and don't have anyone to ask for guidance.

Please don't mark down the question, I will happily edit the question if any feedback on doing so is provided.

JoeXYZ
  • 47
  • 6
  • 1
    I would assume, that you have node installed on the server, right? Then npm should also be installed. So, just transfer the application files (index.js, package.json, etc.) without the node_modules folder. Then run `npm install` inside the root directory of your application on the server. – Geshode Sep 27 '22 at 09:14
  • 1
    You have to deploy your package.json along with main js file. if your production server is like heroku or something similar, it will install your dependency and execute npm run start by itself. – Amit Kumar Sep 27 '22 at 09:20
  • This makes perfect sense @Geshode and Amit Kumar. Thank you both! So I need to deploy app.js package.json and package-lock.json? Also I assume a build.js file is not required as it is a one file app. Just for future reference when would a build file be required? Am I right in thinking that its when there are multiple aspects of the project such as css and other libraries/files – JoeXYZ Sep 27 '22 at 09:37
  • In cases where host lack the possibility to install dependencies, you might need a different approach. See https://stackoverflow.com/questions/50693283/best-practice-for-nodejs-deployment-directly-moving-node-modules-to-server-or – Mikael Feb 24 '23 at 06:09

0 Answers0