'Module does not provide an export named default - compiled typescript module

I'm developing a node npm module in typescript, and after I compile it to commonjs and try to import it, I get the error: SyntaxError: The requested module 'woo-swell-migrate' does not provide an export named 'default'

enter image description here

But... it does have a default export. Here is the compiled index.js file:

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const woocommerce_rest_api_1 = __importDefault(require("@woocommerce/woocommerce-rest-api"));
const swell_node_1 = __importDefault(require("swell-node"));
const path_1 = __importDefault(require("path"));
class WooSwell {
    /**
     *
     * @param config - required params for connecting to woo and swell
     *
     * @param dirPaths - directory paths to store json files and images in
     * @param dirPaths.data - directory to store json files in
     * @param dirPaths.images - directory where wordpress image backup is stored
     */
    constructor(config, dirPaths) {
        this.swell = swell_node_1.default.init(config.swell.store, config.swell.key);
        this.woo = new woocommerce_rest_api_1.default({
            consumerKey: config.woo.consumerKey,
            consumerSecret: config.woo.consumerSecret,
            url: config.woo.url,
            version: config.woo.version
        });
        this.wooImages = {};
        this.paths = {
            wooImageFiles: dirPaths.images,
            wooImageJson: path_1.default.resolve(dirPaths.data, 'woo-images.json'),
            wooProducts: path_1.default.resolve(dirPaths.data, 'woo-products.json'),
            swellCategories: path_1.default.resolve(dirPaths.data, 'swell-categories.json')
        };
    }
    /**
     * gets all records from all pages (or some pages, optionally) of endpoint
     *
     * @param endpoint - example: '/products'
     *
     * @param options - optional. if not provided, will return all records from all pages with no filters
     *
     * @param options.pages - supply a range of pages if not needing all - example: { first: 1, last: 10 }
     *
     * @param options.queryOptions - Swell query options, limit, sort, where, etc. See https://swell.store/docs/api/?javascript#querying
     *
     * @returns - record array
     */
    async getAllPagesSwell(endpoint, options) {
        const res = await this.swell.get(endpoint, options === null || options === void 0 ? void 0 : options.queryOptions);
        let firstPage = (options === null || options === void 0 ? void 0 : options.pages.first) || 1;
        let lastPage = (options === null || options === void 0 ? void 0 : options.pages.last) || Object.keys(res.pages).length;
        let records = [];
        for (let i = firstPage; i <= lastPage; i++) {
            const res = await this.swell.get(endpoint, Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.queryOptions), { page: i }));
            records.push(...res.results);
        }
        return records;
    }
    /**
     * gets all records from all pages of endpoint
     *
     * @param endpoint example: 'products'
     *
     * @param options - optional.
     *
     * @param options.pages - supply a page range if not loading all pages { start: 10, end: 15 }
     *
     * @returns - record array
     */
    async getAllPagesWoo(endpoint, options) {
        var _a, _b;
        const res = await this.woo.get(endpoint);
        const firstPage = ((_a = options === null || options === void 0 ? void 0 : options.pages) === null || _a === void 0 ? void 0 : _a.first) || 1;
        const lastPage = ((_b = options === null || options === void 0 ? void 0 : options.pages) === null || _b === void 0 ? void 0 : _b.last) || parseInt(res.headers['x-wp-totalpages']);
        const records = [];
        for (let i = firstPage; i <= lastPage; i++) {
            records.push(...(await this.woo.get(endpoint, { page: i })).data);
        }
        return records;
    }
}
exports.default = WooSwell;

It's there... right at the bottom. exports.default = WooSwell. So why am I getting this error?

Here is my package.json:

{
  "name": "woo-swell-migrate",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "type": "module",
  "scripts": {
    "build": "tsc",
    "test": "jest --config jestconfig.json"
  },
  "keywords": [],
  "license": "ISC",
  "dependencies": {
    "@woocommerce/woocommerce-rest-api": "^1.0.1",
    "dotenv": "^16.0.0",
    "es2017": "^0.0.0",
    "image-size": "^1.0.1",
    "mime-types": "^2.1.35",
    "swell-node": "^4.0.9",
    "ts-jest": "^28.0.1"
  },
  "devDependencies": {
    "@types/mime-types": "^2.1.1",
    "@types/jest": "^27.5.0",
    "@types/woocommerce__woocommerce-rest-api": "^1.0.2",
    "@types/node": "^17.0.31",
    "jest": "^28.0.3"
  }
}

and my tsconfig.json:

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "declaration": true,
    "outDir": "./dist",
    "esModuleInterop": true,
    "moduleResolution": "node",
    "strict": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "**/__tests__/*"]
}


Solution 1:[1]

exports.default is exporting a member named default, sort of like this (invalid because default is a keyword):

export const default = someValue;

You can try using an import wildcard:

import * as WooSwellMigrate from "woo-swell-migrate";

const WooSwell = WooSwellMigrate.default; // access the exported member named "default"

But if it's within your reach, you should change how woo-swell-migrate is built! Try to use the ESM module system instead of CommonJS in its tsconfig.json.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hittingonme