fs undefined in cakefile - node.js

I'm trying to build a simple cakefile to perform build tasks for a node project I'm working on. Following this gist from github, I've managed to throw the following basic code together:
CoffeeScript = require 'coffee-script'
{exec} = require 'child_process'
fs = require 'fs'
web_build_path = 'bin/web'
task 'build', 'build server-side project code and output to bin dir', ->
makeUnrevisionedDirs()
#snip other methods...
makeUnrevisionedDirs = () ->
console.log(": adding unrevisioned directories...")
if not fs.existsSync(web_build_path)
console.log(":: adding #{web_build_path} dir")
fs.mkdirSync(web_build_path)
if not fs.existsSync('logs')
console.log(":: adding logs dir")
fs.mkdirSync('logs')
if not fs.existsSync('bin')
console.log(":: adding bin dir")
fs.mkdirSync('bin')
And get the following error, as if fs was never required:
TypeError: Cannot call method 'existsSync' of undefined
at makeUnrevisionedDirs (C:\fms\Cakefile:25:9, <js>:22:13)
at Object.makeUnrevisionedDirs [as action] (C:\fms\Cakefile:7:2, <js>:11:5)
at helpers.extend.invoke (C:\Users\Mike\AppData\Roaming\npm\node_modules\coffe
e-script\lib\coffee-script\cake.js:45:26)
at Object.exports.run (C:\Users\Mike\AppData\Roaming\npm\node_modules\coffee-s
cript\lib\coffee-script\cake.js:72:21)
at Object.<anonymous> (C:\Users\Mike\AppData\Roaming\npm\node_modules\coffee-s
cript\bin\cake:7:38)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:903:3
why is fs undefined here?

I had this problem and discovered I was running a really old version of node (0.6) - your require is failing quietly, fs must be a newer package that didn't exist.
In the end I installed the n package, upgraded node and all was well
How can I update Node.js and npm to the next versions?
(go right to the bottom of the answer for the 3 commands I used)
Note that I was also running Z shell and needed to tell the shell to rehash its list of executables with the rehash command before it found the newer version of node.

Related

Adding gulp-sass breaks by task runner

I have a basic gulp setup in VS2017 to minify my Javascript. I decided to add gulp-sass (my package.json says I'm on gulp-sass v4.0.1) but it throws this error:
C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:66
let sassMap;
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Work\MyProject\MyProject\gulpfile.js:11:12)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
My gulpfile looks like this:
var gulp = require('gulp');
var concat = require('gulp-concat');
var rename = require('gulp-rename');
var watch = require('gulp-watch');
var uglify = require('gulp-uglify');
var sass = require('gulp-sass');
gulp.task('minify', function () {
gulp.src('src/**/*.js')
.pipe(uglify({ mangle: false }))
.pipe(concat('scripts.min.js'))
.pipe(gulp.dest('Content'));
});
gulp.task('sass', function () {
gulp.src('src/css/**/*.scss')
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(gulp.dest('Content'));
});
gulp.task('watch', function () {
gulp.watch('src/**/*.js', ['minify']);
});
I did some Googling and a simple fix suggested was to add "use strict" to the top of the offending file, in this case index.js:66. However, after doing that I get:
Failed to run "C:\Work\MyProject\MyProject\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
C:\Work\MyProject\MyProject\node_modules\node-sass\lib\binding.js:15
throw new Error(errors.missingBinary());
^
Error: Missing binding C:\Work\MyProject\MyProject\node_modules\node-sass\vendor\win32-x64-47\binding.node
Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 5.x
Found bindings for the following environments:
- Windows 64-bit with Node.js 6.x
This usually happens because your environment has changed since running `npm install`.
Run `npm rebuild node-sass --force` to build the binding for your current environment.
at module.exports (C:\Work\MyProject\MyProject\node_modules\node-sass\lib\binding.js:15:13)
at Object.<anonymous> (C:\Work\MyProject\MyProject\node_modules\node-sass\lib\index.js:14:35)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:163:21)
at Module._compile (module.js:397:26)
I am running Node.js v6. I'm lost as to why what should be a simple process is giving me these errors. What am I doing wrong?
Update:
I ran the following commands suggested in the comments:
npm install node-sass -f
npm rebuild nose-sass
Both ran successfully. However, I'm still getting this error:
Failed to run "C:\Work\MyProject\MyProject\Gulpfile.js"...
cmd.exe /c gulp --tasks-simple
C:\Work\MyProject\MyProject\node_modules\gulp-sass\index.js:66
let sassMap;
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (C:\Work\MyProject\MyProject\gulpfile.js:8:12)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
Update 2:
I was advised to add "use strict"; to the top of my gulpfile.js, but the same error occurs. Here's the file contents:
"use strict";
var gulp = require('gulp');
var sass = require('gulp-sass'); // If I comment this out, I can build
gulp.task('sass', function () {
gulp.src('src/css/**/*.scss')
.pipe(sass({ outputStyle: 'compressed' }))
.pipe(gulp.dest('Content'));
});
Most common issue online appears to be Node.js version of < 6.0, but I'm running v6.11.1.
Update 3: (solved)
I finally found the cause & solution; I've added it as an answer down below for any future readers. Enjoy.
Managed to find the problem so I'm answering my own question for future readers.
Whilst I have node.js v6.11.1 installed, Visual Studio 2017 comes bundled with it's own version of node that it uses by default. Even if you run node -v in the VS2017 shell and it tells you it's running v6.11.1, it's actually - by default - running whatever it finds in .\node_mobules\.bin.
The solution is this:
In VS2017, go "Tools > Options > Projects and Solutions > Web Package Management > External Web Tools".
You'll probably see this:
Add the path to your standalone installation of node (default C:\Program Files\nodejs) and, using the arrows, position it above the .\node_modules\bin version, like this:
Hit OK and either refresh the Task Runner Explorer or restart VS2017. Your gulpfile should now build.
In my case, I had to move $(PATH) above $(VSINSTALLDIR)/Web/External to fix the problem.

How do I include a root CA certifciate in my node JS server?

I'm using npm v 5.7.1 on Mac High Sierra. I just installed this module
npm install ssl-root-cas
(for some reason, adding "-g" didn't work). Then i have this in my JS file
require('ssl-root-cas')
.inject()
.addFile(fs.readFileSync(conf.root_cert));
However, upon executing the above code (specifically the "inject()" line), I get the error
/Users/satishp/Documents/workspace/projA/node_modules/ssl-root-cas/ssl-root-cas.js:3637
var filepaths = filepath.split(/\//g);
^
TypeError: filepath.split is not a function
at Array.module.exports.rootCas.addFile (/Users/satishp/Documents/workspace/projA/node_modules/ssl-root-cas/ssl-root-cas.js:3637:28)
at Object.<anonymous> (/Users/satishp/Documents/workspace/projA/server.js:79:3)
at Module._compile (module.js:662:30)
at Object.Module._extensions..js (module.js:673:10)
at Module.load (module.js:575:32)
at tryModuleLoad (module.js:515:12)
at Function.Module._load (module.js:507:3)
at Function.Module.runMain (module.js:703:10)
at startup (bootstrap_node.js:193:16)
at bootstrap_node.js:660:3
I'm somewhat confused about how to proceed. How do I overcome this error?
You just need to pass the file path for the addFile function, no need to manually load the file content:
require('ssl-root-cas')
.inject()
.addFile(conf.root_cert);

ES 2015 VS 2015 for node.js running task runner explorer

I have a ASP.NET 5 project in VS 2015. I'm setting up my gulp tasks and I am using the gulp-chmod module. This allows me to remove read only properties set by TFS in my copy processes. I've used v1.3 of this module sucessfully before in VS 2015 however the new version v2.0 has been upgraded to use ES 2015 features specifically:
note use of const, let
'use strict';
const through = require('through2');
const deepAssign = require('deep-assign');
const Mode = require('stat-mode');
const defaultMode = 0o777 & (~process.umask());
function normalize(mode) {
let called = false;const through = require('through2');
const deepAssign = require('deep-assign');
...
I get the error:
cmd.exe /c gulp --tasks-simple
<MY_PATH>\node_modules\gulp-chmod\index.js:2
const through = require('through2');
^^^^^
SyntaxError: Use of const in strict mode.
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (<MYPATH>\gulpfile.js:9:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
In the output from Task Runner explorer.
I have the latest version of Node.js installed and off the command line it all works well.
My node path is set to point to ./node_modules/.bin
I checked in the package manager console using node -v and it appears I am using the latest node version.
So how do I control the Node version in use through task manager. I would like it to use ES2015 to compile my modules?
Thanks in advance
It sounds like you're running on an older version of Node.js.
I found this github ticket that suggests adding the following to your gulp script to show what version of node you're running under.
https://github.com/sindresorhus/gulp-imagemin/issues/178
console.log('Node version: ' + process.version);
You can go to Tools > Options > Projects and Solutions > External Web Tools to find out where the version of node that VS is using resides.
This question has a fairly elegant solution of moving your "$(PATH)" entry to the top: Gulp task failing when run from VS 2015 Task Runner explorer, but not from command prompt

AssertionError: path must be a string is thrown when requiring own module

I wanted to create a very minified version of hapi-ninja and came across following problem:
var settings = require('./app/server/config/settings');
var routes = require('./app/server/config/rout');
The first line works as it should an returns my modules. But the second line throws following Exception
AssertionError: path must be a string
at Module.require (module.js:362:3)
at require (module.js:380:17)
at Object.<anonymous> (/home/hknlof/development/mygit/todos/app/server/config/rout.js:8:21)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/hknlof/development/mygit/todos/tryout.js:1:75)
I am running on Node v0.10.25 and hapi-ninja is working fine. When I don't require the rout module it does work. My rout and settings modules look very much the same as in hapi-ninja. I isolated the two require statements in one file. Tried both on their own, still the same result. Can't get my head around this. Tried debugging but the value of the node internal path does never change to the above string. Encoding ist always utf-8.
Thank you vkuchartkin and Tracker1.
So my mistake was that I forgot to require a certain directory in my rout.js
var getController = require(module, '../controller');
was my code. Should have been:
var requireDir = require('require-directory');
var get Controller = requireDir(module, '../controller');
Now I feel a bit stupid

node.js cannot find a module in the same folder

Im trying to use a simple "colors" module to set cli-colors in my logs, nothing special.
Well, i have a module called colors.js in the path ./app/config/colors.js, the content:
var clc = require('cli-color');
var colors = {
ok: clc.cyan,
error: clc.red.bold,
warn: clc.yellowBright,
high: clc.white.bgGreen
};
module.exports = colors;
Simple. Well, when i require it in the server.js (at the root of the project, above of /app) it works fine, but, when i try to use it in the ./app/config/db.js it throws me an error:
Error: Cannot find module './app/config/colors.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/nano/Dev/bears-api/app/config/db.js:3:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
14 Sep 10:21:00 - [nodemon] app crashed - waiting for file changes before starting...
Why if it works in the server.js?
You probably required the module using a relative path.
Relative paths are resolved in relation to the requiring module's location.
Quoting docs
A module prefixed with './' is relative to the file calling require().
That is, circle.js must be in the same directory as foo.js for
require('./circle') to find it.
So if you did a
var whatever = require('./app/config/colors.js');
inside a module located in ./app/config/ then node will look for ./app/config/app/config/colors.js and fail.
If both requiring and required module are in the same directory just use:
var whatever = require('./colors.js');
or even shorter:
var whatever = require('./colors');
The module should be in the "node_modules" folder to access it like you have described.

Resources