start start - 1 year ago 66
Javascript Question

Is my "import" statement being incorrectly transpiled?

It is my understanding that the following two lines are equivalent:

const Up = require('write-up').default


import Up from 'write-up'

Both examples should make the default export of the
module available as

Unfortunately, using Babel and Webpack, that's not the behavior I'm seeing.

The first example works just fine. It produces this line:

var Up = __webpack_require__(5).default;

is set to the default export of the
module, which is the behavior I expect.

The second example does not work. It produces this:

var _writeUp = __webpack_require__(5);

var _writeUp2 = _interopRequireDefault(_writeUp);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

Instead of setting
to the default export,
is set to an object containing every single export of the
even has a
module also has named exports, but this still occurs when I remove all of them.)

What am I doing wrong?

Here's the relevant portion from my Webpack config:

test: /.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015']

Answer Source

Babel-compiled import statements have a dual behavior. If the module being imported was compiled from ES6 export statements with Babel, then

import foo from 'foo';

will behave (mostly) like

const foo = require('foo').default;

but if foo was not compiled with Babel, or something that tries to be compatible with Babel, then as far as Babel is concerned, it has no special behavior and is a normal CommonJS module. In that case, which appears to be your case, it will behave like

const foo = require('foo');

Babel does this so that you can import normal CommonJS modules like

import fs from 'fs';

where fs is a standard Node module and has no .default property.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download