How to use System.import() correctly? - node.js

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.

Related

googleapis-common throwing error in UUID dependency

I'm trying to get a very basic oauth example to work in a node.js app with express and googleapis. Upon running the application it throws a TypeError inside the UUID dependency which is included with the googleapis-common module. I'm getting a bit frustrated at this point because I have not been able to find any additional information about this to allow me to resolve it myself.
Take a look at the screenshot below for the specifics:
Here it is in text if that makes things easier:
Exception has occurred: TypeError: Cannot assign to read only property 'name' of function 'function generateUUID(value, namespace, buf, offset) {
if (typeof value === 'string') {
value = strin...<omitted>... }'
at _default (C:\Users\ficar\OneDrive\Desktop\Frontend\node_modules\googleapis-common\node_modules\uuid\dist\v35.js:71:23)
at Object.<anonymous> (C:\Users\ficar\OneDrive\Desktop\Frontend\node_modules\googleapis-common\node_modules\uuid\dist\v3.js:14:27)
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)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\ficar\OneDrive\Desktop\Frontend\node_modules\googleapis-common\node_modules\uuid\dist\index.js:63:34)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
The file this is being thrown in is called "v35.js".
My initial thought is that I must be missing some additional library that interprets the logic throwing the error differently. Eager to learn more about this and find a resolution.
Looks like this is how the uuid module works
node_modules/uuid/dist/v35.js
function _default(name, version, hashfunc) {
function generateUUID(value, namespace, buf, offset) {
...
} // Function#name is not settable on some platforms (#270)
try {
generateUUID.name = name; // eslint-disable-next-line no-empty
} catch (err) {} // For CommonJS default export support
...
Authors warn (comment on line 4), that name property may not be settable and bypass it with empty catch

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

proper way of using es6 classes in a nodejs project

I'd like to be able to use the cool es6 classes feature of nodejs 4.1.2
I created the following project:
a.js:
class a {
constructor(test) {
a.test=test;
}
}
index.js:
require('./a.js');
var b = new a(5);
as you can see I create a simple class that it's constructor gets a parameter. and in my include i require that class and create a new object based on that class. pretty simple.. but still i'm getting the following error:
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:413:25)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/ufk/work-projects/bingo/server/bingo-tiny/index.js:1:63)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
any ideas why ?
Or you can run like this:
node --use_strict index.js
i'm still confused about why 'use strict' is needed, but this is the code that works:
index.js:
"use strict";
var a = require('./a.js');
var b = new a(5);
a.js:
"use strict";
class a {
constructor(test) {
a.test=test;
}
}
module.exports=a;

Do I need to 'require' a file if it is already required by another file

I have 3 files
B requires A
C requires (B and A)
In this scenario does C needs to require A?
Doubt is because B is already requires A and when C requires B it should also requires A.
So I just want to be sure whether c needs to explicitly require A or not.
Upon the same situation, when I required explicitly A :
I found following error :
module.js:340
throw err;
^
Error: Cannot find module 'A'
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> (/Users/topi/controller.js:3: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)
at Module.require (module.js:364:17)
Why this is so?
In most cases, yes. Each module that needs a reference to another module needs to require() it independently.
Why this is so?
When you use:
var a = require('./a');
This declares a as local variable to the current module, which are all evaluated within their own closure. This is why modules have exports to allow them to specify their "public" API.
It can also be suggested that this is so that each module must list its dependencies, such as c depending on a and b.
// c.js
var a = require('./a');
var b = require('./b');
But, you could use exports to pass-along one module through another -- e.g. a through b:
// b.js
var a = exports.a = require('./a');
// c.js
var b = require('./b');
console.log(b.a);
And, technically, you could also attach a to global. Though, generally you shouldn't. Doing so is typically considered a code smell.
yes you need its work as a individual the class system of node.js is different then other server side or oop languages .
var a = require('./a');
var b = require('./b');
Have to do this

Browserify/Jadeify: require maps no longer supported

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?

Resources