Using compiled node dependencies on different environments - node.js

I have a node project that I run locally and deploy to a CentOS server. I have a few dependencies, one of which relies on a compiled dependency.
Using npm, I can compile the dependency locally using npm install, and it'll work great. This however is not compiled for the deployment server, so it will break if it is uploaded.
I can compile the dependency on a CentOS box, and it'll work on the server, but it'll break in the local environment.
Can anyone think of a workaround so that I can force node to use the global dependencies in the local environment, and a the local ones on the server?
Thanks!
Update:
I've figured out a way to do this for now, it's not elegant, but it works:
run npm install on my local environment
rename the node_modules folder to node_modules_local
run npm install on the server
prepend a variable to all require() paths
set a local environment variable to "development"
check if we're in development mode, if we are, set the require path variable to ./node_modules_local.

Check in the source code of the dependencies (compiled dependencies and js-only-dependencies). When you deploy your app run 'npm rebuild' on the server. Seems like this is the officially recommended way to go. See http://www.mikealrogers.com/posts/nodemodules-in-git.html for details

Related

Using local NPM dependencies in the PATH - is there a tool that does this?

Is there an NPM package/tool that can (automatically) add local NPM packages to the $PATH?
This would represent a local development env that was independent of other projects.
NVM allows us to switch Node.js versions, but that doesn't seem to be enough to create an independent development space for each project. By By putting the locally installed command line tools on the $PATH, and giving precedence to local NPM dependencies, this would allow for us to change their versions without affecting any other project.
NPX does this, which is bundled with NPM:
https://medium.com/#maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b
However, NPX looks like it does far too much.
I just wanted a simple tool that only adds the local executables to the path, if you are within an NPM project, so I wrote GMX:
https://github.com/ORESoftware/gmx

How to set global node modules so that all application can use same

I am new to node, i have made few small application using node, but everytime i have to use npm install for every application which download the required dependencies in node_modules folder. There are many libraries which are common.
I tried installing using npm install express -g but i was not sure how to use this dependency in other application which is in some other folder.
Is there any way i can have only one folder like in D:\Users\User\AppData\Roaming\npm\node_modules from where my all applications can have the module which they need ?
Can anyone let me know how to do the settings for the same ?
Any help would be highly appreciated !!
Every node application that has a package.json has a specific set of rules for using specific versions of it's modules. You can install globally only one version of a specific module, but if you happen to have an application that needs an older / newer version that is not installed globally on your dev environment, then it will fail to work.
The recommended way of using node modules ( packages ) is to have a local directory inside your project, which contains all libraries that the project needs. This practice is everywhere and so you should follow it.
There are some ways to mitigate the slow npm install, though.
There is a new npm-replacement, created and maintained by Facebook, called yarn.
What yarn does is it creates a local cache of all installed packages and then symlinks them to your project folder from your local computer cache. This way the npm install procedure becomes very fast.

How to do a manual install of Node js package dependencies

I am rather new to Node but am working on a project behind a very restrictive firewall so I cannot use npm to install packages (no proxy either). I am trying to use express and have been able to include it by just storing the files locally and requiring the local file path but I do not know how to structure the project so that node can find and include all of the dependencies for express (which I also have manually downloaded from github and stored locally within the project). Does anyone know how to do a completely manual global or local install of individual node dependency packages?
Simply download the files and place them in a 'node_modules' folder in the root of your app. Then use 'npm init' to create a package.json. Add your dependencies in the dependencies list in that file.
Then run simply 'npm install'.

Can/Should gulp livereload be installed globablly

I am new to Node and I don't fully understand yet what installing locally means exactly. I know I need to install gulp both globally as well as locally but in my case I have a number of projects in separate folders under a development folder and I wonder if I really need to install the livereload extension locally in each separate project folder (which is what the documentation seems to suggest) would it not be easier to install it globally; or locally in the main development folder. Can someone explain how this works and what options I'd have.
Similarly I wonder whether if I install gulp locally in the development folder will this be available to each of it's children or whether I'd again need/want to install it in each project folder locally.
Here is an, albiet old, article on the node js blog that goes over locally vs globally. http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation
The basics are that if you want to require require('gulp-livereload') inside of your code it should be a local (dev) dependency. If you wish to interact with the module via the cli then install globally. For example you might have both gulp and nodemon installed globally.
The point of local install is that someone can bring down your project, type 'npm install' and get all of the dependencies local to that app.
So to answer your question install gulp-livereload locally. But other modules such as gulp, nodemon, etc you would have both global and local.
As an extra help if you want to install gulp as a project dependency but have it globally as well you can run 'npm link gulp' in order to keep them in sync.
Also, you can have just one global version while, using the local one, you can use for a specific project the specific version used while developing it.

Skip local installation of Gulp

Can I somehow skip local installation of Gulp to the project?
I installed Gulp globally, added it to package.json as a dependency. But still Gulp wants to be installed locally also with the Local gulp not found in ... message. If I do local install, Gulp is copied into my project by node.
Is there a way to skip local installation of Gulp? I want to be able to run it across the whole server from command line and manage it installation globally.
One clean way to do this is without have it locally is to install gulp globally like as you did and then run in your directory the command :
npm link gulp
It will create a symbolic link in your node_modules folder to your global gulp install. You need to be careful on the versions since all your symlinks and therefore your local project(s) will use the global one, no matter of the defined version in the package.json, which could cause errors on a deployed environment.
This is also applicable for every node package, and allow you to update the local install you've created that way of multiple projects at once.

Resources