r/javascript • u/vintharas • Jan 17 '16
Why I Left Gulp and Grunt for npm Scripts
https://medium.com/@housecor/why-i-left-gulp-and-grunt-for-npm-scripts-3d6853dd22b8?utm_content=buffer56f1a&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer#.vtaziro8n48
u/billybolero Jan 17 '16
26
u/TweetsInCommentsBot Jan 17 '16
Q: How do you know if someone uses npm scripts instead of grunt or gulp?
A: Don't worry, they'll tell you.
This message was created by a bot
6
37
u/joshmanders Full Snack Developer Jan 17 '16
Yes, when you're doing simple things as cleaning a dist/ directory and running webpack or sass/less over some files. npm scripts is a great choice. No extra dependencies. But lets be perfectly honest here. A lot of people do some serious heavy lifting with build tools like Gulp/Grunt and if those tools were migrated you'd have unwieldy long pre/post and regular scripts. That's worse to maintain than a gulp plugin.
14
u/qualiana Jan 17 '16
Exactly this. We have these posts every month, and the response is always the same: that's nice, but talk to us when you're doing more than running
werbpack -p.2
Jan 19 '16
This conversation is kind of stupid. People whine about moving off of Gulp/Grunt with shifting arguments. First it starts out with... that's nice, but show me a build process that doesn't suck. Then when you do show them exactly the kind of proof they asked for its more whining: that's reinventing the wheel or I could never work with so much NIH.
0
u/housecor Jan 18 '16
The examples I've provided are seriously powerful - hot reloading dev server, minify, bundle, sourcemap, automated tests, linting, production build and deploy, and so on. npm scripts actually offer more flexibility than Gulp/Grunt because you have 200k+ npm packages and all the compositional power of both your OS's CLI and Node directly at your disposal. No plugin abstractions required.
3
u/qualiana Jan 18 '16
Don't get me wrong: I use and love
run-script. These posts are just repetitive, for the same reason that the "gulp vs. Grunt" and "you don't need jQuery" ones were before: they miss the point. Do you need to use gulp? No. Does it make for easier-to-read scripts in yourpackage.json? Yes. You can also use all of those npm packages in gulp scripts, so the lack of a (well-maintained) gulp plugin doesn't limit what you can do.1
u/joshmanders Full Snack Developer Jan 18 '16
You know you can do the same thing in gulp right? Not EVERYTHING has to be a gulp plugin. Use
through2and you can use any package you need.I do it here: https://github.com/kratos/kratos.github.io/blob/develop/gulpfile.js
to pipe markdown files into a jade template for rendering of pages.
2
u/dmitri14_gmail_com Jan 18 '16
Nice example. The only thing I would improve is avoid mixing different level abstractions.
throughis a lower level, so I would encapsulate it into a mini-Gulp-plugin ;-).So effectively you are creating your own plugin as you go. Which is only good for single time use, but as soon as you repeat it, you are back to the plugin architecture. :-)
1
u/dmitri14_gmail_com Jan 18 '16
200k+ npm plugins Wild West is less user-friendly than 100 top quality standardised Gulp plugins. More choice is not always better quality.
4
Jan 18 '16
Also, the claim that there are 220k NPM packages and a combined ~7k for Grunt and Gulp is very misleading. There's nothing preventing your usage of those same 220k packages with Grunt and Gulp except for the same thing that prevents you from using them with NPM scripts... Their irrelevance in relation to the task you are trying to complete.
4
u/joshmanders Full Snack Developer Jan 18 '16
No way man, can't use anything that doesn't have
gulp-as a prefix. Them's the rules.3
u/mycall Jan 17 '16
if those tools were migrated you'd have unwieldy long pre/post and regular scripts.
I see calling one script with a few parameters. Not hard to maintain.
Still, complicated build/deploy/test/publish situations do require more. If Gulp/Grunt work well, go for it. Most situations don't need it though (e.g. simple web deployments).
2
2
u/runvnc Jan 17 '16
What heavy lifting exactly for example?
7
u/joshmanders Full Snack Developer Jan 17 '16
I use Gulp to not only build changelogs based on commits, and figure out if it should deploy a major, minor or patch release from that data, all the way to building and deploying docker containers.
-4
u/mycall Jan 17 '16
Couldn't a shell script do all that just as easily?
14
u/joshmanders Full Snack Developer Jan 17 '16
If you want to rebuild Gulp/Grunt just to say "I don't use Gulp/Grunt" sure. I don't want to.
2
u/mycall Jan 17 '16
A few dozen lines of script for a particular purpose is hardly rebuilding Gulp/Grunt. It just depends what you are doing. Sometimes writing Java/Python is easier than using Gulp/Grunt too.
6
u/joshmanders Full Snack Developer Jan 17 '16
A few dozen lines of a script usually fall into not very much stuff that would require heavy lifting. My commits analyzing code is longer than a few dozen.
-7
u/mycall Jan 18 '16
My commits analyzing code is longer than a few dozen
If code was written correctly to begin with, code analyzing is not that important.
5
2
u/joshmanders Full Snack Developer Jan 18 '16
Has nothing to do with the code and all to do with the commits. Using a certain commit style allows you to programmatically determine if the new release should be major/minor/patch and have it automated.
0
3
u/jasontrill Jan 17 '16 edited Mar 09 '26
reddit leadership is turds, goodbye
0
u/mycall Jan 18 '16
The shell scripts I'm thinking of would need to be done for a particular purpose that doesn't need to be shared. This is quite common.
2
1
u/housecor Jan 17 '16
I disagree. You have just as much compositional power with npm scripts. You can call any .js file and run it via Node. For a complex example, check out https://github.com/kriasoft/react-starter-kit
0
u/joshmanders Full Snack Developer Jan 17 '16
Please read my response to /u/Veeediot in this same thread.
0
Jan 17 '16
[deleted]
2
u/joshmanders Full Snack Developer Jan 17 '16
So you're making a makeshift build system that's less maintained against edge cases and a wide variety of people, and to what? Say you use npm scripts?
Okay, you do that. I'd rather not introduce bugs into my stuff just to be trendy.
4
u/relishtheradish Jan 18 '16
Gulp hasn't been totally perfect for me, but nonetheless it's made build tasks relatively painless to write and as a dev that uses both Windows and Mac its portability is huge. Writing bash scripts, especially for any non-trivial tasks, seems like a step back to me.
1
u/Epp3s Jun 21 '16
But isn't bash coming to Windows 10? So won't that blow cross-platform/OS issues out of the water? Granted you'd need to be on Win 10, but still. Just wondering.
0
u/housecor Jan 18 '16
Using the npm scripts approach isn't necessarily about "Writing bash scripts". It's this:
Write command line scripts that run on your OS
Leverage node packages to make your command line calls cross-platform if necessary.
Call separate scripts (typically written in JS and using npm packages) as necessary.
Thus, you have the same power for managing complexity, but without the downsides of working with an extra layer of abstraction.
6
u/Shpirt Jan 17 '16
Why not makefiles?
4
u/alfred-nsh Jan 18 '16
I heard they don't work on windows.
3
u/makis Jan 19 '16
because reimplementing unix tools in javascript just to make them work on Windows makes a lot of sense, yeah...
3
u/flying-sheep Jan 19 '16
hah, so we had 40 years of tool evolution and some people arrive at the conclusion that embedding a worse version of makefiles without variables or multiline tasks is somehow good.
1
1
3
u/ishmal Jan 17 '16
Looks like it depends on a Unix-y OS for streaming.
1
u/housecor Jan 17 '16
Virtually any example you see using a Unix command can be replaced by an npm package. For example, you can stream cross-platform with npm-run-all. https://www.npmjs.com/package/npm-run-all
5
u/fuckcreativity Jan 17 '16
I like to combine both approaches. I'll use my npm scripts to call the local gulp like so
scripts {
"build": "node node_modules/gulp/bin/gulp build-task"
}and do some of the most common tasks. This way if you have gulp, it's easy to use. If you don't have it, you have access to some of the most important tasks, and can install globally if you really want it. I typically try to avoid forcing people to install globals by giving them npm scripts as an option
8
u/yamalight Jan 17 '16
you don't actually need to write
"node node_modules/gulp/bin/gulp- just using"gulp task"is sufficient since npm will first search for packages in projectnode_modulesfolder5
4
u/TheNiXXeD Jan 17 '16
This is good practice. Nobody should have to global install to use your project.
1
u/NeekGerd Jan 18 '16 edited Jan 18 '16
This is the best solution IMHO.
Because it's cross-platform, and you don't have to install gulp globally to make it work.
Simple fork, local install and run.
And like another comment said, no need for the
node node_modules/gulp/bin/gulppart. But the reason is because the npm execution will have its ownPATHfilled with the./node_modules/.bin/folder.edit: phrasing
8
u/krasimirtsonev Jan 17 '16
The truth is that it's not npm scripts against gulp/grunt but bash against gulp/grunt. I'm also considering moving to complete npm script setup. It's more robust and simple.
16
u/TheNiXXeD Jan 17 '16
And suddenly your stuff doesn't work on Windows. I don't use it but I do feel sorry for the few people with Windows computers at my work.
-4
u/mycall Jan 17 '16
There are many ways to run bash on Windows. The point is use whatever you are comfortable with since there is no best/optimum solution.
7
u/TheNiXXeD Jan 17 '16
Of course there are ways around it, but that's not the point. For Unix/Linux/Mac, it just works. If you use something like gulp, it also just works on Windows too.
You can use npm to the same effect, but you can't use native OS commands.
0
u/makis Jan 19 '16
If you use something like gulp, it also just works on Windows too.
don't be so sure
1
u/joshmanders Full Snack Developer Jan 19 '16
Chances are a gulp plugin will work on Windows a lot more than a bash file with
#!/usr/bin/env nodeat the top.1
u/makis Jan 19 '16
Yeah, in theory...
I tried to make gulp-scp work, oh sorry, it's scp2, oh sorry, it's sftp, oh sorry, it doesn't work anyway, I better start a process running rsync, that'll work for sure, it's been doing its job since 1996.
And so I did.
And it works on windows too.1
u/joshmanders Full Snack Developer Jan 19 '16
Honestly I can't say anything about those. But it's not unknown that Node.js doesn't accommodate Windows very well. Most developers are on Mac or Linux. Windows needs some more love.
1
u/housecor Jan 17 '16
While you can certainly use plain Bash, npm scripts can work great cross-platform as well. Combining cross-platform compatible commands with npm packages produces a cross-platform compatible solution.
0
Jan 17 '16
[deleted]
5
u/TheNiXXeD Jan 17 '16
As a lead who's had to see various people run through Windows setup, no. It's actually much worse to get a Windows machine working with node than any other platform. Don't use the excuse "you can just install x" ever.
For your own solo project nobody will touch, sure. But if you're publishing stuff to npm or working on a team, no.
0
2
u/flying-sheep Jan 19 '16
To be fair, if the Grunt or Gulp plugin you need isn’t available, you can certainly still utilize npm packages. But then you’re no longer leveraging Gulp or Grunt.
humbug. gulp works with streams. by using vinyl-transform, you can “leverage gulp” using anything that handles streams.
JS land has churn with no end, but let’s be fair: all build tools fucking suck, in any language.
there’s nothing that doesn’t have one or more of those:
glass ceiling
tedious manual scripting
underdocumented neverending torrent of plugins
so i’m OK with the churn here if something of value comes out of it.
variable-less shell script lines in a package.json are not such a thing
1
u/Deinumite Jan 17 '16
That example of "streaming" shows a common "useless use of cat".
1
u/housecor Jan 17 '16
Ah! Great point. Fixed.
1
u/Deinumite Jan 18 '16
A good example for pipes is usually like sort or uniq or something.
Just curious, I think a big reason people use gulp and grunt is for the file watching capability. Do you use something like guard for this or do you just not use that for your workflow?
2
u/housecor Jan 18 '16
I use React's hot module reloading, combined with Webpack's dev server and browsersync for cross-plat mobile testing. It's an enjoyable, rapid feedback combo.
1
u/flying-sheep Jan 19 '16
i like using cat that way in longer pipes.
it’s a natural starting point.
cat file | herp | derpif you want to put a further step in front of the first, you don’t have to change
herp -i file | derpto
hurr <file | herp | derpbut you can simply insert
| hurr, just like at any other point of the pipemy shell allows to do
<file | herp | derpwhich is also good.
0
u/dmitri14_gmail_com Jan 17 '16
"Documentation is excellent" must be a good joke. It gets dry very fast, e.g. doesn't even mention in what environment postinstall is running and how to access user's local directory still remains a mystery.
1
u/housecor Jan 17 '16
Your concerns appear to be about the quality of Node and npm's documentation as a whole. The post specifically references npm's documentation of the scripts feature, which I find quite good.
2
u/dmitri14_gmail_com Jan 17 '16
Postinstall is npm script, and this lacking information is just one level below the basic one.
1
u/housecor Jan 17 '16
Ah, good point. I admit that I've found various blog posts, videos, and open source projects to be the best way to learn. You're right that "excellent" is giving the docs too much credit. I adjusted the post.
2
u/dmitri14_gmail_com Jan 17 '16
Yes, they definitely are. But that also means you have to be "in luck" to come across the right ones.
I actually never had this problems with Gulp. Its abstraction is really tiny and much simpler to reason.
Also the plugins I've found of high quality and easier to deal with than with command lines directly. Gulp forces plugins into standardised API, which is why they are easier to use than the non-standardised command line options, that you are stuck with inside the npm scripts.
Imo.
2
u/Graftak9000 Jan 17 '16
So gulp is to npm scripts what jQuery is to the DOM? (Talking in laymen terms)
2
u/dmitri14_gmail_com Jan 18 '16
Indeed, great analogy! Suffices to add, jQuery is tremendously successful.
0
Jan 18 '16
Why would you choose this over a Makefile?
2
u/flying-sheep Jan 19 '16
jup, that’s pretty much less powerful makefiles with more complex syntax because it’s wrapped into JSON
1
Jan 19 '16
I get the feeling that if I had suggested triggering Makefiles from within the package.json file, it wouldn't be so unpopular.
1
-1
u/makis Jan 19 '16
because fuck you we are developers NIH DRY life is miserable javascript sucks
I will never know or understand them man , never…
0
Jan 17 '16
[deleted]
4
u/dmitri14_gmail_com Jan 17 '16
For anything non-trivial you depend on plugins pretty much the same way. Plus no standard interface, so more time spent on learning.
0
u/housecor Jan 17 '16
The vast majority of build related tools in npm provide a standard interface: A CLI.
3
u/dmitri14_gmail_com Jan 18 '16
Yes, and this is the problem --- there is no standard for CLI. Or maybe there should be but I've never seen one.
Gulp forces your plugin into standard pattern which is a good thing. With CLI you are your own cowboy doing whatever you want. Which for your users isn't a good thing apparently.
This is the key difference why I find Gulp plugins much easier to use. It is unfortunate that people abuse Gulp's power creating over-engineered scripts, but every powerful tool can be abused, doesn't mean the tool itself is bad.
1
u/housecor Jan 18 '16
Does Gulp truly provide a standard pattern? How do I know how to configure Webpack vs React vs ESLint vs Mocha in Gulp?
Answer? I don't. I have to read the plugin's docs. And since the plugin's docs aren't comprehensive, I also read the underlying tool's docs. Then I try to determine how to configure the Gulp plugin accordingly. I often end up writing a chunk of JSON that's specific to the tool. Configuration is certainly not standardized.
In contrast, when I use a tool that exposes a CLI directly, I interact in a standardized way. The CLI exposes flags and accepts parameters. I only need to reference one set of docs: The docs for the tool I'm working with.
2
u/joshmanders Full Snack Developer Jan 18 '16
Standard practice with gulp plugins is to expose a function that can be executed in a
.pipe()call that takes at least a single argument which is an object literally with key/values that correspond to configuration options for the plugin.99.9999% of all plugins tell you right in the
readme.mdhow to use the plugin and give examples.CLI isn't as standard. What if one plugin only accepts a pipe stream in, and others only accepts an array of files? At least with Gulp I know it's accepting the same standard format through all plugins.
1
u/dmitri14_gmail_com Jan 18 '16
Gulp plugins receive and emit streams. That removes the need to bloat their configuration, and limit it to advanced parts you may not even need.
In comparison, browserify vs webpack can't even agree where their output will go, with different syntaxes to be used in the CLI. How is that standard?
Webpack does even worse by forcing you into their proprietory configuration that you can't avoid even for most trivial setup.
0
Jan 17 '16
[deleted]
2
u/dmitri14_gmail_com Jan 18 '16
You are saying why you use it, it is hard to disagree with that :)
As for plugins, Gulp's main role is to make them easier to use, by providing standardised interface. jQuery vs vanilla JS is a good analogy mentioned above.
2
u/dandv Jan 19 '16
Using npm instead of Grunt/Gulp/etc. has been extensively discussed by Keith Cirkel. Would've been nice to at least acknowledge that, if not join efforts and try to address some of the inherent shortcomings (e.g. the readability of long lines).