1

I have a file server.js in which I am starting multiple servers. eg.

var engine = require('engine.io');
var server = engine.listen(80);

And if the engineio module is not installed in the current directory by using the

npm install engine.io

It throws an error

Error: Cannot find module 'engine.io'

Now on a machine where this server has to be deployed I want handle all these npm module installation dependencies from the script file itself.

How can i do this from my server.js file only

UPDATE

As suggested i tried following the post
Can I install a NPM package from javascript running in Node.js?

but it throws an error when it searches for a node module "npm" at :

var npm = require("npm");

Reason : i dont have the node modules installed on my machine. ( and so its not having node_modules folder in my working directory)

Also,

I created a batch file in my working directory and put the following into it

npm install engine.io
npm install eyes

And when i run it,

It only installs the first module and then exits

Thanks

Community
  • 1
  • 1
ankur
  • 557
  • 1
  • 10
  • 37
  • You can use `npm` programatically (see [here](http://stackoverflow.com/a/15957574/444991)), but it's *much* more common (and recommended) to just invoke `npm install` on the server (often with the help of `npm shrinkwrap`), *or* to simply include the `node_modules` folder in your deployment bundle (depending on how you deploy, you can sometimes do this without having to include `node_modules` in your VCS). Note, you could also use the `process` module, and use `exec()` to literally call `npm install` from within your `server.js` file. – Matt Mar 13 '15 at 09:29
  • While this is possible, perhaps this task is best handled outside of the application - the target is to make sure that whatever is starting your application (init script? systemd unit? Something custom?) will run `npm install` just before that. Using *npm* programmatically is really useful only when you do not know beforehand which packages you are going to need. – Robert Rossmann Mar 13 '15 at 09:36
  • okay so i tried to run the code from a script file `var npm = require("npm");` it also says that "Since npm is in the node_modules folder, you can use require('npm') like any other module" but i dont even have the node_modules folder. so the above command when put in a node script and run ... throws the error `Error: Cannot find module 'npm'` – ankur Mar 13 '15 at 11:33
  • @RobertRossmann : okay i get the idea what you are talking about... and my case is not like that "I am not aware of what modules will be required" so i agree " the target is to make sure that whatever is starting your application (init script? systemd unit? Something custom?) will run npm install just before that" **how do i do that** -- from a script or something – ankur Mar 13 '15 at 11:37
  • Well, just run `npm install` from the project's root after it is deployed on target machine. This depends on where you are deploying your app, how you are running it etc. You have not told us anything about that.:) – Robert Rossmann Mar 13 '15 at 11:39
  • the idea is that i dont want to run any command (npm install) on the target machine where the project is deployed. The target machine has node installed in it and set it as an Environment Variable. **All i want** is to start my server from a **single batch file** on the target machine – ankur Mar 13 '15 at 11:46
  • @Matt Could you please have a look now – ankur Mar 13 '15 at 12:10
  • If you do not run `npm install` you will be unable to start the server. There is nothing you can do. Another option would be to run `npm install` via [child_process](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback), but unless your application is completely standalone (no dependencies), you **must run `npm install`**. – Robert Rossmann Mar 13 '15 at 13:23
  • when i run npm install from the current directory it throws an error `Couldnt read dependencies`.....also please note that i do not have a package.json file in current directory – ankur Mar 13 '15 at 19:30
  • @RobertRossmann I dont think i am able to explain the problem correctly. If i manually execute `npm install engine.io` and `npm install eyes` etc.... and then from a batch file start the server.js script(please look at my question) everithing works perfectly.. my servers are started ... **now** to distribute this server script all i want is the users to start **just a batch file** (node js is already installed and PATH variable is set)... i dont want to tell the client teams to go to the directory and execute `npm install engine.io` – ankur Mar 13 '15 at 19:37
  • Then distribute the application with *node_modules* folder. Or, distribute it with a shell script that runs both commands. – Robert Rossmann Mar 13 '15 at 19:42
  • @RobertRossmann : okay so if i distribute my project with the node_modules folder that has the npm module in it.... and later on , there would be a time when node versions would get updated on the client machines. So my question is that the older npm version wouldnt cause any issue ? – ankur Mar 13 '15 at 19:58

0 Answers0