我有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
會失敗