No changes made to lang.js file since months ago but suddenly syntax error when trying to preview locally and can't deploy changes.
Not sure why this is happening - could somebody please kindly help? Many thanks!
Disclosure/warning: I have 0 programming knowledge.
MacBook:pty-bus Ying$ node index.js
/Users/Ying/Desktop/pty-bus/lang.js:19
...req.lang.marketing.global,
^^^
SyntaxError: Unexpected token ...
at Object.exports.runInThisContext (vm.js:78:16)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/Ying/Desktop/pty-bus/index.js:20:36)
at Module._compile (module.js:571:32)
lang.js
var path = require('path');
function langMiddleware(req, res, next){
let lang = req.query.lang || 'en';
if(['es', 'en'].indexOf(lang) == -1) lang = 'en'
fs.readFile(path.join(__dirname, 'lang', lang + '.json'), 'utf8', function(err, data){
req.lang = JSON.parse(data);
req.langCode = lang;
next();
})
};
function render(req, res, template, vars={}){
let renderObj = {
...req.lang.marketing.global,
title: req.lang.titles[template],
site_title: req.lang.site_title,
site_description: req.lang.site_description,
...(req.lang.marketing[template] || {}),
lang: {...(req.lang.marketing.global.language || {})},
langCode: req.langCode,
...vars,
path: req.path
};
res.render(template, renderObj);
}
module.exports = { langMiddleware, render }
It looks like your ability to use spread operator has been removed. I would guess you have recently changed your node version, or edited either your babelrc or webpack config. Either way, I don't think it's anything wrong with your current code in that file.
Node.js v7.4.0 has V8 5.4 which supports ...operator for arrays, but not for objects. You need at least V8 6.0, i.e. the last Node.js 8 branch version will do (v8.15.0).
Tt looks like node v7.4.0 not support spread operator - change let renderObj = {...} to
let renderObj = {
title: req.lang.titles[template],
site_title: req.lang.site_title,
site_description: req.lang.site_description,
langCode: req.langCode,
path: req.path
};
Object.assign(renderObj,
req.lang.marketing.global,
(req.lang.marketing[template] || {}),
vars
);
renderObj.lang = Object.assign({}, (req.lang.marketing.global.language || {}));
Related
Im trying to resolve the issue I encounter when Im trying to update jest in my package.json
jest 26.6.3 → 27.0.1
Im receiving error
TypeError: require(...).createTransformer is not a function
at Object.<anonymous> (/local/repo/elukchm/monorepo/packages/edf/jestPreprocess.js:34:40)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
The code mentioned in files:
jestPreprocess.js
const babelOptions = {
...
};
module.exports = require("babel-jest").createTransformer(babelOptions);
Inside "babel-jest"
const createTransformer = userOptions => {
var _inputOptions$plugins, _inputOptions$presets;
const inputOptions =
userOptions !== null && userOptions !== void 0 ? userOptions : {};
const options = {
...
};
Could you please take a look and tell what should I change? Thank you in advance.
babel-jest#27 switched to ESM, so require is now getting the whole exported scope rather than just the default. Super simple to fix:
module.exports = require("babel-jest").createTransformer(babelOptions);
-->
module.exports = require("babel-jest").default.createTransformer(babelOptions);
I am beginner in node.js.
I am trying to call a very basic addition module saved in C:\wks\guru99 with file name: Node_03_addition.js. The calling application is also saved in the same location with the name: app.js
Addition Module:
var exports = module.exports = {};
exports.addNumber=function(a,b)
{
return a+b;
};
Application File:
var Addition = require('/.Node_03_addition.js');
console.log(Addition.addNumber(1,2));
when I run the application in cmd using node app.js
I am receiving the error that Module is not found. Can someone please help me with where I am going wrong here.
c:\wks\guru99>node app.js
internal/modules/cjs/loader.js:969
throw err;
^
Error: Cannot find module '/.Node_03_addition.js'
Require stack:
- c:\wks\guru99\app.js
[90m at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:842:27)[39m
[90m at Module.require (internal/modules/cjs/loader.js:1026:19)[39m
[90m at require (internal/modules/cjs/helpers.js:72:18)[39m
at Object.<anonymous> (c:\wks\guru99\app.js:1:16)
[90m at Module._compile (internal/modules/cjs/loader.js:1138:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:986:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:879:14)[39m
[90m at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)[39m {
code: [32m'MODULE_NOT_FOUND'[39m,
requireStack: [ [32m'c:\\wks\\guru99\\app.js'[39m ]
}
SUGGESTION
// Node_03_addition.js
// Declare your function
const addNumber = function(a,b) {
return a + b;
};
// Export your function
module.exports = {
addNumber
};
// Application-File.js
// Import your module
const Addition = require('./Node_03_addition');
// Use it
console.log(Addition.addNumber(1,2)); // 3
Addition Module:
var exports = module.exports = {};
exports.addNumber=function(a,b)
{
return a+b;
};
Application File:
var Addition = require('./Node_03_addition.js');
console.log(Addition.addNumber(1,2));
I solved this during the writing of the question and have provided my answer below since it was a bit tricky to work out. I am happy to hear any better or alternative answers.
I have an Angular OpenLayers 6 geomapping app. Being Angular I use Typescript and it transpiles and runs fine. And also being Angular it uses ng test to do the testing. All tests run fine.
However I use mocha + chai for testing in the IDE (IntelliJ) since I don't require UI testing for the mathematical work I'm currently performing (ng test runs the UI tests if and when I need that. But in the IDE I select the tests to run). Testing this way worked fine until I added a new test that creates a new instance of a class that imports GeoJSON:
import GeoJSON from 'ol/format/GeoJSON';
That test fails (in mocha) with:
/home/smx9b6/dev/ng-eow/node_modules/ol/format/GeoJSON.js:17
import { assert } from '../asserts.js';
^
SyntaxError: Unexpected token {
Looking at the GeoJSON.js file it seems to have UMD module format (i think this is UMD):
/**
* #module ol/format/GeoJSON
*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { assert } from '../asserts.js';
import Feature from '../Feature.js';
var GeoJSON = /** #class */ (function (_super) {
__extends(GeoJSON, _super);
/**
* #param {Options=} opt_options Options.
*/
function GeoJSON(opt_options) {
...
}
GeoJSON.prototype.writeGeometryObject = function (geometry, opt_options) {
return writeGeometry(geometry, this.adaptOptions(opt_options));
};
return GeoJSON;
}(JSONFeature));
And others, such as turf.js use the ES6 module format. eg. line-to-polygon:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var bbox_1 = require("#turf/bbox");
var invariant_1 = require("#turf/invariant");
var helpers_1 = require("#turf/helpers");
...
function lineToPolygon(lines, options) {
if (options === void 0) { options = {}; }
...
}
...
exports.default = lineToPolygon;
Mocha can't deal with this but Angular can - I don't know why. I run mocha with (as reported by IntelliJ when running the test - full paths removed):
node node_modules/mocha/bin/_mocha --require ts-node/register --ui bdd --reporter mochaIntellijReporter.js
src/app/geometry-ops.spec.ts --grep "^geometry-ops centroid "
I have commonjs set as the module type:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": [],
"resolveJsonModule": true,
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
},
...
}
The error in full is:
/home/smx9b6/dev/ng-eow/node_modules/ol/format/GeoJSON.js:17
import { assert } from '../asserts.js';
^
SyntaxError: Unexpected token {
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/smx9b6/dev/ng-eow/src/app/layers-geometries.ts:4:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/home/smx9b6/dev/ng-eow/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/home/smx9b6/dev/ng-eow/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/smx9b6/dev/ng-eow/src/app/geometry-ops.spec.ts:13:1)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Module.m._compile (/home/smx9b6/dev/ng-eow/node_modules/ts-node/src/index.ts:439:23)
at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Object.require.extensions.(anonymous function) [as .ts] (/home/smx9b6/dev/ng-eow/node_modules/ts-node/src/index.ts:442:12)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at /home/smx9b6/dev/ng-eow/node_modules/mocha/lib/mocha.js:334:36
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/mocha.js:331:14)
at Mocha.run (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/mocha.js:809:10)
at Object.exports.singleRun (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/cli/run-helpers.js:108:16)
at exports.runMocha (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/cli/run-helpers.js:142:13)
at Object.exports.handler.argv [as handler] (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/cli/run.js:292:3)
at Object.runCommand (/home/smx9b6/dev/ng-eow/node_modules/mocha/node_modules/yargs/lib/command.js:242:26)
at Object.parseArgs [as _parseArgs] (/home/smx9b6/dev/ng-eow/node_modules/mocha/node_modules/yargs/yargs.js:1087:28)
at Object.parse (/home/smx9b6/dev/ng-eow/node_modules/mocha/node_modules/yargs/yargs.js:566:25)
at Object.exports.main (/home/smx9b6/dev/ng-eow/node_modules/mocha/lib/cli/cli.js:68:6)
at Object.<anonymous> (/home/smx9b6/dev/ng-eow/node_modules/mocha/bin/_mocha:10:23)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
I worked out and provide an answer below. However I would still like to hear any feedback on this. Such as should OpenLayers change their module format? (I'm still getting my head around all the different module formats).
The answer is to add --require esm --require jsdom-global/register to the node / mocha call.
The esm is to specify ES6 as the module format and jsdom-global is to define the DOM in non-browser environments (specifically to define document).
The full command is:
node node_modules/mocha/bin/_mocha --ui bdd \
--require ts-node/register \
--require esm \
--require jsdom-global/register \
--reporter landing \
src/app/geometry-ops.spec.ts --grep "^geometry-ops centroid "
(I added spaces in the command since it irks me when I have to scroll right to see such things).
I am happy to hear any better or alternative answers.
This code snippet seems not work, why?
var acorn = require("./node_modules/acorn/dist/acorn_loose");
/* Extend default Acorn's methods.*/
acorn.pluginsLoose.testPlug = function(looseParser) {
looseParser.extend('finishNode', function(nextMethod) {
return function(node, type) {
console.log(node, type);
return nextMethod.call(this, node, type);
};
});
};
var res = acorn.parse_dammit("alert(1", {ecmaVersion: 6, plugins: {testPlug: true}});
console.log(res)
Give us next error:
...\node_modules\acorn\dist\acorn.js:508
if (!plugin) throw new Error("Plugin '" + name + "' not found")
^
Error: Plugin 'testPlug' not found
at Parser.loadPlugins (...\node_modules\acorn\dist\acorn.js:508:26)
at new Parser (...\node_modules\acorn\dist\acorn.js:444:10)
at Object.tokenizer (...\node_modules\acorn\dist\acorn.js:3115:12)
at new LooseParser (...\node_modules\acorn\dist\acorn_loose.js:15:23)
at Object.parse_dammit (...\node_modules\acorn\dist\acorn_loose.js:1258:13)
at Object.<anonymous> (...\plugins_poc.js:16:17)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
Is it bug or I do something wrong?
Enviroment: idea intellij 16.
Update:
Chrome give the similar error.
var res = acorn.parse_dammit("alert(1", {ecmaVersion: 6, plugins: {testPlug: true}});
You need to use pluginsLoose instead of plugins here as well!
I'm trying to create a simple application to compile mustache templates into static pages server side, here's what I've got so far:
var view = {
title: "Joe",
calc: function () {
return 2+4;
}
};
var mustache = require("mustache");
var template = require("./home.template");
var output = mustache.to_html(template, view);
console.log(output);
And my template looks like:
{{title}} spend {{calc}}
Any suggestions as to what is causing this to fail?
Here is the complete error message:
home.template:1
} spend {{calc}}
^
module.js:437
var compiledWrapper = runInThisContext(wrapper, filename, true);
^
SyntaxError: Unexpected token {
at Module._compile (module.js:437:25)
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> (/Users/MorehouseJ09/Documents/production_development/mustache/current/compiler.js:12:16)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
Any help would be great!
Use fs.readFile() to read your template in as a string. Require won't work unless it's requiring javascript code, not mustache code.
http://nodejs.org/api/fs.html#fs_fs_readfile_filename_encoding_callback
Edit
See if this works...
var mustache = require("mustache");
var fs = require("fs");
var view = {
title: "Joe",
calc: function () {
return 2+4;
}
};
fs.readFile('./home.template', 'utf-8', function (err, data) {
if (err) throw err;
var output = mustache.to_html(data, view);
console.log(output);
});