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"
}
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 ?
Most helpful comment
Do this instead.
Since
winstonis in fact a namespace for a set of exports, you should avoid using the syntax for importing the _default_ export.