ken
ken

Reputation: 8993

Rollup can't see named export from commonjs module

I have a simple AWS Lambda function (state-info.ts) which I am tree-shaking with Rollup and it is giving me the following error:

[!] Error: 'DB' is not exported by ../../forest-fire/abstracted-admin/lib/index.js

https://github.com/rollup/rollup/wiki/Troubleshooting#name-is-not-exported-by-module
src/state-info.ts (10:9)
8: import { Lambda } from "aws-sdk";
9: import { STATES } from "./models/shared";
10: import { DB } from "abstracted-admin";
            ^
11: import { StateInfo } from "./models/StateInfo";
12: import { IApiResponse } from "./shared/ApiRetriever";

Now it just so happens that I wrote the module abstracted-admin (currently at v0.6.5 on npm) which it is complaining about and it DOES export DB as a named export (and as the default export). But for some reason Rollup is unhappy.

I've created a video walk-through of everything for full context: video.

For those of you who don't like video, here are the key facts/files:

abstracted-admin/lib/index.d.ts:

import { DB } from "./db";
export default DB;
export { DB, IFirebaseConfig, IFirebaseListener } from "./db";

abstracted-admin/lib/index.js:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const db_1 = require("./db");
exports.default = db_1.DB;
var db_2 = require("./db");
exports.DB = db_2.DB;

and from within the abstracted-admin/package.json:

{
  ...
  "files": ["lib"],
  "main": "lib/index.js",
  "typings": "lib/index.d.ts",
}

state-info.ts (aka, the file being rolled up):

import {
  IDictionary,
  AWSGatewayCallback,
  IAWSGatewayRequest,
  IAWSGatewayResponse
} from "common-types";
import GetStateInfo from "./vote-smart/get-state-info";
import { Lambda } from "aws-sdk";
import { STATES } from "./models/shared";
import { DB } from "abstracted-admin";
import { StateInfo } from "./models/StateInfo";
import { IApiResponse } from "./shared/ApiRetriever";
/** ... */

Meanwhile my rollup config is:

import cjs from "rollup-plugin-commonjs";
import resolve from "rollup-plugin-node-resolve";
import json from "rollup-plugin-json";
import ts from "rollup-plugin-typescript2";
import globals from "rollup-plugin-node-globals";

export default {
  input: "src/state-info.ts",
  output: {
    file: "lib/state-info-rolled.js",
    format: "cjs"
  },
  external: ["aws-sdk"],
  plugins: [
    globals(),
    json(),
    cjs({
      include: "node_modules/**",
      exclude: ["node_modules/aws-sdk/**"]
    }),
    ts(),
    resolve({
      jsnext: true,
      main: true,
      browser: false,
      preferBuiltins: false,
      extensions: [".js", ".json"]
    })
  ]
};

Upvotes: 2

Views: 6854

Answers (1)

7zark7
7zark7

Reputation: 10145

I believe Rollup expects the below in this situation:

import admin from "abstracted-admin";
const { DB } = admin;

It's different from Webpack's behavior and has caught me a few times.

Upvotes: 4

Related Questions