FAQ Database Discussion Community


CommonJS Illegal invocation error when calling function on module.exports

javascript,commonjs,webpack,method-invocation
Doing this works fine: var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame; function _test() { console.log('hello from test'); } requestAnimationFrame(_test); However moving this to another file and exporting it using CommonJS/webpack results in: Uncaught TypeError: Illegal invocation (like so:) module.exports.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame; ... var poly =...

reduce a lot of require() function

javascript,browserify,commonjs
I have a lot of CommonJS modules and I need to add all of them to array. Therefore, I have a huge repeated code: //Container module var module1 = require('module1'), module2 = require('module2'), ... module25 = require('module25') var container = []; container.push(module1); container.push(module2); ... container.push(module25); module.exports = container; Is it...

Titanium (Node.js) require() vs Global

javascript,node.js,titanium,require,commonjs
I'm unsure if any of the following is considered a better practice. Environment: Appcelerator Titanium/Alloy, which is a framework to make native apps. It uses javascript as source, comparable to node.js (more like CommonJS). Case 1: Many files where i use: var localInstance = require('main/module/sub/foobar')({ initialize: true}); Case 2: A...

How does the React library require its source libraries directly?

javascript,reactjs,commonjs
I was looking through the source code of Facebook's React and saw that throughout the source of the project they don't specify relative paths for loading its own modules. For example, instead of doing var foo = require('../bar/foobar') the library uses var foo = require('foobar') as if the module was...

RequireJS with CommonJS modules

javascript,requirejs,amd,commonjs
Using RequireJS with CommonJS modules, what happens when I do this: define(function(require, exports, module) { //Put traditional CommonJS module content here var simpleCommonJSModule = require('simple-commonjs-module'); module.exports = new String('foo'); return { //return empty object along with using module.exports } }); if I return something, I assume the module.exports will be...

How to avoid code duplication using Browserify

javascript,browserify,commonjs
CommonJS noob here, I read about browserify and thought it was simpler than my existing RequireJS setup, so I went ahead and changed them. What I have found out is I am going to have code duplication in each bundle. Let me explain: Lets say, I have page1.js and page2.js...

Browserify dynamic seperate bundles

javascript,browserify,commonjs
My app loads an object of messages in a given language into the application. My structure is like so: /lang /en.js (100 kb file) /ru.js (100 kb file) /... many more app.js (this is `MyApp` as below) The language files are very big so I would like to create seperate...

Webstorm helper for requiring module

webstorm,commonjs
I'm looking for a way to require module easily with Webstorm (shortcut, plugin, intention, ...). Here is my use case : Given my cursor located at the end of a unknown variable myVar somewhere in my JS file. When I press a shortcut, I jump at the top of the...

Porting Chrome extension using jQuery to Firefox SDK Add-on

jquery,google-chrome,firefox,require,commonjs
My Chrome extension makes use of jQuery in the background page, for things like jQuery.extend(), jQuery.ajax(), jQuery.deferred(), (not DOM manipulation stuff, which doesn't make sense in a background page). Migrating this code to a Firefox SDK Add-on, there's no background window object, which jQuery requires to work, so something like...

Compile a directory tree of separate apps (one entry point each), using CommonJS modules and ES6 modules

build,couchdb,ecmascript-6,commonjs
I am writing a Couch app. The output of the build process must be a directory tree of self contained JS files, like so: dist ├── _attachments │ ├── logo.jpg │ └── splash.jpg ├── lists │ └── sitemap.js ├── shows │   ├── article.js │ ├── home.js │ └── dashboard.js ├──...

Export a constructor from a CommonJS style module in RequireJS

javascript,requirejs,commonjs
I am attempting to export a constructor function using the exports object in a CommonJS style module. For some reason, requiring the module results in an empty object being returned instead of the exported function. For example, this module; define(function(require, exports) { var Example = function() { this.example = true;...

Wait until processing completes before exporting Node.js module?

javascript,arrays,node.js,asynchronous,commonjs
I'm loading a configuration object, then remapping its properties in the for..in loop below (it does some string concatenation on the values of .styles which is just an array of strings). When I'm requiring the module, the re-mapping clearly hasn't occurred. I'm guessing this is some sort of async issue,...

CommonJS is synchronous but what would happen if there was a call to an async function

node.js,commonjs
CommonJS uses a require() statement that is synchronous but what if you have a module like so: function asyncFunction(){ var promise = ...; return promise; } module.exports = asyncFunction(); what kind of problems could arise here? are you supposed to always have synchronous code returned for the module.exports object? For...

How can I injector Angular module in webpack?

angularjs,commonjs,webpack
I install ngRoute from npm, my webpack default load default.js webpack.config.js 'use strict'; var path = require('path'), webpack = require("webpack"), AngularPlugin = require('angular-webpack-plugin'); module.exports = { resolve: { root: [path.join(__dirname, "bower_components")] }, plugins: [ new webpack.ResolverPlugin( [new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])], ["normal", "loader"] ), new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery", "windows.jQuery": "jquery",...

Share a Function Across Multiple Cloud Code Files

javascript,parse.com,commonjs
I want to have a utilities.js file that I require in my Parse Cloud Code that contains various functions that I share across my Cloud Code files. So in utilities.js I would have: function sheBear(){ return "rawr rawr rawr" } ...and in whatever.js I could have: require('cloud/helpers/utilities.js') Parse.Cloud.define("tester", function(request) {...

TypeScript won't resolve module when import is used

node.js,typescript,amd,commonjs,typescript1.4
// Modules/MyModule.ts -------------------------------- import fs = require("fs"); module Hello { export function World(): string { return "Hello World"; } } // Main/App.ts ---------------------------------------- console.log(Hello.World()); // Cannot find name 'Hello' For some reason this produces the error specified above. The weird thing is if I uncomment the import statement I don't...

Use requirejs modules as es6 module

javascript,requirejs,ecmascript-6,commonjs,es6-module-loader
I'm trying to migrate my project to ES6 modules, but I met an issue. I have 3rd party script that use modules with help of requirejs. Let name it 3rdParty.js. It was obtained via bower, so there is no ability and wish to update it. Is there any way to...

How to use semantic-ui in react with webpack?

jquery,reactjs,commonjs,semantic-ui
I would like to use CommonJS to include semantic-ui in my react jsx file. I installed semantic-ui with bower, and webpack is correctly configured to include the bower_components directory. However, when I use require('semantic-ui/dist/semantic-ui.js') in the jsx file, the console always throw an error "Uncaught ReferenceError: jQuery is not defined",...

CommonJS Modules - Exporting a function that returns a function with arguments

javascript,node.js,commonjs,rendr
In this rendr - sessions example, there's an express middleware module... module.exports = function incrementCounter() { return function incrementCounter(req, res, next) { var app = req.rendrApp , count = app.get('session').count || 0; req.updateSession('count', count + 1); next(); }; }; Can you not achieve the same thing with the following? module.exports...

Typescript can't find external module when referenced by two different files

node.js,typescript,commonjs
I may be missing something obvious, but Typescript's module resolver for commonjs isn't working as expected. Given the following directory structure and files: ./first/shared.ts ./first/second/class_a.ts ./first/second/class_b.ts ./third/class_c.ts Where: class_a references shared.ts class_b references class_a.ts and class_c.ts class_c references shared.ts Specifically: shared.ts: class Shared{} export = Shared; class_a.ts: import Shared =...

Would bower only be useful for css if using browserify and node.js commonJs

node.js,bower,browserify,commonjs
I see from this stack overflow question the difference between bower and npm: npm is used for managing Node.js modules, but can work on the front-end too when combined with Browserify or WebPack. npm does nested dependency tree, so your dependencies can have their own dependencies which can have their...

Using Chutzpah to test CommonJS modules used in duktape

javascript,visual-studio,unit-testing,commonjs,chutzpah
I'm working in a game engine (Atomic Game Engine) which uses the Duktape embeddable javascript engine. This allows loading of CommonJS modules, so I have modules like the following: require("Scripts/LumaJS/Interface"); // Interface to be implemented by state objects var IState = new Interface({ optional: ["onEnter", "onExit", "onUpdate"], reserved: ["label"] });...

How can I import this React package using RequireJS?

requirejs,commonjs
There is a package called formsy-react that looks like it was made to be imported using CommonJS (see code here: https://github.com/christianalfoni/formsy-react/blob/master/release/formsy-react.js) but I'm having trouble getting it to work with RequireJS. Specifically I try require(['formsy-react.js'], function (Formsy) { console.log(Formsy) }); but Formsy is not defined. I'm new to RequireJS so...