Is it possible / How to run gulp modules directly? - node.js

It would make sense for me to use some of gulp modules/plugins independently. E.g.: I have installed gulp-less globally (npm install --global gulp-less) and want to use it instead of lessc in the manner like grunt-less -options ./my/css/folder or grunt less -options ./my/css/folder. But my attempt end with an error message:
$ gulp less ./css/
Using gulpfile ~/temp/gulptest/gulpfile.js
Task 'less' is not in your gulpfile
Please check the documentation for proper gulpfile formatting
Ideally I also would like to use gulp plugins outside of any project as independent tools.
How to run gulp modules outside of the build context?
If this option is not provided: Is there a workaround?

Put in your gulpfile.js:
gulp.task('less', function () {
// as you wish
})

Related

How to create a simple (Hello World) node.js TypeScript project with ES6 modules?

Imagine a very simple program with a main file and some functions in a separate file, using ES6 modules and intended to be run with node.js.
my-utils.ts:
import * as fs from "fs";
export function readSimpleFile() {
return fs.fileReadSync("hello.txt", "utf-8");
}
main.ts:
import {readSimpleFile} from "./my-utils"
console.log(readSimpleFile());
What is the minimum set of files I need to add to the project and commands I have to run to get it building, running and checking types?
If you are to run a typescript project with node you need to have at least node, npm and typescript installed on your plateform.
Using an IDE to setup the project
Using intelliJ IDEA or Webstorm (they are the ones I know the best), the compilation of typescript into javascript is done automatically; you only need to do some settings.
Let us assume you have a file called project.ts containing your hello world code; IDEA or Webstorm will compile your code to project.js. Then you will only need to do node project.js to run your project.
Doing everything from scratch
First you need to know where exactly your npm packages are installed globally. This command can help you identify the path: npm config get prefix. In this folder, you should have a nodes_modules subfolder that contains the typescript module. If there is no typescript module, that is because you did not install typescript globally (npm install -g typescript).
Then you have to add the path of the bin of typescript subfolder in your environment variable.
Now you can compile the project with typescipt: tsc project.ts and you can run it node project.js.
Since you are using node function like fs you will need to install node typings npm install #types/node --save-dev before compiling with tsc.
Compilation option
To enable or disable all strict type checking options, you might need to use compilation option. You have to create the file in which you will specify the compilation option: tsc --init will create a tsconfig.json in which you can specify what behaviour you would like to have during the compilation of your app. All options are listed here.

How to run node.js ES6 file on mac

Can someone help me run a node.js file that has ES6 features? This is my current workflow:
Save change in file
npm run build --- builds file to my src/dist folder
cd into my dist directory
node
var file = require('./index.js')
file.someMethod()
Then when I make a change to my method, I need to repeat all the steps above again.
Does anyone know of a more efficient way to do this?
Thanks in advance!
You dont need anything if you are not using import/export. Just Install node 8.x and then run node my_file.js. Nodejs supports most of the es6 features out of the box.
If you want to use import/export then I would recommend to use typescript: https://www.typescriptlang.org/. It has compiler TS (statically typed ES6) to ES5.
Oh, and of course both babel and typescript have watcher modules (auto recompiling on file change).
It really depends on what npm run build actually does in your app. Does it run gulp/grunt? Webpack? Etc. If it's webpack there is a webpack-dev-server that you should look into.
If it's grunt or gulp then you have a custom build chain that you're working with. If all you need is to support es6 features without having to build the project then you could use something simple like babel-cli and run your file from outside the dist directory.
$ npm i -g babel-cli
$ babel-node
> var file = require('./index.js');
> file.someMethod()
By running babel-node you're running the normal node repl but with babel required. Es6 features should now be supported in the repl without needing to be compiled to es5.
You may also want to try simply updating to the latest version of node because node 8 has pretty extensive es6 support.

browserify will not compile express js

I wrote a very basic express.js app. Then tried to make it one .js file. Browserify compiled the whole thing to a one file. But browserify-compiled code didn't work. As far as I know, browserify just replaces require statements with module codes. Error is:
C:\Users\HP\n\express\app.js:27025
__proto__: http.IncomingMessage.prototype
^
TypeError: Cannot read property 'prototype' of undefined
at Object.__dirname.173.accepts (C:\Users\HP\n\express\app.js:27025:34)
at s (C:\Users\HP\n\express\app.js:1:316)
at C:\Users\HP\n\express\app.js:1:367
at Object.__dirname.170../application (C:\Users\HP\n\express\app.js:26823:11)
at s (C:\Users\HP\n\express\app.js:1:316)
at C:\Users\HP\n\express\app.js:1:367
at Object.__dirname.168../lib/express (C:\Users\HP\n\express\app.js:26154:18)
at s (C:\Users\HP\n\express\app.js:1:316)
at C:\Users\HP\n\express\app.js:1:367
at Object.__dirname.153.express (C:\Users\HP\n\express\app.js:24010:15)
Browserify is designed specifically to package code for a browser.
Node.js supports a number of modules that a browser doesn't which have to be emulated by builtins. These modules will be replaced by a browser-specific shim. Some only supply a subset of the Node API that makes sense to have in a browser.
So you are running an app that has converted all the Node.js modules to support running what it can in a browser, back in Node where the modules are available but are no longer being used.
Try rollup or you could possibly configure babel to work like you need
I had this very same issue but like you said the compile code should work on server side. I solved it from this link:
https://www.linkedin.com/pulse/bundling-nodemodules-your-nodejs-app-one-single-file-xuan-son-nguyen/
Use browserify for bundling and terser for minifying. Starting by installing them globally:
npm install -g browserify
npm install -g terser
Next, we have to add a build script to package.json
...
"scripts": {
...
"build": "browserify --node --ignore-missing index.js | terser > bundle.js"
}
...
Each time you want to promote to production, you have to make a new bundle:
npm run build
A new file called "bundle.js" will be created.
Let there be peace, and there was peace. Happy coding.

Execute arbitrary node script in gulp

I want to run an index.js file as a part of my gulp process. I could add node index.js through something like gulp-shell as described in this thread (How to run bash commands in gulp?) but it seems there should be an easier way.
I figured there would be a plugin called gulp-node or something to do this but it doesn't exist. How do others do it without turning your initial script into a gulp plugin, which seems intrusive?
You can use var data = require('./path/to/index') to load/run the index.js module in your Gulp task and send any exported information to data. You can read more about require and Node.js modules here.

Can I write npm package in CoffeeScript?

I have used CoffeeScript for a while. Now I need to write a npm package, can I write it in CoffeeScript, or I should compile CoffeeScript into JavaScript?
I'm going to suggest that you write your package in coffeescript, but only publish it in javascript. I do it like this:
coffeescript code goes in src
code is compiled to lib
src is committed to my git repo, lib is in my .gitignore
lib is published to npm, src is in my .npmignore
the coffee-script package is in my devDependencies
You can take a look at a simple package of mine, refix, for inspiration:
https://github.com/linus/refix
npm install refix
You can write NPM modules in coffeescript, but in order for them to be usable by JS users they must be compiled to JS before you publish on NPM.
package.json makes this easy with their prepublish script hook which runs the specified script before you publish. Heres an example of a prepublish NPM hook in zombie.js
https://github.com/assaf/zombie/blob/master/package.json#L16
If a lot of your modules have coffee-script in their devDependencies, it's useful to just globally install coffee-script instead of install it for each module (which takes much longer).
coffee-build is a global version manager for coffee-script.
Just add these 2 scripts to your package.json:
{
"name": "my-coffee-module",
"scripts": {
"build": "coffee-build -v 1.11.x -b -o js src",
"postinstall": "npm run build"
}
}
Notice how -v 1.11.x is not an exact version, which allows implicit upgrades.
The only downfall is that users must npm install -g coffee-build before they can install your module.
I have written npm packages in CoffeeScript from scratch. I encourage you to use CoffeScript for node as well as for the Browser. However, before you can use or publish your module, you have to compile the source CoffeeScript to JavaScript. That should not hold you back from using CoffeeScript, though.
Tip: While developing, use coffee -cw yourfile.coffee (command line) to watch the file for changes and compile on save.
While I'm not sure if it's the best approach, technically it is possible to write your package mostly in CoffeeScript.
Basically, you can write a JS file that simply wraps the coffee command, like so:
bin/howl.coffee
console.log 'Awwwooooo!'
bin/howl.js
#!/usr/bin/env node
var path = require('path');
var exec = require('child_process').exec;
var coffee = path.resolve(__dirname, '../node_modules/coffee-script/bin/coffee');
var howl = path.resolve(__dirname, './howl.coffee');
var command = coffee + ' ' + howl;
exec(command, function(error, stdout) {
if (error) { throw error };
console.log(stdout);
});
Running node howl.js (or simply howl when it's installed globally) will now output Awwooooo!. You can do things like require other CoffeeScript files and access arguments by passing them from the JavaScript "wrapper" to the CoffeeScript.
Anyway, there may be reasons not to do this, but it has worked for me so far so figured I'd submit this for an additional perspective.
For a simple example project using this technique, check out https://www.github.com/joshuabc/packdown.

Resources