question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`require` retrieves an empty object instead of exported value

See original GitHub issue

I’ve got a file called IN.js:

// IN.js

module.exports = 5;

Which gets transpiled with this command by babelify (as per the CLI instructions here):

browserify IN.js -t babelify --outfile OUT.js

My OUT.js contains this:

// OUT.js

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";

module.exports = 5;

},{}]},{},[1]);

Te problem is when I try to make use of this module via require, the following TEST.js file shows an unexpected result:

// TEST.js

const testVal = require('./out'); // should be 5

console.log(testVal);
// --> {}

Unfortunately, it logs out an empty object ({}). Am I doing it right expecting testVal to be 5?

PS: my package.json’s devDependencies looks like this:

"devDependencies": {
    "babel-cli": "^6.5.1",
    "babel-preset-es2015": "^6.5.0",
    "babelify": "^7.2.0"
  }

and my .babelrc looks like this:

{
  "presets": [
    "es2015"
  ],
  "plugins": []
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:6
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
zertoshcommented, Apr 26, 2016

@brycehanscomb the option you’re missing is the browserify’s standalone. A browserify bundle just runs - doesn’t return or export anything. The empty object you’re seeing is from node’s module wrapper. When you use the standalone option, then you can require that bundle and get the export of the entry file.

3reactions
renatorroliveiracommented, Feb 14, 2018

So we can’t use import/export syntax for publishing modules? I thought I could write ES6 with import/export and transpile using babelcommand. Publishing those transpiled versions should work… at least I thought it should. But it doesn’t, Node.js at least don’t “see” the exports.default (transpiled file, generated from export default syntax) on the script that requires it…

Is there any thing I can do to make babel use old module.exports syntax on transpiled files?

Read more comments on GitHub >

github_iconTop Results From Across the Web

module.exports returns an empty object - Stack Overflow
You're assigning module.exports from a callback that is called asynchronously, as a result of an http request. That means the assignment is done ......
Read more >
Understanding Modules and Import and Export Statements in ...
Now, in script.js , you will use import to retrieve the code from the functions.js module at the top of the file.
Read more >
Working with object metadata - Amazon Simple Storage Service
Name Description Can user modify the value? Date Current date and time. No Content‑Disposition Object presentational information. Yes Content‑Length Object size in bytes. No
Read more >
Underscore.js
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects.
Read more >
Object.freeze() - JavaScript - MDN Web Docs
The Object.freeze() method freezes an object. ... For data properties of a frozen object, their values cannot be changed since the writable ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found