Gulp imagemin dest.on is not a function error - javascript

I'm trying to minify images with imagemin, but it falls with an error.
Can't understand what I've done wrong.
Here is my gulpfile
'use strict';
var gulp = require('gulp');
var imagemin = require('imagemin');
var clean = require('gulp-clean');
gulp.task('img', function () {
return gulp.src('src/img/**')
.pipe(imagemin())
.pipe(gulp.dest('build/img'));
});
gulp.task('clean', function() {
console.log('-----------удаляю build');
return gulp.src('build', {read: false})
.pipe(clean());
});
And an error message from console
$ gulp img
(node:14312) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version.
[22:10:20] Using gulpfile Z:\a\gulpfile.js
[22:10:20] Starting 'img'...
[22:10:20] 'img' errored after 26 ms
[22:10:20] TypeError: dest.on is not a function
at DestroyableTransform.Readable.pipe (Z:\a\node_modules\vinyl-fs\node_modules\readable-stream\lib\_stream_readable.js:516:8)
at Gulp.<anonymous> (Z:\a\gulpfile.js:9:6)
at module.exports (Z:\a\node_modules\orchestrator\lib\runTask.js:34:7)
at Gulp.Orchestrator._runTask (Z:\a\node_modules\orchestrator\index.js:273:3)
at Gulp.Orchestrator._runStep (Z:\a\node_modules\orchestrator\index.js:214:10)
at Gulp.Orchestrator.start (Z:\a\node_modules\orchestrator\index.js:134:8)
at C:\Users\Artur\AppData\Roaming\npm\node_modules\gulp\node_modules\gulp-cli\lib\versioned\^3.7.0\index.js:46:20
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Function.Module.runMain (module.js:577:11)

Usually this error is thrown when you're trying to pipe() something that's not a stream to gulp.dest().
I see in your code var imagemin = require('imagemin'); - are you sure you didn't mean to write var imagemin = require('gulp-imagemin');? imagemin won't return a proper gulp stream, but gulp-imagemin will.

Related

NodeJS can not go to the folder up to execute the command from child_process.spawn

To start WebPack (which is in the parent folder) I use Gulp, but as soon as I try to go to the parent folder in the path, I get an error.
Gulp
var spawn = require('child_process').spawn;
gulp.task('_Scada2', function (done) {
spawn('webpack', [], { cwd: '../../Scada.Web/' })
.on('close', done);
});
get an error
[14:20:47] '_Scada2' errored after 4.47 ms
[14:20:47] Error: spawn webpack ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:229:19)
at onErrorNT (internal/child_process.js:406:16)
at process._tickCallback (internal/process/next_tick.js:63:19)
Процесс завершен с кодом 1.
Try __dirname + '../../Scada.Web/'

spawn ls ENOENT when watched file is modified

I am learning Node.js via the book Node.js the Right Way. I am trying to run the following example to watch changes to a file called target.txt that resides in the the same directory as the .js file.
"use strict";
const
fs = require('fs'),
spawn = require('child_process').spawn,
filename = process.argv[2];
if (!filename) {
throw Error("A file to watch must be specified!");
}
fs.watch(filename, function () {
let ls = spawn('ls', ['-lh', filename]);
ls.stdout.pipe(process.stdout);
});
console.log("Now watching " + filename + " for changes...");
I get the following error when I change the text file and save it:
events.js:160
throw er; // Unhandled 'error' event
^
Error: spawn ls ENOENT
at exports._errnoException (util.js:1018:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:367:16)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
Node.js version: v6.11.0
IDE: Visual Studio Code 1.13.1
OS: Windows 10 64x
There's no ls on Windows, you should use dir instead.
However, that's not an executable. To run .bat and .cmd files you can:
Spawn cmd.exe and pass those files as arguments:
require('child_process').spawn('cmd', ['/c', 'dir']);
Use spawn with the shell option set to true:
require('child_process').spawn('dir', [], { shell: true });
Use exec instead of spawn:
require('child_process').exec('dir', (err, stdout, stderr) => { ... });
For more on that, take a look at this section in the official docs.
EDIT:
I'm not sure I understood you question in the comment correctly, but if you go for the second option, for example, you code will look like this:
...
fs.watch(filename, function () {
let dir = spawn('dir', [filename], { shell: true });
dir.stdout.pipe(process.stdout);
});
...
Please, keep in mind you may need to adjust this code slightly. I'm writing all this from memory as I don't have access to a Windows machine right now, so I can't test it myself.

cannot find module at function_module 469:15

I am learning nodejs from Courseera and wanted to run a code from command prompt in windows 10.But this error Cannot find module always occurs.Iam completely at aloss what this means.
I have tried all the methods given in the other stack overflow threads but couldn't solve the issue.
node and npm are correctly installed with versions v6.9.2 and 3.10.9 respectively.
D:\shaury\node-http\public>node -v
v6.9.2
D:\shaury\node-http\public>npm -v
3.10.9
This is the problem which is occcuring:
D:\shaury\node-http\public>node serve
module.js:471
throw err;
^
Error: Cannot find module 'D:\shaury\node-http\public\serve'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
There's a typo in your code.
var http = require('http');
var hostname = 'localhost';
var port = 3000;
var server = http.createServer(function(req, res){
console.log(req.headers);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<html><body><h1>Hello World</h1></body></html>');
});
//here you should surround your string in 'quotation marks' but make sure to use `backticks`
server.listen(port, hostname, function(){
console.log(`Server running at http://${hostname}:${port}/`);
});
Edit: actually you might need to check that you are in the correct directory. When you launch your app it should be from the directory that your .js file is or else supply the path.

Node js Error: spawn node ENOENT with grunt

I'm trying to run a simple grunt task with protractor but everytime I receive an error on the console. I am using grunt with forever utility and placed various breakpoints take narrow down the error causing code. The selenium server created a session for the test cases but the system generates the following error while trying to execute the test suite.
Error: spawn node ENOENT
at exports._errnoException (util.js:746:11)
at Process.ChildProcess._handle.onexit (child_process.js:1046:32)
at child_process.js:1137:20
at process._tickCallback (node.js:355:11)
[launcher] Process exited with error code 1
I cleared all the node processes and still the problem persisted. Any ideas?
Gruntfile.js
module.exports = function (grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
protractor: {
all: {
options: {
configFile: "conf.js",
keepAlive: true
}
}
}
});
grunt.loadNpmTasks('grunt-protractor-runner');
grunt.registerTask('test', protractor);
};
my_spec.js
this.foreverMonitor = function(port){
forever.list(false, function(){
var child = new (forever.Monitor)('app.js', {
killTree: true,
silent: true
...
});
child.start();
forever.startServer(child);
});
}
var port = 5000;
foreverMonitor(port);
browser.getSession().then(function(sess){
console.log(sess.id);
});
describe('..', function(){
it('..', expect());
});

Node.js file watching basic example

I'm reading a book named "Node.js the right way".
one of the example codes in the book is that we watch a file "text.txt" for change.
The node.js code is as follows:
const fs = require('fs');
var filename = process.argv[2];
console.log(filename);
var count = 0;
if (!filename) {
throw Error("A file to watch must be specified!");
} else {
console.log(filename);
}
fs.watch(filename, function() {
count = count + 1;
console.log("File 'text.txt' has been changed ("+count+") times!");
});
console.log("Now watching " + filename + " for changes...");
The book says that the terminal command should be like this:
$ node --harmony watcher.js text.txt
However, this fails, and gives the following error:
fs.js:1237
throw errnoException(err, 'watch');
^
Error: watch ENOENT
at exports._errnoException (util.js:837:11)
at FSWatcher.start (fs.js:1237:11)
at Object.fs.watch (fs.js:1263:11)
at Object.<anonymous> (/home/ubuntu/workspace/watcher.js:12:4)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
Unless I typed the full target file path like this:
$ node --harmony watcher.js /home/ubuntu/workspace/text.txt
Which one is correct? and why does it identifies the "watcher.js" file and not the text.txt file although they are both in the same directory? How can I overcome this and just type "text.txt" in the command line?
You should resolve the file in the system. Try this command:
node --harmony watcher.js ./text.txt
You could fix your code with:
const fs = require('fs');
const path = require('path');
var filename = process.argv[2];
if(!path.isAbsolute(filename)){
filename = path.join(__dirname, filename);
}
console.log(filename);
This has to do with the scoping of the two variable. You should think of
node --harmony watcher.js
as the name of the program you are executing and
text.txt
as the variable you are feeding it. When you run the node executable it has knowledge of the directory you are in so it can 'see' watchers.js. But where does 'test.txt' get used? It's consumed by fs in the actual body of the program which doesn't have any knowledge of the directory you are in. So it's attempting to find /test.txt instead of /home/foo/bar/test.txt
You could use https://nodejs.org/docs/latest/api/globals.html#globals_dirname like this
var filepath = __dirname + filename;

Categories