Open jackieli123723 opened 2 years ago
import Koa from 'koa'; import cookie from 'koa-cookie'; const app = Koa(); app.use(cookie());
报错
Hint: hit control+c anytime to enter REPL. file:///home/runner/import-koa/index.js:11 app.use(cookie()); ^
TypeError: cookie is not a function at file:///home/runner/import-koa/index.js:11:9
卧槽 这个是什么操作分析 是esm 不支持
koa-cookie源码
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; } function cookie() { return (() => { var _ref = _asyncToGenerator(function* (context, next) { const cookieHeader = context.headers.cookie; if (cookieHeader) { const cookies = cookieHeader.split(';'); context.cookie = {}; cookies.forEach(function (item) { const crumbs = item.split('='); if (crumbs.length > 1) context.cookie[crumbs[0].trim()] = crumbs[1].trim(); }); } yield next(); }); return function (_x, _x2) { return _ref.apply(this, arguments); }; })(); } exports.default = cookie;
修复1删除Object.defineProperty(exports, "__esModule", { value: true}); && exports.default = cookie;
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; } function cookie() { return (() => { var _ref = _asyncToGenerator(function* (context, next) { const cookieHeader = context.headers.cookie; if (cookieHeader) { const cookies = cookieHeader.split(';'); context.cookie = {}; cookies.forEach(function (item) { const crumbs = item.split('='); if (crumbs.length > 1) context.cookie[crumbs[0].trim()] = crumbs[1].trim(); }); } yield next(); }); return function (_x, _x2) { return _ref.apply(this, arguments); }; })(); } // way1 ok esm fix 参考 ts-sort-utils export { cookie }
使用方式
import {cookie} from './middleware/koa-cookie.js'; const app = new Koa(); app.use(cookie());
修复方式2 way1 改为 //WAY2 export default cookie;
//way2 import cookie from './middleware/koa-cookie.js'; const app = new Koa(); app.use(cookie());
修复方式3 包装下koa-cookie2.js import cookie from "koa-cookie"; export default cookie.default;
//way3 import cookie from './middleware/koa-cookie2.js'; const app = new Koa(); app.use(cookie());
修复方式4 使用方式
import cookie from 'koa-cookie'; const app = new Koa(); app.use(cookie.default());
报错
Hint: hit control+c anytime to enter REPL. file:///home/runner/import-koa/index.js:11 app.use(cookie()); ^
TypeError: cookie is not a function at file:///home/runner/import-koa/index.js:11:9
卧槽 这个是什么操作分析 是esm 不支持
koa-cookie源码
修复1删除Object.defineProperty(exports, "__esModule", { value: true}); && exports.default = cookie;
使用方式
修复方式2 way1 改为 //WAY2 export default cookie;
使用方式
修复方式3 包装下koa-cookie2.js import cookie from "koa-cookie"; export default cookie.default;
使用方式
修复方式4 使用方式