简体   繁体   English

module.exports函数不是函数

[英]module.exports function is not a function

I'm trying to require an endpoints.js file into my webpack.config.js 我试图在我的webpack.config.js中要求一个endpoints.js文件

Expected 预期

endpoints.js gets required correctly then sets a custom api file depending on the process.env.NODE_ENV 正确获取了endpoints.js,然后根据process.env.NODE_ENV设置了自定义api文件

Results 结果

在此处输入图片说明

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

TypeError: endpoints is not a function TypeError:端点不是函数


Webpack.config.js 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 endpoints.js

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

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

I also tried the following, but got an Unexpected token export 我也尝试了以下操作,但是导出意外的令牌

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

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

Ah I was using module.exports wrong, however it looked correct according to this site . 啊,我在使用module.exports时出错,但是根据此站点 ,它看起来正确。

This is how I needed to use module.exports to export out my endpoints function. 这就是我需要使用module.exports导出端点功能的方式。

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

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

module.exports = endpoints;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM