Rollup: Can't import external commonjs dependency with a default key

Created on 7 Apr 2016  ·  2Comments  ·  Source: rollup/rollup

I'm using rollup in a nodejs server project with es2015 modules. I have a Logger.js file that tries to import winston:

import winston from "winston"

Winston is a normal npm package that exports a commonjs module. Rollup turns that import into this:

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var winston = _interopDefault(require('winston'));

The problem is that winston exports a value called default that contains the default transports and exception handlers of winston. The generated code assigns that value instead of the proper module to the winston variable.

My rollup.config.js looks like this:

import babel from "rollup-plugin-babel"

export default {
  entry: "start.js",
  plugins: [
    babel({
      presets: ["es2015-rollup"]
    })
  ],
  format: "cjs",
  dest: "build/main.rollup.js"
}
c⁵ ⋅ question

Most helpful comment

Do this instead.

import * as winston from 'winston'

Since winston is in fact a namespace for a set of exports, you should avoid using the syntax for importing the _default_ export.

import winston from 'winston';

// is functionally equivalent to
import { default as winston } from 'winston';

All 2 comments

Do this instead.

import * as winston from 'winston'

Since winston is in fact a namespace for a set of exports, you should avoid using the syntax for importing the _default_ export.

import winston from 'winston';

// is functionally equivalent to
import { default as winston } from 'winston';

Thanks for the solution ! we lost 1 day with this stuff :(

Am I wrong or seems like winston could be doing something different in their package to at least avoid falling into this ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iam-peekay picture iam-peekay  ·  3Comments

ashubham picture ashubham  ·  3Comments

Uyarn picture Uyarn  ·  3Comments

Havunen picture Havunen  ·  3Comments

good-idea picture good-idea  ·  3Comments