NodeJS Build Package (zip) using Grunt on Jenkins - node.js

I'm trying to build a package (zip) using grunt in Jenkins (though Jenkins is kind of irrelevant at this stage).
Basically the code is checked out of git, and I run
npm install --production
but grunt needs the files installed locally.
So I run
npm install
But now I have all the grunt packages in the node_modules directory.
As part of the grunt build I simply want to copy the node_modules directory into the package.
Am I headed down the right track with trying to package my code using grunt?
Or should I be using grunt to only run jslint, unit tests etc and use this to package: https://github.com/zeit/pkg
The output is another tool that I will be calling from the command line.

Related

Running npm build before/after npm install

I'm not familiar with npm so I might be holding the wrong end of the shovel here...
There is a package on npm that I would like to modify and use in my own project. The package is angular-crumbs. I forked the source repo (https://github.com/emilol/angular-crumbs) into my own account (https://github.com/capesean/angular-crumbs) and then run npm install capesean/angular-crumbs -force. However, this produces a node_modules folder in my project that hasn't been built (and whatever else - as I understand it) with the commands in the source repo's package.json file:
"build": "npm run clean && npm run transpile && npm run package && npm run minify && npm run copy"
i.e. it doesn't have the types, the correct package.json file, etc.
So my question is, how do I get the properly-built files (including type definitions, etc.) from my own repo to install or build-after-installing in my target project?
I am not sure about what you're trying to do, are trying to work on the
angular-crumbs source code, or are you trying to use it in your own project as a dependencuy ?
Anyway, running npm install will install all your dependencies so that you can directly use them in your project, those dependencies don't need to be built after they are installed.
In your case you seem to have an angular application (which is completely different from node.js), usually to start an angular app you can run ng serve which will build your source code and run an angular server so you can access it on localhost.

how to set grunt variable env path

I am trying to set grunt for my node.js project and I have followed below steps:
1)I have installed node.js and it is working fine.
2)Installed git.
3)Installed grun by running: npm install -g grunt-cli.
C:\Users\user\Downloads\bpost-gs-api-1.1.1>npm install -g grunt-cli
C:\Users\user\AppData\Roaming\npm\grunt -> C:\Users\891153\AppData\Roaming\npm\node_modules\grunt-cli\bin\grunt
C:\Users\user\AppData\Roaming\npm
`-- grunt-cli#1.2.0
Now I have run the cmd grunt -version, I got below error:
grunt is not recognized as an internal or external command,
operable program or batch file.
Can you please help me to set up grunt for my project. How to setup variable env and how to setup path etc.
Appreciate your help
https://gruntjs.com/getting-started
In order to get started, you'll want to install Grunt's command line interface (CLI) globally. You may need to use sudo (for OSX, *nix, BSD etc) or run your command shell as Administrator (for Windows) to do this.
npm install -g grunt-cli
This will put the grunt command in your system path, allowing it to be run from any directory.
Note that installing grunt-cli does not install the Grunt task runner! The job of the Grunt CLI is simple: run the version of Grunt which has been installed next to a Gruntfile. This allows multiple versions of Grunt to be installed on the same machine simultaneously.
start from reading this:
https://gruntjs.com/installing-grunt
If you need a specific version of Grunt or a Grunt plugin, run npm install grunt#VERSION --save-dev where VERSION is the version you need.
for better understood on how to start setup a grunt project: https://gruntjs.com/getting-started#preparing-a-new-grunt-project
A typical setup will involve adding two files to your project: package.json and the Gruntfile.
package.json: This file is used by npm to store metadata for projects published as npm modules. You will list grunt and the Grunt plugins your project needs as devDependencies in this file.
Gruntfile: This file is named Gruntfile.js or Gruntfile.coffee and is used to configure or define tasks and load Grunt plugins. When this documentation mentions a Gruntfile it is talking about a file, which is either a Gruntfile.js or a Gruntfile.coffee.

How to install a node.js package using npm

I need help trying to install a node.js package from Github using the npm command prompt. It's an adaptive grid Jquery plugin called Masonjs: https://github.com/DrewDahlman/Mason.
It's my first time to use node.js, hence the difficulty understanding the setup instructions. I've CD'd to the project folder and run the 'install npm' and 'install bower' steps successfully, but I don't understand how to complete the remaining steps: running and building Gulp (is building necessary?).
Any help would be very much appreciated.
If you just want to use masonjs library in your project, you don't need to run gulp commands for running and building. Just cd into your project directory where you have initialized npm/bower and run npm install masonjs / bower install masonjs.
The next step would be to add the mason.js file in your index page. Now there would be different folder structure in which this mason.js file is present.
In case of npm module the path would be {your-project-directory}/node_modules/masonjs/lib
And in case of bower components the path would be {your-project-directory}/bower_components/MasonJS/dist
Now just use this library and it will work perfectly.

Bower, Grunt on Ubuntu 12.04 - command not found

I've installed bower and grunt on my machine but non of it works. I get :command not found for both.
I've placed paths to bower and grunt in .bash_profile file, like:
export PATH="/home/user/.node/lib/node_modules/grunt-cli/bin:$PATH"
export PATH="/home/user/.node/lib/node_modules/bower/bin:$PATH"
It feels like packages are installed correctly but it can't be found.
Npm and node is located in home/user/.node and home/user/.npm directories is this is the right place for it?
which bower/grunt outputs nothing
Just had to remind myself of this one, to set up environment on a new machine.
As per http://gruntjs.com/getting-started, there are two steps required for installation and use of Grunt.js task runner on a given project:
You should globally install only 'grunt-cli', the Grunt Command Line Interface. This will put the grunt command on your system path. This is achieved by running npm install -g grunt-cli, which may require root privileges depending on your setup.
You should locally install the grunt task runner proper. This is achieved by running npm install, after adding the desired version of Grunt.js to your project's package.json file. This will install the specific version of Grunt.js described in your project's package.json, under the devDependencies section. This is the file used by nodejs to describe project development and deployment dependencies, among other stuff.
I managed to fix it by adding paths to .bashrc file, like:
PATH=$PATH:/home/user/.node/lib/node_modules/grunt-cli/bin
PATH=$PATH:/home/user/.node/lib/node_modules/bower
Reference

Jenkins script quitting prematurely when using npm install on Windows

In my Jenkins job I want to build a JavaScript app using Grunt. The Jenkins build scripts creates a build directory (if it doesn't already exist), changes to that directory and runs:
npm install grunt
npm install grunt-zip
grunt --gruntfile=[something]
(Of course grunt-cli is installed globally.) When I build the job, the first statement causes Grunt and dependencies to be pulled down as expected. However, the job then terminates successfully:
Archiving artifacts
No emails were triggered.
Finished: SUCCESS
The second npm install is not run. Any idea why the script is terminating after running npm install instead of continuing to the subsequent statements?
So it turns out that npm is a batch file, not an executable, so it needs to be invoked using call from the Jenkins script:
call npm install grunt
i would recommend not using the local grunt / nodejs install, but instead getting jenkins to do this for you!
it's much easier and means there's less coupling to system specific installs and variables.
steps:
a) use nodejs jenkins plugin + get it to install nodejs on machine/grunt-cli -> Jenkins integration with Grunt
b) populate your package.json with any nodejs dependances required, eg grunt/grunt-zip etc
c) when running grunt just do a "npm update" before "grunt" command
that way your not doing explicit npm install, it's all configured from your package.json, and your build scripts will be less brittle, and your developers can use the same steps as the build server, eg "npm update;grunt" locally same as build server
For future googlers:
use command chaining for this.
This works:
npm install && npm install install grunt-zip
This wont work:
npm install
npm install grunt-zip

Resources