Questions tagged [commonjs]

CommonJS is a project whose goal is to move JavaScript outside the browser.

CommonJS is a project whose goal is to move JavaScript outside the browser, such as on servers and desktops, in applications. The project is not affiliated with the ECMA International group working on the ECMAScript standard. CommonJS provides APIs to access functionality not seen in browsers, such as database access.

Documentation here

1289 questions
899
votes
6 answers

Relation between CommonJS, AMD and RequireJS?

I'm still very confused about CommonJS, AMD and RequireJS, even after reading a lot. I know that CommonJS (formerly ServerJS) is a group for defining some JavaScript specifications (i.e. modules) when the language is used outside the browser.…
gremo
  • 47,186
  • 75
  • 257
  • 421
858
votes
24 answers

module.exports vs exports in Node.js

I've found the following contract in a Node.js module: module.exports = exports = nano = function database_module(cfg) {...} I wonder what's the difference between module.exports and exports and why both are used here.
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
337
votes
43 answers

Field 'browser' doesn't contain a valid alias configuration

I've started using webpack2 (to be precise, v2.3.2) and after re-creating my config I keep running into an issue I can't seem to solve I get (sorry in advance for ugly dump): ERROR in ./src/main.js Module not found: Error: Can't resolve…
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
332
votes
9 answers

Difference between "module.exports" and "exports" in the CommonJs Module System

On this page (http://docs.nodejitsu.com/articles/getting-started/what-is-require), it states that "If you want to set the exports object to a function or a new object, you have to use the module.exports object." My question is why. //…
Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67
205
votes
4 answers

Babel 6 changes how it exports default

Before, babel would add the line module.exports = exports["default"]. It no longer does this. What this means is before I could do: var foo = require('./foo'); // use foo Now I have to do this: var foo = require('./foo').default; // use foo Not a…
kentcdodds
  • 27,113
  • 32
  • 108
  • 187
181
votes
24 answers

How to check whether a script is running under Node.js?

I have a script I am requiring from a Node.js script, which I want to keep JavaScript engine independent. For example, I want to do exports.x = y; only if it’s running under Node.js. How can I perform this test? When posting this question, I didn’t…
theosp
  • 7,439
  • 3
  • 23
  • 24
125
votes
5 answers

Node.js - use of module.exports as a constructor

According to the Node.js manual: If you want the root of your module's export to be a function (such as a constructor) or if you want to export a complete object in one assignment instead of building it one property at a time, assign it to …
Naresh
  • 23,937
  • 33
  • 132
  • 204
113
votes
7 answers

Load "Vanilla" Javascript Libraries into Node.js

There are some third party Javascript libraries that have some functionality I would like to use in a Node.js server. (Specifically I want to use a QuadTree javascript library that I found.) But these libraries are just straightforward .js files and…
Chris W.
  • 37,583
  • 36
  • 99
  • 136
106
votes
4 answers

module.exports in typescript

does somebody know how to do a module.exports? I tried some different ways ending up with export class Greeter {} which will compile to exports.Greeter = Greeter; But what I really want is this: exports = Greeter; So that I can use it like…
Kersten
  • 1,662
  • 3
  • 16
  • 28
101
votes
4 answers

Can I use alias with NodeJS require function?

I have an ES6 module that exports two constants: export const foo = "foo"; export const bar = "bar"; I can do the following in another module: import { foo as f, bar as b } from 'module'; console.log(`${f} ${b}`); // foo bar When I use NodeJS…
xMort
  • 1,545
  • 3
  • 11
  • 20
83
votes
6 answers

How can I 'require' CommonJS modules in the browser?

What is the best way to load CommonJS modules as client-side JavaScript code in the browser? CommonJS modules put their functionality in the module.exports namespace and are usually included using require(pathToModule) in a server-side script.…
travelboy
  • 2,647
  • 3
  • 27
  • 37
82
votes
1 answer

What is CommonJS and why should I care how it can help in writing .js applications?

I'm learning JS and someone more experienced mentioned that they use CommonJS to handle keeping js code organized (since there's no "module" functionality). I looked on Quora, but it explained the group and it's goals, not so much how I might use…
Clay Nichols
  • 11,848
  • 30
  • 109
  • 170
80
votes
7 answers

How to handle circular dependencies with RequireJS/AMD?

In my system, I have a number of "classes" loaded in the browser each a separate files during development, and concatenated together for production. As they are loaded, they initialize a property on a global object, here G, as in this example: var G…
avernet
  • 30,895
  • 44
  • 126
  • 163
68
votes
2 answers

RequireJS: How to define modules that contain a single "class"?

I have a number of JavaScript "classes" each implemented in its own JavaScript file. For development those files are loaded individually, and for production they are concatenated, but in both cases I have to manually define a loading order, making…
avernet
  • 30,895
  • 44
  • 126
  • 163
68
votes
11 answers

TypeScript error in Angular2 code: Cannot find name 'module'

I have defined the following Angular2 component: import {Component} from 'angular2/core'; @Component({ selector: 'my-app', moduleId: module.id, templateUrl: './app.component.html' }) export class AppComponent { } When I try to compile this,…
Naresh
  • 23,937
  • 33
  • 132
  • 204
1
2 3
85 86