Gulp-Sourcemaps, SyntaxError: Unexpected token > - node.js

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.

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

Visual Studio NPM Task Runner fails, but running task outside of VS runs fine

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.

Jest with 'npm test' fails with "Use of const in strict mode"

I am just trying to use Jest as unit testing framework.
I done and example from official ReactJS side.
function sum(value1, value2) {
return value1 + value2;
}
module.exports = sum;
----------------------------------------------------------------
jest.dontMock('../sum');
describe('sum', function() {
it('adds 1 + 2 to equal 3', function() {
var sum = require('../sum');
expect(sum(1, 2)).toBe(3);
});
});
But when I run npm tests I got next error:
/home/app.local/node_modules/jest-cli/bin/jest.js:12
const fs = require('fs');
^^^^^
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 Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
npm ERR! Test failed. See above for more details.
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
What is going wrong ?
If you read the documentation of the link that you posted, you can see that it says the following:
Jest uses ES2015 features and requires a Node.js version of at least 4.0.0 to run.
You are on version 0.10.25 which is probably why you are having issues. Upgrading your version to >= 4.0.0 should fix this.

Koa-pg Can't find module pg

Hey so I'm trying to get Nodejs Koa to talk to postgres using the Koa-pg module, but I keep getting a 'Can't find module pg' error.
I've tried to follow the koa-pg examples, but have come up short...so any advice would on how to progress would be appreciated.
If created my app.js file as follows:
var koa = require('koa');
var route = require('koa-route');
var koaPg = require('koa-pg');
var roads = require('./controllers/roads');
var app = module.exports = koa();
app.use(route.get('/roads/bbox/', roads.bbox));
app.listen(3000);
console.log('listening on port 3000');
And then created my controller file as follows:
var credentials = require('../credentials.js');
var environment = credentials.dev;
app.use(koaPg('postgres://' + environment.user + '#' + environment.host + ':' + environment.port + '/' + environment.database))
module.exports.bbox = function * bbox(next) {
var result = yield this.koaPg.db.client.queryPromise('SELECT now()')
console.log('result: ', result)
this.body = result.rows[0].now.toISOString()
};
But I'm getting the following error:
module.js:338
throw err;
^
Error: Cannot find module 'pg'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (c:\Users\User\Documents\restful_koa\node_modul
es\koa-pg\index.js:12:27)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
This is only a personal project but I'd love to understand where i'm going wrong.
Cheers
As mentioned in the comments:
You need to install the pg module via npm install pg or npm install pg --save if you want to save it to package.json.
The reason you need to do this is koa-pg has co-pg as a dependency so when you install the former the latter is also installed. But pg is not a dependency of co-pg and that is why you have to install it separately.

Node.js with ExpressJS error: Cannot read property 'prototype' of undefined

Running node.js v0.10.2 and express v3.1.1 (latest at this time) and getting this error:
/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:10
window.XMLHttpRequest.prototype.withCredentials = false;
^
TypeError: Cannot read property 'prototype' of undefined
at create (/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:10:26)
at /root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:9503:18
at Object.<anonymous> (/root/dmr-addresses/node_modules/jquery/lib/node-jquery.js:9505:2)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/root/dmr-addresses/address/log.js:1:71)
line 1 of log.js is:
var $ = require('jquery');
I've tried running npm install jquery but it has not fixed the problem.
Check this:
Same error here...
I don't know what I'm doing, but I changed the node-jquery.js fourth-fifth row's and it's start working :)
old:
if(window == null ) {
window = require('jsdom').jsdom().createWindow();
new:
if(!window || !window.document) {
window = require('jsdom').createWindow();
window.document = require('jsdom').jsdom();
You don't actually have a prototype object in Node server code, it's all stored in the much nicer __proto__ object and you should be using Object.create/defineProperty.
What exactly are you trying to do? Run an ajax query with Node? If so, you should be using Nodes http.request
An example could be:
require('request').post({
"uri" : "http://example.com/",
"headers" : {
'content-type': 'application/json'
},
"body" : "hello=world"
},
function(e,r,b){
// e = errors, r = response and b = returned body
console.log(b,r.statusCode));
});
Looks like this is an issue with the jsdom module that node-jquery depends on. It appears that this is a known issue, and that it has been fixed, but not published to npm yet.
Check it out: https://github.com/coolaj86/node-jquery/issues/52

Resources