我有React应用程序和一个文件,我想存储与api相关的东西。从使用module.exports的文件导入Webpack
const proxy = require('http-proxy-middleware');
const path = require('path');
//.....
const targetApi = (objectWithUrlEntries) => {
Object.keys(objectWithUrlEntries).forEach((key) => {
objectWithUrlEntries[key] = path.join('/api/', objectWithUrlEntries[key]);
});
};
module.exports.proxyExpressCalls = proxyExpressCalls;
module.exports.devServerProxyConfig = devServerProxyConfig;
module.exports.targetApi = targetApi;
这些东西中的一部分将被webpack本身使用,其中一些将被用于app内部(正确地定位api调用)。
然而,当我试图导入我的应用程序的东西:
// @flow
import { buildUrl } from 'data/utils';
import type { Axios } from './flow.types';
import { targetApi } from './api';
console.log(targetApi());
我得到的错误。在终端:
警告在./src/data/redux/api/user.js 6:12-21“出口 'targetApi' 未在 './api'
发现在浏览器:
api.js?d669:39 Uncaught TypeError: Cannot set property 'proxyExpressCalls' of undefined
at Object.eval (api.js?d669:39)
at eval (api.js:60)
at Object../src/data/redux/api/api.js (client.bundle.js:11620)
at __webpack_require__ (client.bundle.js:708)
at fn (client.bundle.js:113)
at eval (user.js:15)
at Object../src/data/redux/api/user.js (client.bundle.js:11668)
at __webpack_require__ (client.bundle.js:708)
at fn (client.bundle.js:113)
at eval (user.js:18)
所以问题是,应用程序被捆绑时commonjs
出口失败,但如果我会用ES6 export
语法然后Node
会失败