Search code examples
javascriptimportwebpackexportwebpack-2

module.exports function is not a function


I'm trying to require an endpoints.js file into my webpack.config.js

Expected

endpoints.js gets required correctly then sets a custom api file depending on the process.env.NODE_ENV

Results

enter image description here

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

TypeError: endpoints is not a function


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`
        }
    }
}

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`
    }
};

Solution

  • Ah I was using module.exports wrong, however it looked correct according to this site.

    This is how I needed to use module.exports to export out my endpoints function.

    function endpoints(env) {
        let prefix = env === 'development' ? 'http://localhost' : '';
    
        return {
            "login": `${prefix}/app/api/login`
        }
    }
    
    module.exports = endpoints;