how to set grunt variable env path - node.js

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.

Related

NodeJS Build Package (zip) using Grunt on Jenkins

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.

A valid Gruntfile could not be found

I have followed this link to try to install a mean.js stack on an ubuntu server.
Regarding grunt, in /opt/mean, I have done
sudo npm install -g bower grunt-cli
sudo npm install grunt --save-dev
sudo npm install
However, grunt or grunt jshint inside the folder gives
A valid Gruntfile could not be found. Please see the getting started
guide for more information on how to configure grunt:
http://gruntjs.com/getting-started
Fatal error: Unable to find Gruntfile.
Note that npm start works, and http://server_domain_or_IP:3000 can display the sample application.
Does anyone know how to fix the grunt error?
As the error states, you do not have a Gruntfile and https://gruntjs.com/getting-started shows you an example of one.
Simply installing Grunt is not enough. When you run grunt it needs a configuration file (your Gruntfile) to run any tasks. The Gruntfile itself contains the tasks to be run.
You will need a Gruntfile.js in your project. you can create the same using the following grunt command.
grunt-init gruntfile
The above command will create the sample gruntfile.js file in your project.

Why can I run globally installed node modules by name?

I install a node module globally, let's say the grunt module. I install it by:
npm install -g grunt
It's installed in %APPDATA%\npm\node_modules\grunt.
Then I can run it in command line, like grunt --version. How does this happen? I mean, why can I directly use grunt as a command?
BTW, I'm using Windows. And I install NodeJS by .msi installer.
You aren't really running the grunt package as a whole from the command.
The setup for this starts in grunt's package.json. In that, it's specified a bin script that's named the same as the package.
"bin": {
"grunt": "bin/grunt"
},
When you install the package globally, npm adds an executable file for each bin script (there can be multiple per package) to a directory in your system's PATH, allowing a command line to find them when you type the command.
When you run grunt, it's sort of a shortcut to running node bin/grunt from the directory where it's installed, passing along any arguments you provided after it.

Locally installed gulp not running in command line?

I am new to nodejs and gulp stuff. I working on a nodejs project in which I have to run jslint on all the files. I am using gulp for this purpose.
My problem is that In order to run gulp on cli I don't want to install gulp globally and also does not want to update my path variable, So I have installed gulp and other node modules in my project locally using the package.json file
cd myproject
npm install
Since I don't want to install gulp globally and want to run the local gulp I have added script in my package.json file like this as given in this question
{
"name": "",
"version": "1.0.0",
"main": "index.js",
"private": true,
"dependencies": {
"async": "1.5.0"
},
"devDependencies": {
"gulp": "^3.9.0",
"gulp-jslint": "^0.2.2"
},
"scripts": {
"gulp": "./node_modules/.bin/gulp" // is this correct?
}
}
Add added a gulpfile.js inside my myproject folder
var gulp = require('gulp');
// include plug-ins
var jslint = require('gulp-jslint');
// JS hint task
gulp.task('lint', function() {
gulp.src('./common/srp/*.js')
.pipe(jslint())
.pipe(jslint.reporter('default'));
});
gulp.task("default", ["lint"]);
But now on my command line inside myproject folder, when I run gulp and gulp lint I get an error
user1-VirtualBox:~/myproject$ gulp lint
/usr/local/node-v0.10.26-linux-x64/bin/gulp No such file or
directory
Its looking for gulp in the global node module.
Is there a way to make gulp run on cli without installing globally and updating PATH variable.
Any help will be appreciated
Thanks
You can find any executable installed by npm in node_modules/.bin. So you can run gulp locally using:
./node_modules/.bin/gulp
You can find more information at no command 'gulp' found - after installation
With your code you should be able to run command
npm run gulp
Please try
One way to define script is
"scripts": {
"gulp": "gulp"
}
If in case you are not able to run gulp command in your project, run
npm link gulp
It will link your global install gulp with your local project. Then try
gulp -v
If it is showing you the version then you are done. Now you can run any gulp command as you want.
Scripts defined in package.json are accessed through NPM, i.e. npm run-script gulp. I imagine you're trying to run plain old gulp, which should fail since you didn't install it globally.
The scripts section won't automatically create an alias, which I think is your mistake here. You could define one yourself or create a simple bash script if you don't want to type it every time.
Try:
path_to_node path_to_gulp_js gulp_task
Example:
node\node.exe node_modules\gulp\bin\gulp.js build
Like #snorberhuis said. The only way for me to get gulp to work globally was to call gulp manually
I am building in a Jenkins environment
Execute Windows Batch Command
cd your-app
npm install gulp
Execute Windows Batch Command
cd your-app\node_modules\.bin
gulp
Just another alternative that will work locally but will give you global like feeling.
Add to your shell config i.e. ~/.bash_profile the following
export PATH=$PATH:./node_modules/.bin
you have to source that file, execute rehash or just open a new shell and then gulp (and any other script inside that folder) shall be available as a global command.
The way I did this after bashing my head every possible place is simply going to your Application and install npm dependencies like this:
1- E:\webra-jenkins\Code\trunk\WebRa.Web>npm install
Once npm installed then go this directory
2- [%Application_path%]\node_modules\.bin
And execute the gulp and give your file/task, like this:
3-[%Application_path%]\node_modules\.bin>gulp gulpfile --tasks
In my case as I saw the following lines... I got the inner happiness
18:06:36] Working directory changed to [%Application_path%]
[18:06:37] Tasks for [%Application_path%]\gulpfile.js
Now you can run your tasks 1 by one.
[%Application_path%]\node_modules\.bin>gulp pack-vendor-js
Check in your project node_modules/.bin folder and make sure gulp is in there. I had a case where it wasn't there that I never tracked down the reason for. If it isn't there, try re-installing gulp locally and see if it shows up. If that doesn't work and you get tired of the problem, the gulp-cli package will fix it for sure, but that shouldn't be something you have to do.
The simplest solution I know of is to use npm bin:
`npm bin`/gulp ...
This keeps you away from hard-coding any paths.
Nothing was working for me. I followed all instructions from everyone. No matter what I did I could not run the Gulp commands.
To fix this I opened the Node.js command prompt that comes installed automatically when you download and run node.js.
Once I was in this command prompt I could run the following commands:
npm install -g gulp
gulp -v
This is probably a matter of common knowledge but as someone starting out no one suggested to run the node.js command prompt and install gulp from there. Everything I read talked about regular powershell or command prompts with elevated permissions.
Globally install gulp in C:\Users\%USERNAME% using this command
npm install –g gulp
You can install any other gulp methods you need to use.. Ex:
npm install -g gulp-concat
npm install -g gulp-uglify
npm install -g gulp-replace
Then at the directory you wish to use GULP. Open the command prompt (Shift + RightClick) then install locally and you'll be able to execute gulp.
npm install gulp
You can install any other gulp methods you need to use.. Ex:
npm install gulp-concat
npm install gulp-uglify
npm install gulp-replace

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

Resources