【发布时间】:2018-03-27 18:51:09
【问题描述】:
我有 React 应用程序和一个文件,我想在其中存储与 api 相关的内容。
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 自己使用,而另一些会在应用程序内部使用(以正确定位 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 "export '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 将失败。
【问题讨论】:
标签: node.js reactjs webpack ecmascript-6 commonjs