I would like to use this node.js module https://github.com/mlin/node-assert-type
Based on documentation, to declare the module;
var ty = require("assert-type"); //https://github.com/mlin/node-assert-type
var T = ty.Assert;
In actual practice, this does not work. Some coffeescript error appears.
I have to make the following workaround;
var cs = require("coffee-script/register");//this line needed to require("assert-type")
var ty = require("assert-type"); //https://github.com/mlin/node-assert-type
var T = ty.Assert;
To use this module, I am forced to install coffeescript with npm install -g coffeescript.
Is there some way to omit the line var cs = require("coffee-script/register");? After all, the module itself is using coffeescript locally. Am I doing it the right way?
Is it a normal practice to add a line to load coffeescript for node.js modules which use coffee-script?
It is not normal practice. I mean, it would be inevitable that coffeescript gets installed since it is a dependency but the user of the module should not worry about it.
I just took a quick look at the source code of the assert-type and this is what I found:
the project is 3 years old. That's a lot!
the package.json is listing coffeescript as a dependency BUT it is using latest instead of locking a version of coffeescript which is a terrible practice.
My guess is that what it changed was coffeescript module, that instead of needing require('coffeescript') you now need require('coffeescript/register'). (Take a look at the index.js in the repo)
Based on that I'd say it is fine that you write that line. A better option would be to make the changes in the node-assert-type repo and submit a PR with the fixes for #2 and #3.
Hope that helps.
Related
I'm attempting to use a jQuery plugin (in this case 'fitText') in an Ember project.
In the ember-cli-build.js (that replaced the Brocofile recently...) - I import the dependency (which I have already installed with bower).
/* global require, module */
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
// Add options here
});
app.import('bower_components/fittext/fittext.js');
return app.toTree();
};
In my console, I can type in 'fitText' and I DO get the object back. So, it's global now, and I should be able to use it anywhere. BUT "Uncaught TypeError: $(...).fitText is not a function"
#Tom Netzband pointed out that the bower version of fitText was the one that #adacio maintains and is vanilla JS. I've since switched it out for a app.import('vendor/jquery.fittext.js'); (the JS is really only 20 lines or so)
Previously I needed to import it in the controller or route where I want to use it - but the docs don't reference that anymore. In the docs with moment.js as the example, it seems to become a global.
I've put it in the ready hook in app.js - next to similar jQuery things that work fine... but no luck. In this case I only want to affect a few words on the index.hbs / route
UPDATE
This has really just become another question now. I thought it was still the same problem but it's not. So awarding answer and asking a new one. Thanks.
It looks like bower install fittext doesn't install the jQuery plugin version of fitText, it only installs the plain js version of it.
If you look at your bower_components/fittext/fittext.js file, you'll see jQuery isn't passed in or used, and the example from the example.html file shows usage as being:
fitText(document.getElementById('fittext'), 1.2)
So if you want the jQuery version you might have to download it straight from github and throw it in your vendor folder and include it outside of bower.
UPDATE
Looks like the version registered with bower is a forked version from the jQuery version where jQuery has been removed: https://github.com/adactio/FitText.js
There is in fact a Bower version of the jQuery version of the library.
bower install FitText.js --save
I think Dave Rupert has a jQuery version in his crates as well.
$ bower install jquery-fittext --save
I had a small Ember-App in just one single HTML File and everything was working fine, but since it was getting quite big I started to port it to Ember-CLI. Most things worked fine to port but I'm still struggling to add JStorage:
https://github.com/andris9/jStorage
I'm not really sure how to start as its a plain JS Lib, that I normally would just drop into the code somewhere before I use it. Now with all the modules I'm totally lost where to even start looking for how to do it.
Can anyone point me in the right direction how to use such JS Libs?
I found a few Topics around it but did not get to any working path.
Here is how I used it previously:
App.Something = Ember.Object.extend({
init: function() {
var stored = $.jStorage.get('something');
...
}
});
Well, after a lot of smoke out of my ears i got it to work:
add json2
$ bower install --save json2
This does not work out of box because there is no tags in the repo.
Edit the bower.js File to set the version to "master". Then it works.
add jStorage
$ bower install --save jstorage
Install the dependencies (not sure if necessary but i did it)
$ ember install:bower
Then the files are available in the folder bower_components, which is ignored by git and apparently my editor (atom.io) too.
Import the files in the Brockfile.js like this
...
app.import('bower_components/json2/json2.js');
app.import('bower_components/jstorage/jstorage.js');
module.exports = app.toTree();
Use it, prefixing $ with Ember not to upset JSHint (would work without)
...
var stored = Ember.$.jStorage.get(id);
...
I know I can install underscore using npm but that's not what I can do in my work environment. I need to be able to download the Underscore.js library and then make it "browserify-compatible".
So let's assume Underscore.js looks something like this:
(function() {
var root = this;
// Rest of the code
}.call(this));
I downloaded that file on my hard drive and saved it as under.js.
My file that requires underscore looks like this:
var underscore = require("./under");
console.log(underscore);
And then I run browserify from the cli.
I have an HTML page called test.html and basically all it does is load the generated bundle.js.
However, the console.log(underscore) line fails - says that underscore is undefined.
What have I tried?
Obviously I added module.exports to the first line - right before the function definition in under.js, and that's how I got the error mentioned above. I also tried the method from this answer , still got the same error.
So, how would I use Browserify to load libraries such as Underscore.js or Backbone without using npm-installed modules?
That's because browserify does not add variables to the global scope. The version you download is identical to the version that you install via NPM.
You need to explicitly attach it to the window to export it to the top level scope.
If you create a file called "expose_underscore.js" and put this in it:
var _ = require('./under');
window._ = _;
Will do it, followed by: browserify expose_underscore.js > bundle.js and then add bundle.js as a <script> tag you will be able to do the following in your console:
HOWEVER, you shouldn't do this if you're using browserify. The point behind it (and Node's version of commonJS) is that you explicitly require it everywhere you need it. So every file you have that needs underscore should import it to a local variable.
Don't worry -- you will still only have one copy loaded.
I typically add my vendor libs like Underscore as script tags. Underscore will attach itself to the global scope, so then you don't need to require it anywhere to use it.
If you do want to use it in a Browserified fashion, verify that you have the correct path in your require statement (browserify requires are relative paths) and move the module.exports statement to the end of the file.
Right now I have a unit testing build environment using node.js and nodeunit. Very happy with these but now I need TCO. I know that TCO has been added into the ES6 standard, but I don't know how to allow it for use with my project. Tried the Harmony flag, but couldn't get it to work with nodeunit. Any help?
Got the idea for using Harmony here:
Node.js tail-call optimization: possible or not?
I like the way these guys think, but I can't do the first answer because then other working on the project would also be forced to change their nodeunit.cmd files (which may screw up other projects they are working on) and the second answer doesn't appear to work:
NodeUnit enable harmony features
From what I understand, it looks like you want to write unit tests in ES5 using nodeunit to test your code written in ES6.
If I understood well, then you can check out this post which shows how to achieve that.
This solution requires you to npm install traceur and then you can require() your ES6 module from within your tests like so :
var traceur = require('traceur');
traceur.require.makeDefault(function(filename) {
return filename.indexOf('node_modules') === -1; // Don't parse node modules
});
var myModule = require('./../path/to/my/module.js');
module.exports = {
// ... tests ...
};
Now you should be able to run that with nodeunit.
I'd like to use the musicjson.js package that helps to convert musicXML files into json notation, looking if it's a good way to import for example exported musicXML Finale scores into a browser playing with the Fermata/VexFlow class.
https://github.com/saebekassebil/musicjson
The thing is that this module works with require (calling for
nodes packages like fs) and I'm just a newbee in requirejs...Even if I spent few time in understanding the tutorial in the website, I don't still get how to solve this kind of basic problem when the dependencies of my musicjson.js need to be called like :
var xmldom = require('flat-xmldom'),
fs = require('fs'),
path = require('path'),
util = require('util');
My index.php page does the classic require call:
<!DOCTYPE html>
<head>
<!-- javascript head -->
<!-- REQUIRE -->
<script data-main="scripts/main" src="bower_components/requirejs/require.js"></script>
</head>
<body>
</body>
</html>
In my scripts/main.js, I'd like to do simply what it is told from musicjon :
var music = require('musicjson');
music.musicJSON(xml, function(err, json) {
// Do something with the MusicJSON data
});
I putted also, in the same directory scripts/, the flat-xmldom folder, fs.js, path.js, util.js
When I do this, I've just obtain this classic error of :
*Error: Module name "musicjson" has not been loaded yet for context: _. Use require([])*
...That looks like a common error referenced in the requirejs website,
but if I try things that I guess it should be written, I get a bit lost to determine where is the fundamental conceptual mistake here :
require.config({
baseUrl: '/scripts/',
paths: {
flatxmldom:'./flat-xmldom/__package__',
fs: 'fs',
path:'path',
util:'util',
musicjson: 'musicjson'
}
});
require(['flatxmldom','fs','path','util','musicjson'],function(flatxmldom,fs,path,util,musicjson){})
Error returned in this case for example :
*Module name "fs" has not been loaded yet for context: _. Use require([])*
Thanks a lot for your attention.
So, this is not a RequireJS problem per-se. The package you want to use is a Node.js package. It is intended to run in node (a server/desktop execution environment for JavaScript). It cannot/will not run in web page in a browser.
The packages it is trying to use (fs in particular) provide access to system resources such as the file system. Node provides these packages as part of its core libraries to any package that runs in node. A browser is specifically designed for security reasons never to allow direct access to such resources to any code that run in the browser because who knows where it came from or might try to do.
I haven't really tried to do this myself, but browserify (alternative to requirejs) claims that it will allow you to use any node package in your application.
If musicjson is at the heart of what your app should achieve and requirejs is a small step on the way to getting there, you could try your luck with browserify instead.