我正在类型记录中开发一个节点npm模块,在我将它编译成commonjs并尝试导入它之后,我得到了错误:SyntaxError: The requested module 'woo-swell-migrate' does not provide an export named 'default'

但是..。它确实有一个默认的导出。下面是编译的index.js文件:
"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;就在那儿..。就在底部。exports.default = WooSwell。那我为什么要犯这个错误呢?
这是我的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"
}
}还有我的tsconfig.json
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"esModuleInterop": true,
"moduleResolution": "node",
"strict": true
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}发布于 2022-05-06 14:17:06
exports.default导出一个名为default的成员,类似于这样(无效,因为default是关键字):
export const default = someValue;您可以尝试使用导入通配符:
import * as WooSwellMigrate from "woo-swell-migrate";
const WooSwell = WooSwellMigrate.default; // access the exported member named "default"但是,如果它在您的能力范围内,您应该改变woo-swell-migrate的构建方式!尝试在其CommonJS中使用ESM模块系统而不是tsconfig.json。
https://stackoverflow.com/questions/72142661
复制相似问题