问题
I have developed my first Node.js pp. For now, it just sits on my laptop.
During development I had to install some modules:
npm install socket.io
npm install mysql@2.0.0-alpha3
npm install iniparser
npm install js-yaml
npm install nodemailer
I have installed all of them "per-project", not globally.
The directory of my project looks like this (my code is all in push_server.js):
|
|--- push_server.js
|
|--- node_modules
|--- iniparser
|--- js-yaml
|--- mysql
|--- socket.io
|--- nodemailer
Now I want to push this project to the production server.
My question is: can I upload the whole codebase (including installed modules) or should I upload just the code of my app and re-install the modules one-by-one on the server?
Note: my dev machine runs Ubuntu 10.04, the production server runs CentOS 5.3
I think all those modules are made up of js files only, therefore it should be allright.
However, is it possible that a module installed by npm compiles some code on the local machine, therefore that code is likely not to work on another machine. Also, how can I know whether a module does that?
Hope the question is not too silly - I have just started with Node.js.
Thanks.
回答1:
I always try to use the latest versions of the required modules. Node.js is still very young and whenever a module is updated it often brings bugfixes, speed improvements, etc. I install modules locally, test my code on them and finally use them in production. When you install them with npm install module --save
they are automatically added to your package.json
file as dependencies
.
Using git
for version control and deployment I usually put the node_modules
folder into my .gitignore
file. It is therefore not uploaded to my server. On the server you just have to run npm install
to install all your required modules, which is much faster than installing them one by one.
This workflow is also used by heroku.
回答2:
You should definitely copy node_modules
if possible.
If you re-download them at the server, you run a risk of getting slightly different versions of the modules in use. Even if you require a strict module version X, that module can again have wildcard dependencies to other modules Y Z. This means that if Y is updated and you publish to the server, it will now run using different code for Y than what you used for your local testing and validation.
In some cases you may not even have a required module available at deployment time, because the author has decided to de-publish the package for whatever reason.
来源:https://stackoverflow.com/questions/12955597/node-js-do-i-need-to-reinstall-all-the-modules-on-the-production-server-when-d