Visual Studio NPM Task Runner fails, but running task outside of VS runs fine - node.js

I'm working on bringing a ReactJS app over to a .NET setup. I'm trying to run an NPM script from the task runner (or anywhere within VS.) When I run my build command from the task runner, it fails due to an ES6 arrow function within one of my scripts. However, if I load up cmd.exe or powershell, navigate to the project and run it by hand, it runs perfectly.
Any idea why Visual Studio would be doing this?
The output looks something like this:
> cmd.exe /c npm run build --color=always
> ...cmd1
> ...cmd2
> mocha --reporter spec tools/testSetup.js "Scripts/app/**/*.test.js"
C:\Code\HAL\dev\Test\HALTest\HALReact\tools\testSetup.js:40
Object.keys(document.defaultView).forEach((property) => {
^
SyntaxError: Unexpected token >
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 C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:220:27
at Array.forEach (native)
at Mocha.loadFiles (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:217:14)
at Mocha.run (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\lib\mocha.js:469:10)
at Object.<anonymous> (C:\Code\HAL\dev\Test\HALTest\HALReact\node_modules\mocha\bin\_mocha:404:18)
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:906:3
The file that it errors on basically just sets up JSDOM:
process.env.NODE_ENV = 'test';
// Register babel so that it will transpile ES6 to ES5
// before our tests run.
require('babel-register')();
// Disable webpack-specific features for tests since
// Mocha doesn't know what to do with them.
require.extensions['.css'] = function () {return null;};
require.extensions['.png'] = function () {return null;};
require.extensions['.jpg'] = function () {return null;};
// Configure JSDOM and set global variables
// to simulate a browser environment for tests.
var jsdom = require('jsdom').jsdom;
var exposedProperties = ['window', 'navigator', 'document'];
global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
if (typeof global[property] === 'undefined') {
exposedProperties.push(property);
global[property] = document.defaultView[property];
}
});
global.navigator = {
userAgent: 'node.js'
};
documentRef = document; //eslint-disable-line no-undef
Again, I can run this from the CLI directly, and it will work. It has babel-register invoked. I'm just not sure why it wants to error here. If I swap out the arrow function with a regular function, I get other errors in my tests, so it's like the task runner is not able to find babel, or is losing some environmental setting.

Related

Npm test Yeoman Custom Generator

some days ago i've started to write a custom yeoman generator and i want to make a test for it (so i can use travis ecc) but i have a problem when i lunch npm test.
My generator copy only 4 files, a gulpfile.js and 3 files each in a different folder so the final result is:
Project-folder
|
|----- gulpfile.js
|--------production
---------css/main.css
---------html/index.html
---------js/index.js
I want to test my generator so i write this test,i start to write it from the template test provided by generator-generator
app.js
'use strict';
var path = require('path');
var assert = require('yeoman-assert');
var helpers = require('yeoman-test');
describe('generator-postcss-template:app', function () {
before(function () {
return helpers.run(path.join(__dirname, '../generators/app'))
.withPrompts({someAnswer: true})
.toPromise();
});
it('creates files', function () {
assert.file([
'production/css/styles.css',
'production/html/index.html'
]);
});
});
When i lunch the npm test i have this error
module.js:471
throw err;
^
Error: Cannot find module 'gulp-eslint'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/run/media/giulio/Lavoro/Personal/generator-postcss-template/gulpfile.js:4:14)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
npm ERR! Test failed. See above for more details.
i don't know how fix it,test of generator-generator is the same but check only if dummyfile was created and test pass with 100%
Some one can help me?
-- Update--
after deletion of all modules and reinstall it the error was changed
new error

Main Bower files

In my angularjs app, i try to concat bower file because i have to many lib. I make a task for concat file, here is code
var filter = require('gulp-filter');
var mainBowerFiles = require('gulp-main-bower-files');
var dest = 'dist/scripts';
gulp.task('main-bower-files', function() {
return gulp.src('./bower.json')
.pipe(mainBowerFiles([[filter, ]options][, callback]))
.pipe(gulp.dest('dist/scripts'));
});
After i call this task in nodejs cmd, i get this error in chrome console
.pipe(mainBowerFiles([[filter, ]options][, callback]))
This is error in node cmd
.pipe(mainBowerFiles([[filter, ]options][, callback]))
^^^^^^^
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416: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 Liftoff.handleArguments (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:116:3)
at Liftoff.<anonymous> (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:198:16)
at module.exports (C:\Users\aaa\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\node_modules\flagged-respawn\index.js:17:3)
If someone know solution??? Thnx
i fix this with another library, called BOWER-FILES.
var lib = require('bower-files')();
gulp.task('bower_js', function () {
gulp.src(jsBowerFile)
.pipe(concat('bower_js.js'))
.pipe(gulp.dest('dist/scripts'))
.pipe(ngmin())
.pipe(uglify({mangle: false}))
.pipe(gulp.dest('dist/scripts'));
});

Gulp-Sourcemaps, SyntaxError: Unexpected token >

Gulp / npm noobie here.
I'm trying to use gulp-sourcemaps, and for some reason, it crashes on var sourcemaps = require('sourcemaps').(It crash only when this line's in the file)
gulpfile:
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var concat = require('gulp-concat');
var sourcemaps = require('gulp-sourcemaps');
gulp.task('generateApp', function () {
return gulp.src([some paths...])
.pipe(sourcemaps.init())
.pipe(concat('app.min.js'))
.pipe(uglify())
.pipe(sourcemaps.write())
.pipe(gulp.dest(path...));
});
Error :
C:\Projets\node_modules\strip-bom\index.js:2
module.exports = x => {
^
SyntaxError: Unexpected token >
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> (C:\Projets\node_modules\gulp-sourcemaps\src\init.js:10: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)
Has anyone encounter this type of error?
I tried to google it, without any success.
I just started getting the same error and fixed it by replacing the code in C:\Projects\node_modules\strip-bom\index.js with this:
'use strict';
module.exports = function (x) {
if (typeof x !== 'string') {
throw new TypeError('Expected a string, got ' + typeof x);
}
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
// conversion translates it to FEFF (UTF-16 BOM)
if (x.charCodeAt(0) === 0xFEFF) {
return x.slice(1);
}
return x;
};
Then, I had to run npm rebuild node-sass to get it to work again. It seems to be an issue with an older version of the Strip-bom node module.
For more info, check this out: https://github.com/sindresorhus/strip-bom/commit/e2a3c3b83706ee5baac284f3862d3f6b9e1564e5
UPDATED ANSWER:
This error is caused by using an older version of Node. The Strip-bom module is now using ES2015 (ES6) syntax which requires Node 5.0+. (See Node's ES2015 support list here)
To test your version of Node, run:
node -v
If it's less than 5.0, you'll need to update it. You can download the newest version of Node here:
https://nodejs.org/en/
After installing the new version of Node, I still needed to run npm rebuild node-sass to get Gulp up and running again.
The former answer will still work if you don't want to update your Node version, however, as Louis pointed out, manually editing node module files is not a best-practice.

TypeScript / NodeJS: variable not defined, using internal ref paths for separate files

I have been testing TypeScript with node, all was going well until i tried splitting these out.
Am I forced to use modules ?
I have 2 files, app.ts which has a reference path to the hellofile.tst
/// <reference path="hellofile.ts" />
var testme = new Hello()
console.log(testme.testMe());
and the hellofile.ts which contains
class Hello {
testMe():string {
return "hello";
}
}
Now running the program (i am using webstorm), I get the following error.
/usr/local/bin/node app.js
/
Users/tst/WebstormProjects/NodeJsWithTypescript/app.js:2
var testme = new Hello();
^
ReferenceError: Hello is not defined
at Object.<anonymous> (/Users/tst/WebstormProjects/NodeJsWithTypescript/app.js:2:18)
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:906:3
Process finished with exit code 8
You don't have to use modules. You can compile with --out flag
But external modules with commonjs is highly recommended if targeting nodejs : http://m.youtube.com/watch?v=KDrWLMUY0R0

Error when running Mocha test in nodeJS [duplicate]

This question already has answers here:
Cannot run Mocha with CoffeeScript
(6 answers)
Closed 3 years ago.
I'm trying to get Mocha to run in nodejs and expressjs. My test is as follow:
assert = require 'assert'
request = require 'request'
app = require '../../server'
describe "authentication", ->
describe "GET /login ", ->
body = null
before (done) ->
options =
uri: "http://localhost:3000/login"
request options, (err, response, _body) ->
body = _body
done()
it "has user field", ->
assert.ok /user/.test(body)
# assert.match body, /user/
I have added coffee-script as a dependency in my server.js file:
require('coffee-script');
var express = require('express');
var http = require('http');
var path = require('path');
var app = model.exports = express();
And I have a helper file _helper.js:
require('coffee-script')
I run the command:
mocha test/_helper.js test\apps\authentication-test.coffee
which gives the following error:
(exports, require, module, __filename, __dirname) { assert = require 'assert'
SyntaxError: Unexpected string
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 C:\Program Files\nodejs\node_modules\mocha\lib\mocha.js:172:27
at Array.forEach (native)
at Mocha.loadFiles (C:\Program Files\nodejs\node_modules\mocha\lib\mocha.js:16
9:14)
at Mocha.run (C:\Program Files\nodejs\node_modules\mocha\lib\mocha.js:356:31)
at Object.<anonymous> (C:\Program Files\nodejs\node_modules\mocha\bin\_mocha:3
59:16)
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:902:3
Anyone sees what I'm missing ?
If you're using CoffeeScript 1.7, you'll need to use require the coffee-script/register module to be able to require and compile .coffee files on the fly (see changelog). Try running Mocha with:
mocha --compilers coffee:coffee-script/register
Or modifying your Mocha helper file:
require('coffee-script/register')
Mocha doesn't check for Coffeescript files by default. You have to specified a compiler option:
mocha --compilers coffee:coffee-script
or add
--compilers coffee:coffee-script
in your mocha.opts file
I needed two changes to get CoffeeScript to work with Mocha:
--require coffee-script/register
--compilers coffee:coffee-script/register

Resources