Questions tagged [module.exports]

The module.exports object is created by the Module system. Sometimes this is not acceptable; many want their module to be an instance of some class. To do this, assign the desired export object to module.exports. Assigning the desired object to exports will simply rebind the local exports variable, which is probably not what is desired.

As mentioned above, exports is an object. So it exposes whatever you assigned to it as a module. For example, if you assign a string literal then it will expose that string literal as a module.

The following example exposes simple string message as a module in Message.js.

Message.js

module.exports = 'Hello world';

Now, import this message module and use it as shown below.

app.js

var msg = require('./Messages.js');
console.log(msg);

Run the above example and see the result, as shown below.

C:\> node app.js
Hello World

Note: You must specify ./ as a path of root folder to import a local module. However, you do not need to specify the path to import Node.js core modules or NPM modules in the require() function.

137 questions
11
votes
6 answers

NextJS redirects not redirecting urls after define in next.config.js file

I tried to define redirects in my NextJS app. but it is not working. This is how I tried to do it in my next.config.js file: const withImages = require('next-images') const withPlugins = require("next-compose-plugins"); const optimizedImages =…
Yuval Levy
  • 2,397
  • 10
  • 44
  • 73
9
votes
2 answers

Separate template literals from logic with module exports

I'm experimenting with HTML String template literals (Electron which uses NodeJS). It works well but it looks like a mess. What I miss is the separation between HTML and logic. Here is what I've got module.exports = { snippet: (value1, value2) =>…
Jens Törnell
  • 23,180
  • 45
  • 124
  • 206
7
votes
2 answers

JavaScript: module/require is not defined

I'd like to have Jest testing for some JavaScript code that I'm using in the frontend of my web app. From what I understand, I need to export and import functions from my module so I can test them in Jest, and Jest (only) supports CommonJS module…
mattu
  • 944
  • 11
  • 24
6
votes
2 answers

Is there any way to get all the exported names from a JavaScript file?

Lets say I have a JavaScript file(lib.js) which exports some classes, functions, etc etc. export class Employment { id = ""; startDate = ""; endDate = ""; companyEmailAddress = ""; }; export function HolidayPopupContainer() { …
Chetan
  • 4,735
  • 8
  • 48
  • 57
6
votes
2 answers

Using module.exports in React ES6

I am trying to use module.exports to create a 'global' function which I can use in my React project to make API calls using Axios. I have tried the following... import axios from 'axios'; module.exports = { fetchApi: function (endPoint) { …
Steven Collins
  • 373
  • 2
  • 8
  • 20
4
votes
4 answers

Accessing non-existent property of module.exports inside circular dependency NodeJS

Im having some issues when using module.exports inside NodeJS, and I've followed multiple guides, and im almost certain Im doing it right. I have to scripts, main.js and event.js. Im trying to share a function from main.js to event.js, but its not…
gjoe
  • 99
  • 1
  • 1
  • 7
4
votes
1 answer

Unclear why functions from Data.Ratio are not exposed and how to work around

I am implementing an algorithm using Data.Ratio (convergents of continued fractions). However, I encounter two obstacles: The algorithm starts with the fraction 1%0 - but this throws a zero denominator exception. I would like to pattern match the…
bdecaf
  • 4,652
  • 23
  • 44
3
votes
1 answer

How do I add parameters to long-form return requests in module.exports routes?

I'm coding for an API connection area, that's predominately graphql but needs to have some REST connections for certain things, and have equivalent to the following code: foo.js module.exports = { routes: () => { return [ { …
lilHar
  • 1,735
  • 3
  • 21
  • 35
3
votes
1 answer

export default new object

Is it guaranteed that export default new object(), where object is some kind of type (e.g. Date), returns the same object all the time? // date.js export default new Date() // foo.js import date from './date' // bar.js import date from './date' Can…
Yuki
  • 3,857
  • 5
  • 25
  • 43
3
votes
2 answers

Error: Uncaught ReferenceError: module is not defined when using module.export

Complete error: Uncaught ReferenceError: module is not defined at vsop87Bearth.js:1 I`m trying to use some js files that I found from this(https://github.com/commenthol/astronomia) repository to calculate the rectangular coordinates of the sun. I'm…
2
votes
1 answer

Webpack 5 compiles successfully but application doesn't work: Uncaught ReferenceError: exports is not defined

I am trying to upgrade from webpack 4 to 5 (all config was initially done using CRA + eject). I was able to make the webpack compile successfully but the application breaks with a "Uncaught ReferenceError: exports is not defined" error. I saw some…
2
votes
1 answer

How I can change value varible in a file in another file in javascript

I want to change value variable in an index.js in another file, but i can't do that and this is my code example index.js var length = 0; client.commands.get('join').excute(length); anotherfile.js module.exports = { name: 'join', …
2
votes
2 answers

Call a .json from module.exports

To avoid having to change every emoji from every file whenever I switch an emoji, I decided to place an emojis.json and call the emoji from there. emojis.json { "loading": "", "pressf":…
Dharuz
  • 25
  • 7
2
votes
1 answer

How to use module exports correctly in my local app

Currently I am working on a personal project and am running into a problem understanding the functionality of module.exports within my app... My app structure looks like this: app ├── common │ ├── cmd │ │ ├── run-cmd.js ├── routes │ ├──…
scapegoat17
  • 5,509
  • 14
  • 55
  • 90
2
votes
1 answer

NW.js trouble exporting modules

I am trying to use module.exports() to create a new module in my NW.js application. I have two files that I am using: Index.js const gkm = require('gkm'); //This is a key listener const AudioStreamMeter = require('audio-stream-meter'); //This is a…
bocodes
  • 387
  • 1
  • 4
  • 12
1
2 3
9 10