1

As a prototype, I’ve written a simple Node.js command-line script which loads a .txt file and outputs the contents. I’ve read many articles which suggest that Node.js scripts are usually executed via the command-line and should be installed globally, so that’s the direction I’ve taken, even if there are other techniques.

For the sake of this query, my globally installed command is called my-prototype.

This all works fine on the desktop but I intend to host this prototype on a server, using Express.

How do I globally install my-prototype on the server and, if that’s possible, will I be able to execute it in the same way as in my local tests, as a child process?:

const { execSync } = require('child_process')
const output = execSync(`${process.execPath} my-prototype`)
console.log(output.toString())

I'm not entirely sure that globally installing command-line scripts is for use on a server and wonder whether that's for desktop use only.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
MoO
  • 125
  • 1
  • 1
  • 9
  • Why do you want to host that on an express server? Like what exactly do you want to achieve with that? – EAzevedo Oct 08 '21 at 10:15
  • From what I think I understand from your question, you are looking into creating a node package. – EAzevedo Oct 08 '21 at 10:16
  • You can check “how to create a npm package with node” on Google. – EAzevedo Oct 08 '21 at 10:17
  • https://stackoverflow.com/questions/28440893/install-a-locally-developed-npm-package-globally huh? – Vadim Oct 08 '21 at 10:19
  • Hi @EAzevedo, thanks for replying. With regards to using Express, let’s just say that I wish to host it on a ‘server’ (whatever that may be), so that it’s available as a service for me to use while on the move. If not Express, what would you alternatively suggest? I’m familiar with creating Node packages but I’m not familiar with installing them globally on a server. I’m questioning how to do this and whether, if I achieve this, my example code above would function correctly, as it currently does on my desktop. – MoO Oct 08 '21 at 11:25
  • Hi @Vadim, thank you for the link. While perfectly explaining how to globally install a module, this doesn't help me in my quest to do so on a server, unless I'm missing something. – MoO Oct 08 '21 at 11:29
  • I do not understand what you want to achieve, you described the solution you are looking for, not the problem – Vadim Oct 08 '21 at 11:30
  • So you want to make it available online so you can install it? Like store it somewhere and install it whenever you need? – EAzevedo Oct 08 '21 at 12:21
  • Hi @Vadim, I'm trying to stick to what I've learnt is best, that being the use of **global command-line** for Node.js. However, I'm concerned that this may be for desktop use only as I haven't yet found a way to use this strategy on a _server_. I'm looking to call a web-service/function (if that's the best terminology), which reads files from the server, by way of command-line Node.js, and then return that content after various manipulation (beyond my current simple prototype). **What you've provided in your own post below is getting closer to describing what I'm struggling to convey.** – MoO Oct 08 '21 at 13:37
  • 1
    Hi @EAzevedo, no, that's not what I'm trying to do but thank you for highlighting how my original query has not been clear enough. Let me explain... If you can picture, during development on your desktop, we run `npm i`, which installs all of our package dependencies. I want to do the same thing but on the server, so that my server-side code can use the installed modules. Only, in my case, I'm looking to install my module globally and target it directly, using `node my-prototype`, rather than `require('my-prototype')`. – MoO Oct 08 '21 at 13:49
  • So you want, in your server command line, run node my-prototype? Or where do you want to run that command? If you want to use your my-prototype module in your express server code, you will need to require it or import it. – EAzevedo Oct 08 '21 at 14:01
  • what might also be possible, is that you manually install the script globally on your server (but you will need access to do it), then you can create a wrapper class in your express server to spawn a process that uses that global command. – EAzevedo Oct 08 '21 at 14:09
  • Hi @EAzevedo, so unless I have access to the server, there is no way for me to install **global** modules. Is this the same for `npm install`ing **non-global** module dependencies, listed in `package.json`? I hope not, as that would stop me creating any of my own modules at all, even for requiring/importing them. It would also stop me using any third-party modules (unless they're built-in; e.g. `fs`, `path`, `process`, etc.) – MoO Oct 08 '21 at 16:28
  • I believe you could achieve that by deploying a node script that installs your package globally. – EAzevedo Oct 09 '21 at 10:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237978/discussion-between-eazevedo-and-moo). – EAzevedo Oct 09 '21 at 10:20

1 Answers1

0

If you need just output the contents, then you do not need a console, or rather: you only need it once, to run nodejs

const fs = require('fs');

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

//

app.get('/', async(req, res)=>{
    let fileContains = fs.readFileSync('./package.json', 'utf8');
    try{
        fileContains = JSON.parse(fileContains);
    }catch(e){}
    res.json(fileContains);
});

//

app.listen(80);

Also, read a little more about the pm2 module, it's very useful

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Vadim
  • 306
  • 1
  • 5
  • 13
  • Hi Vadim, although this isn’t exactly what I was looking for, it’s **extremely** helpful in describing what I’d like to achieve - thank you! Rather than calling `fs.readFileSync()`, I’d like to call my own **global command**; e.g. `let fileContains = execSync('node my-prototype')`, as I expect `my-prototype` to do a lot more than just read a file later on. – MoO Oct 08 '21 at 13:27
  • Hi Vadim, please also see my message to EAzevedo on the OP, which better explains how I wish to install and use my module. Vadim's comment highlighted where my OP may have confused. I'm apologise, I'm very confused myself and quite new to some of Node.js' implementation. – MoO Oct 08 '21 at 13:53