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.

ES6 import always returns undefined

See original GitHub issue

Why the module can’t be import after babelify? My case: MyModule.js

export class MyModule {

}

After babelify it I got bundle.js.

import {MyModule} from 'bundle'; 

But MyModule is undefined.

Here is my gulp build

var gulp = require('gulp-help')(require('gulp'));
var $ = require('gulp-load-plugins')();
var browserify = require('browserify');
var babelify = require('babelify');
var bundleCollapser = require('bundle-collapser/plugin');
var derequire = require('derequire');
var source = require('vinyl-source-stream');
var map = require('vinyl-map');
var buffer = require('vinyl-buffer');

gulp.task('build', 'Builds the library', function (cb) {
  var production = $.util.env.type === 'production';
  var b = browserify({
    debug: !production,
    entries: './lib/MyModule.js',
    paths: ['./lib']
  });

  // transform to babel
  b.transform(babelify);

  // convert bundle paths to IDS to save bytes in browserify bundles
  b.plugin(bundleCollapser);

  b.bundle()
    .on('error', function (err) {
      $.util.log($.util.colors.bold('Build Failed!'));
      cb(err);
    })
    .pipe(source('bundle.js'))
    .pipe(map(function (code) {
      return derequire(code);
    }))
    .pipe(buffer())
    .pipe(gulp.dest('dist')) // for npm distribution
    .on('end', function () {
      cb();
    });
});

My .bebalrc

{
  "presets": [
    "env"
  ],
  "plugins": [
    "transform-class-properties"
  ]
}

Here is the demo project https://github.com/unbug/js-middleware

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

7reactions
unbugcommented, Aug 1, 2017

@ksmigiel Search babelify standalone

0reactions
theryan722commented, Jun 29, 2020

@unbug Can you be more specific, your answer is really vague. What did you do to arrive at a solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ES6 modules import is undefined - javascript - Stack Overflow
In essence, when your MyComponent1 is trying to import MyComponent2 via the index file, the index file has not been exported yet so...
Read more >
Imports/requires return undefined #11562 - babel/babel - GitHub
I found out that happens when there is a _interopRequireDefault with a file inside the project which defers the resolving of modules or...
Read more >
Imported es6 module is undefined during Debug (During Test ...
Undefined variables while debugging must be caused by wrong/missing name mappings in sourcemaps: if the variable is renamed while transpiling/ ...
Read more >
JavaScript modules - MDN Web Docs
This guide gives you all you need to get started with JavaScript module syntax.
Read more >
Documentation - TypeScript 2.0
Null - and undefined-aware types. TypeScript has two special types, Null and Undefined, that have the values null and undefined respectively.
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