Why should gulp be installed both globally and locally? - javascript

I am new to npm and gulp. I am reading this css-tricks tutorial on gulp. In section Installing Gulp they installed gulp globally so that it could be used with command line from any location in the computer. In the next section Creating a Gulp Project they again installed gulp locally.
What I don't understand is if gulp is already installed globally and we can use it from anywhere then why install gulp twice? Why not just put a gulpfile.js with other package.json file?

You install gulp globally for using simple gulp command in your terminal and install gulp locally (with package.json dependency) in order not to lose the dependency, because you can install your project to any computer, call npm i and access gulp with ./node_modules/.bin/gulp without any additional installations

You don't even need to have installed gulp globaly. Just have it locally and put gulp commands in package.json scripts like this:
"scripts": {
"start": "gulp",
"speed-test": "gulp speed-test -v",
"build-prod": "gulp build-prod",
"test": "NODE_ENV=test jasmine JASMINE_CONFIG_PATH=spec/support/jasmine.json"
},
Than everyone working on same project can just npm install and start running commands without even having gulp globally installed.
npm start will run gulp
npm run speed-test will run gulp speed-test -v
npm run build-prod will run gulp build-prod
And of course add as many commands as you want there. And if someone from team have or wants to have gulp globally than they can run gulp commands directly from terminal.

Related

Failure to run the Gulp task runner and error while running Gulp Command

I inside the folder where the project is located through the command:
npm i gulp gulp-sass sass --save-dev
I have installed Gulp, but when I want to run it with this command in vs.code terminal :
gulp
I get an error, I wanted to know what caused the error?
On the system node js 14.1.0 & 16.13.0 and npm are already installed
The error message hints that gulp is not recognized as a command, I don't think this has anything to do with gulp-sass or sass specifically.
Option 1
Install gulp-cli globally:
npm i -g gulp-cli
glup-cli provides the executable gulp command, which will use the version of gulp is installed in your package. This is the preferred way to run gulp.
Option 2
Run gulp directly from your node_modules/.bin folder with the npx command. npx is set up by npm, so there is no need to install any additional packages.
npx gulp
or, equivalently
npm exec gulp

What is npm run build command and how webpack is used in creating bundle file

What's the difference between npm run build and npm install webpack?
In what ways these commands differ
webpack is a module bundler for javascript application. In order to run webpack ypu need to get the webapck in your project.npm install webpack will install webpack from node module software library using npm install webpack command.
npm run build is a separate command to build the code. If you open package.json inside scripts you may see a key like build example
scripts:{
build:someValue
}

node.js as localhost and use gulp to watch changes

I cloned a repo here https://github.com/willianjusten/bootstrap-boilerplate and do the following step.
git clone git://github.com/willianjusten/bootstrap-boilerplate.git new_project
cd bootstrap-boilerplate
npm install
gulp
The gulp does the work. But is the server started? In gulp js file I'm seeing the author use livereload, how do I start my development with that?
You can use nodemon (https://github.com/remy/nodemon). If any file is changed, nodemon will re-start automatically.
Sample usage
install
npm install -g nodemon
start node
nodemon server.js

npm environment or shell to run dependent modules locally

Does npm has a shell or environment inbuilt, which can be used to run node modules installed locally ?
Ex:
I have nodeunit, eslint and other packages listed in my dependencies. I can run commands provided by these dependencies from npm scripts like:
"lint": "eslint .",
"test": "nodeunit test/*js",
These commands cannot be run outside npm scripts. I know, with a small configuration we can achieve this (by updating the PATH with node_modules/.bin), but I am wondering if there is npm way of doing this (something like npm shell which would enable this).
In other words:
In my project dir I will not be able to run this: nodeunit . because nodeunit is not available in the PATH. When I execute npm test, npm adds node_modules/.bin to PATH, hence nodeunit is available under npm test script. My question is: Is there a npm way which will enables us to execute these commands in the project directory without using them in npm scripts

NPM install + use local modules? on windows

I created a package.json and added the needed dependencies (grunt, bower, jasmine etc)
when I run
npm install
it correctly installs all the modules in
$pwd/node_modules/*
however when I try to USE those modules:
bower init; grunt init
i get
sh.exe: (bower / grunt / w.e) Command not found
I can solve this by using
npm install -g (package name)
but my understanding is the "-g" makes the install global? (is that correct?)
I want to be able to use the packages i installed locally...is this possible?
You can use npm run-scripts to create a command which will run the local copies of bower and grunt that you have installed.
In package.json, add a key like this:
"scripts": {
"init": "bower init; grunt init"
}
Then run the command npm run init.

Categories

Resources