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
30
votes
5 answers

Reason behind this self invoking anonymous function variant

While looking at code on github, I found the following: (function() { }).call(this); This is clearly a self invoking anonymous function. But why is it written this way? I'm used to seeing the canonical variant (function() {})(). Is there any…
Sean McMillan
  • 10,058
  • 6
  • 55
  • 65
30
votes
4 answers

How to include commonjs module in ES6 module node app?

I have a node app that I'd like to use in the standard ES6 module format (i.e., "type": "module" in the package.json, and using import and export throughout) without transpiling down to ES5. But I would like to take advantage of some older libraries…
Luke
  • 18,811
  • 16
  • 99
  • 115
30
votes
5 answers

EventMachine vs Node.js

I'm going to develop a collaborative site, and one of the features will be collaborative editing with realtime changes. i.e. when two or more users are editing the same doc, they can see each other changes as soon as they happen. I have some…
Pablo B.
  • 1,823
  • 1
  • 17
  • 27
30
votes
3 answers

Class constructor cannot be invoked without 'new' - typescript with commonjs

I am making server side chat with colyseus (node game server framework). Im using typescript with module:commonjs because colyseus is built upon commonjs. I have class ChatRoom that extends Colyseus.Room. At run-time I get this error: Class…
29
votes
9 answers

Typescript module systems on momentJS behaving strangely

I'm trying to use momentJs from typescript: depending on what module system I'm using to compile typescript, I find a different behaviour on how I can use momentJs. When compiling typescript with commonJs everything works as expected and I can just…
nemenos
  • 877
  • 1
  • 10
  • 23
28
votes
2 answers

Why does Electron need to be saved as a developer dependency?

As per the official website, the correct way to save electron files is: npm install electron --save-dev Electron is actually required for running the app (quite literally: require()) and this goes against the top voted answer here. So why do we…
Prithvish Baidya
  • 639
  • 1
  • 8
  • 19
27
votes
3 answers

Structuring a NodeJS module - variables and methods

I want to create modules to structure my NodeJS application, but I'm a little lost, and I haven't found anything (with hours of searching) that is completely definitive on the subject. Say I'd like to create a "user" module, from which I can…
Matt Egan
  • 809
  • 1
  • 10
  • 16
27
votes
2 answers

Differences between creating a new class to using export const

Setup: BabelJS (es2015, react, stage-1) Webpack React / redux New to CommonJS and ES6. I know the difference between an object instance and a static container of methods but I am not sure how they behave when separated to modules. So I wonder what…
Guy
  • 12,488
  • 16
  • 79
  • 119
27
votes
2 answers

How to import part of object in ES6 modules

In the react documentation I found this way to import PureRenderMixin var PureRenderMixin = require('react/addons').addons.PureRenderMixin; How can it be rewritten in ES6 style. The only thing I can do is: import addons from "react/addons"; let…
Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79
26
votes
2 answers

Named export 'Types' not found. The requested module 'mongoose' is a CommonJS module, which may not support all module.exports as named exports

I have an express server written in typescript with "module": "es2020" in its tsconfig. I've also developed another es2020 module for my graphql API, still in typescript, and this module uses mongoose with such named imports: import { Types } from…
0xChqrles
  • 433
  • 1
  • 5
  • 10
26
votes
1 answer

typescript node.js express routes separated files best practices

using Express in a Node project along with Typescript what would be the "best practices" for express.Router. example directory structure |directory_name ---server.js |--node_modules |--routes ---index.ts |--admin ---admin.ts …
Andrei
  • 1,196
  • 1
  • 11
  • 25
25
votes
2 answers

What is the difference between `main` and `module` vs `exports` in package.json?

I'm trying to write a library that can be both required and imported.'ve found different approaches online, which are as follows: { "main": "mylib-cjs.js", "module": "mylib-esm.js" } and { "exports": { "import": "mylib-esm.js", …
EnderShadow8
  • 788
  • 6
  • 17
24
votes
2 answers

Generating source maps from browserify using grunt

I have followed the instructions here: https://www.npmjs.org/package/grunt-browserify, to try and set up source maps for browserify on grunt. The options for browserify in my gruntfile are : browserify: { options: { …
Amal Antony
  • 6,477
  • 14
  • 53
  • 76
23
votes
3 answers

Rollup Error: 'default' is not exported by node_modules/react/index.js

I have a project where I use webpack and want to switch to rollup.js but I have trouble regarding the plugin @rollup/plugin-commonjs. My rollup.conf.js import resolve from '@rollup/plugin-node-resolve'; import commonjs from…
tiefenb
  • 732
  • 3
  • 16
  • 32
23
votes
7 answers

Jest - Mock a constant property from a module for a specific test

So, I'm attempting to do something which on the surface should be very simple... I have some constants defined in: //constants.js module.exports = { MY_CONSTANT: "TEST" } I have a file which I'm trying to test which has a branching statement like…
A.K
  • 293
  • 1
  • 3
  • 10
1 2
3
85 86