最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - module.exports function is not a function - Stack Overflow

programmeradmin2浏览0评论

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

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

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

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`
    }
};
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jun 5, 2017 at 13:49 Leon GabanLeon Gaban 39.1k122 gold badges349 silver badges550 bronze badges 5
  • 2 Spelling? Should it not be endpoints? (enpoints missing d) – Arg0n Commented Jun 5, 2017 at 13:50
  • Sorry, I had fixed the spelling and it still didn't work, updating my question now. – Leon Gaban Commented Jun 5, 2017 at 13:52
  • Do you use Babel so you can export ? Commonjs for module.exports ? – Nevosis Commented Jun 5, 2017 at 13:53
  • How are you generating that code error? That's a fancy background. – evolutionxbox Commented Jun 5, 2017 at 13:57
  • @evolutionxbox I'm using iterm2 :) with a transparent background, and I have some space background ing through. – Leon Gaban Commented Jun 5, 2017 at 13:59
Add a ment  | 

1 Answer 1

Reset to default 3

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;
发布评论

评论列表(0)

  1. 暂无评论