【问题标题】:module.exports function is not a functionmodule.exports 函数不是函数
【发布时间】:2017-06-05 13:49:29
【问题描述】:

我正在尝试将 endpoints.js 文件添加到我的 webpack.config.js 中

预期

正确地需要endpoints.js 然后根据process.env.NODE_ENV设置一个自定义api文件

结果

const api = endpoints(process.env.NODE_ENV);

TypeError:端点不是函数


Webpack.config.js

const webpack = require('webpack')
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
const dist = path.resolve(__dirname, "dist");
const src = path.resolve(__dirname, "src");
const endpoints = require("./src/endpoints");
const api = endpoints(process.env.NODE_ENV);

console.log('webpack endpoints', endpoints);
console.log('webpack api', api);

endpoints.js

module.exports = {
    endpoints: function(env) {
        let prefix = env === 'development' ? 'http://localhost' : '';

        return {
            "login": `${prefix}/app/api/login`
        }
    }
}

我也尝试了以下方法,但得到了意外的令牌导出

export default function endpoints(env) {
    let prefix = env === 'development' ? 'http://localhost' : '';

    return {
        "login": `${prefix}/app/api/login`
    }
};

【问题讨论】:

  • 拼写?不应该是endpoints吗? (enpoints 缺少d
  • 抱歉,我已经修正了拼写,但仍然无法正常工作,现在更新我的问题。
  • 你使用 Babel 来导出吗?用于 module.exports 的 Commonjs ?
  • 你是如何产生那个代码错误的?这是一个花哨的背景。
  • @evolutionxbox 我正在使用具有透明背景的 iterm2 :),并且我有一些空间背景。

标签: javascript import webpack export webpack-2


【解决方案1】:

啊,我使用 module.exports 错误,但根据 to this site 看起来是正确的。

这就是我需要使用 module.exports 导出端点函数的方式。

function endpoints(env) {
    let prefix = env === 'development' ? 'http://localhost' : '';

    return {
        "login": `${prefix}/app/api/login`
    }
}

module.exports = endpoints;

【讨论】:

    猜你喜欢
    • 2012-04-11
    • 1970-01-01
    • 2018-03-28
    • 2012-05-14
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2015-10-08
    相关资源
    最近更新 更多