Setting up build server using Node and Gulp - node.js

I'm developing a front-end package and this is my environment:
IDE: Visual Studio (I'm on Windows and we use TypeScript)
Package manager: NPM
Task runner: Gulp
CI: TeamCity
My problem is that I'm trying to avoid installing node and Gulp globally on my build servers (mainly because I don't want to bother managing this - installing, updating, synchronizing, etc.).
So I found a NuGet package (https://www.nuget.org/packages/Npm.js/) which allowed me to restore all the packages without having node installed on the machine. But then the problem was that the local Gulp I installed assumes there's a node.exe right next to it or in the machine's path:
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\gulp\bin\gulp.js" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\gulp\bin\gulp.js" %*
)
Now obviously this won't work since I don't have node installed on the build machine and there's no local node installation inside node_modules.
So is there any way to avoid installing my build tools globally on each build server? Or were Node and Gulp intended to be installed everywhere? How should then updates be maintained? It's not like a Visual Studio release which comes out once every few months... Or do you just ignore frequent releases of the build tools?
I could also add the NuGet package to the path but that (the path where node.exe is located) depends on the version of the NuGet package and requires more maintenance whenever updating it. So I prefer not going this way either...

It's not optimal but here's what I ended up doing: I used a NuGet package for setting environment variables in MSBuild and created the following build configuration:
<Target Name="GulpBuild">
<SetEnvVariable Name="path" Value="%path%;..\packages\Node.js.5.3.0" />
<Exec Command="node_modules/.bin/gulp" />
</Target>
Now when I build the solution in VS (CTRL+Shift+B) it adds the node location to the path and then calls gulp. Of course I guess I could have just written the gulp command myself including the node path.
In any case, this is of course not optimal because I need to manually update the location if I update the Node.js version, but I couldn't find any better solution.
At least the path configuration is defined inside the repository and does not depend on any machine configuration.

Late here, but I think you can setup your gulp project to run with the gulp installed to node_modules. For example, on npm start you can use the path the binary.
In short, can you change the project to not use the global gulp? It might be work looking int.

Related

NodeJs Plugin Installation not found in Jenkins/configure

I am trying to configure Jenkins to build my code using NodeJS Plugin. I have installed NodeJS plugin but NodeJS Installation are not available in System Configuration.
ManageJenkins -> Configure System -> NodeJS installation (not
available)
I am running Jenkins on localhost.
What can I do to resolve this issue?
Have you installed and followed the instruction mentioned in node.js plugin? It is quite straight forward:
After installing the plugin, go to the global jenkins configuration
panel (JENKINS_HOME/configure or JENKINS_HOME/configureTools if
using jenkins 2), and add new NodeJS installations For every Nodejs
installation, you can choose to install some global npm packages.
Now, go to a job configuration screen, you will have 2 new items :
On the "Build environnment" section, you will be able to pick one of
the NodeJS installations to provide its bin/ folder to the PATH.
This way, during shell build scripts, you will have some npm
executables available to the command line (like bower or grunt)
Go to a job configuration screen, you will have 2 new items : On the
"Build environnment" section, you will be able to pick one of the
NodeJS installations to provide its bin/ folder to the PATH. This
way, during shell build scripts, you will have some npm executables
available to the command line (like bower or grunt)
You have to goto "/pluginManager/advanced" and run "check now" so that it will check the nodejs site and do the global install.
This will solve your problem

Update Node Version in Visual Studio 2017

Does anyone know of a sanctioned way to update Node within Visual Studio 2017? I've got Node 6 set up on my development PC but VS2017 appears to come with a bundled copy of Node 5, which means that running npm install on my command line sets up various packages like node-sass with bindings for Node 6, so when I then run gulp tasks from the VS Task Runner things fall over due to the mismatched versions.
I found the VS Node install at C:\Program Files (x86)\Microsoft Visual Studio 14.0\Web\External but I'm a little reluctant to just hack about and replace it. Anyone got any experience of upgrading this? I know I can sync by downgrading the version on my PC but that seems backwards.
Go to:
Tools > Options > Projects and Solutions > Web Package Management > External Web Tools
Then you need to add your Node install directory to the top of the list, like so:
You don't need to add a new Node install directory, Node exists in PATH.
Notice how $(PATH) is below the internal path $(DevEnvDir)\Extensions\Microsoft\Web Tools\External.
To use the global PATH environment variable before the internal path, you can just use the arrows at the top-right to change the order.
This is what it looks like when the global PATH is searched before the internal one and therefore will make Visual Studio use your own versions of the tools.
For me, the issue turned out to be that there was a local node.exe file in the same directory that the csproj file was in, along with a node.exe.config file.
I had a post-build event that would call gulp to do some other work and VS2017 would use the local node.exe file rather than what was listed in my $(PATH) variable. This was an earlier version of node that I didn't want to use, so the solution for me was to either remove the local node.exe so the one listed in $(PATH) would be used or upgrade the local node.exe file if the node.exe.config is really needed.
I just wanted to add this here in case someone else runs into this same issue.
It looks as if options in
Tools > Options > Projects and Solutions > Web Package Management > External Web Tools
have no effect. However it is possible to set the Node version through
Project > Properties

Install and use independent node.js environments in different project directories

I really like virtual environments in Python, where you can put a whole Python environment including the interpreter into a project directory. If you dig out an old project after years you can just activate the environment and you are ready to go - this is awesome.
What is the node.js way of doing this?
Usually you mark down the package versions and Node.js version that your code supports in the appropriate package.json directive. This means that distributed versions of your projects import the same modules. Locally, this doesn't matter as npm installs your packages in the project directory by default.
However, for managing your local Node versions more efficiently, a tool such as Node Version Manager will do the trick. NVM specifically supports a .nvmrc file in the project directory to mark down the Node version.

is it necessary to have node_modules folder created for every project while using grunt

while using grunt node_modules directory is created for every project I create and their is a lot of duplication going on, which I don't like....
I am using grunt-collections now but it is very inefficient, and looking for an alternative solution for this.
I don't know much about node but is it not possible to use grunt modules from the central node_modules directory within nodejs directory????
Edit:
Is their any way to install packages globally...????
yes, it is necessary. First, to deal with grunt version mismatch issues, all the global grunt command really does is delegate to a local grunt dependency installed in your project's node_modules directory.
Second, you really want a local version of your grunt plugins/modules as well. Having everything installed locally to each project allows you to have two separate projects on the same machine that depend on different versions of the same grunt plugin. This would not be possible if everything was installed globally.

Is it possible to change the directory where node.js modules are installed?

Is there any way to change the directory where node.js modules are installed? By default (on Linux Mint 13), npm install express installs the express module in home/username/node_modules, but I want modules to be installed by default in home/username/Dropbox/node_modules instead.
Well if you want to change installation directory try setting relevant prefix.
Just a suggestion though, if its a small project, a quick fix is simply install packages separately on each machine. If its a big project then I guess you would be atleast using some source control like git. All package dependencies can be managed elegantly using source control.

Resources