Browserify/Jadeify: require maps no longer supported - node.js

I am trying to use browserify/jadeify on my ExpressJS project:
server.coffee
browserify = require "browserify"
jadeify = require "jadeify"
bundle = browserify()
.use(jadeify(__dirname + "/client/jade/templates"))
.addEntry(__dirname + "/public/js/app/main.js")
app.use bundle
main.coffee (client side)
$ = require "jquery"
jadeify = require "jadeify"
html = jadeify "test.jade",
title: "Hello World"
console.log html
What I got is:
Error: require maps no longer supported
at Function.Wrap.require (/labs/Projects/Nodebook/node_modules/browserify/lib/wrap.js:410:15)
at Function.module.exports.Object.keys.forEach.self.(anonymous function) [as require] (/labs/Projects/Nodebook/node_modules/browserify/index.js:158:28)
at module.exports (/labs/Projects/Nodebook/node_modules/jadeify/index.js:53:16)
at Function.Wrap.use (/labs/Projects/Nodebook/node_modules/browserify/lib/wrap.js:105:5)
at Function.module.exports.Object.keys.forEach.self.(anonymous function) [as use] (/labs/Projects/Nodebook/node_modules/browserify/index.js:158:28)
at Object.<anonymous> (/labs/Projects/Nodebook/server.coffee:55:25)
at Object.<anonymous> (/labs/Projects/Nodebook/server.coffee:63:4)
at Module._compile (module.js:449:26)
at Object.exports.run (/usr/lib/node_modules/coffee-script/lib/coffee-script/coffee-script.js:83:25)
at compileScript (/usr/lib/node_modules/coffee-script/lib/coffee-script/command.js:177:29)
Seems like the server does not want to start. How do I get this to work? I think the code is mostly the same as in the documentation. Whats wrong and how do I fix this?

Related

How do I make generator functions work with Hapi JS

I installed various node.js versions with nvm and tried -harmony flag to make generator functions work with yield keyword but I'm getting all kinds of errors when the server starts. One of them is below:
/home/ubuntu/workspace/node_modules/hapi/node_modules/joi/lib/object.js:310
throw castErr;
^
TypeError: Cannot read property '_items' of undefined
at /home/ubuntu/workspace/node_modules/hapi/node_modules/topo/lib/index.js:39:22
at Array.forEach (native)
at internals.Topo.add (/home/ubuntu/workspace/node_modules/hapi/node_modules/topo/lib/index.js:36:24)
at internals.Object.keys (/home/ubuntu/workspace/node_modules/hapi/node_modules/joi/lib/object.js:301:18)
at internals.root.root.object (/home/ubuntu/workspace/node_modules/hapi/node_modules/joi/lib/index.js:71:72)
at Object.<anonymous> (/home/ubuntu/workspace/node_modules/hapi/node_modules/catbox/lib/policy.js:255:24)
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)
Is there something special I need to do to use the yield keyword?
UPDATE: I updated nodejs to 5.4.1 and the errors are gone. But I can't use the yield function.
Here's a code:
var nodes = yield Db.node.find({ type: 'root' });
return reply.success(nodes);
And here's the error I get:
var nodes = yield Db.node.find({ type: 'root' });
^^
SyntaxError: Unexpected identifier
You'll need a plugin for hapi that allows you to run a generator as handler. Something like https://github.com/ide/hapi-async-handler

How to use System.import() correctly?

I use the jspm in my project.
But I need the server side nodejs file to execute some instruction.
For example, I need to use the lodash and found the guide in the https://github.com/systemjs/systemjs
var System = require('jspm').Loader();
System.import('lodash').then(function (_) { console.log(_); });
However, I want to use the lodash globally.
Just like
var _ = System.import('lodash');
var myArr = _.map([1, 2, 3], function(n) { return n * 3; });
It will show
TypeError: _.map is not a function
at Object. (/Users/joyfeel/javascript/jspm-test/index.js:49:16)
at Module._compile (module.js:435:26)
at normalLoader (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:199:5)
at Object.require.extensions.(anonymous function) [as .js] (/usr/local/lib/node_modules/babel/node_modules/babel-core/lib/api/register/node.js:216:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at Object. (/usr/local/lib/node_modules/babel/lib/_babel-node.js:144:25)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
Why does the lodash only be used in .then scope?
Could anyone tell me how to figure it out? Suppose we want to System.import other modules and use it?
_ can only be accessed in the scope of then because System.import always returns a Promise.
Therefore you have to wait for the Promise to be resolved before you can access its result.
Anyway i would not recommend you to use lodash globally.
But when you really want to use _ globally you can do something like:
System.import('lodash').then(function(_) {
GLOBAL._ = _;
});
Still you have to make sure that all code that uses GLOBAL._ waits till the Promise from the lodash import is resolved.
But again: i would discourage you doing it that way but recommend that you import lodash in every module that needs it.

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

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

Node.js application throwing error on registring module

I am following Steven Sanderson's videos here to get started with NodeJS. I have installed EJS and ejs-middleware modules. The server.js is like following:
var express=require('express'),
app = express(),
ejsMiddleware = require('ejs-middleware');
app.use(ejsMiddleware(__dirname + '/static', 'html', app));
But it is throwing exception on this like:
app.use(ejsMiddleware(__dirname + '/static', 'html', app));
The exception is:
Application has thrown an uncaught exception and is terminated:
TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'register'
at C:\Users\...Inventify\node_modules\ejs-middleware\ejs-middleware.js:9:23
at Object.<anonymous> (C:\Users\...Inventify\server.js:8:9)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at Object.<anonymous> (C:\Program Files (x86)\iisnode-dev\release\x86\interceptor.js:211:1)
at Module._compile (module.js:446:26)
I'm not able to get any head or tail of it. Please help me.
EDIT:- Made the following changes according to this answer by #Peter Lyons:
old
registerInApp.register('.' + extension, ejs);
new
registerInApp.engine('.' + extension, require(ejs));
But now getting following exception:
TypeError: Object #<Object> has no method 'substring'
at Function._resolveLookupPaths (module.js:235:23)
at Function._resolveFilename (module.js:327:31)
at Function._load (module.js:279:25)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at C:\Users\...Inventify\node_modules\ejs-middleware\ejs-middleware.js:10:47
at Object.<anonymous> (C:\Users\...Inventify\server.js:12:9)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
It looks like ejs-middleware needs to be updated to call app.engine instead of app.register to work with express 3.0. As a workaround, don't pass the app argument to the ejsMiddleware function and instead manually register it yourself:
app.engine('.html', require('ejs').renderFile);
I updated that. It's actually straight from the express.js documentation for app.engine. From what I can tell (I don't use ejs personally), it looks like you don't need ejs-middleware at all and can just use visionmedia/ejs and be done with it.

Resources