module.exports函数不是函数

时间:2017-06-05 13:49:30

标签: javascript import webpack export webpack-2

我试图将endpoints.js文件放入我的webpack.config.js

预期

端点.js需要正确,然后根据process.env.NODE_ENV

设置自定义api文件

结果

enter image description here

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

1 个答案:

答案 0 :(得分:2)

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

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

<?php 

$args = array( 'numberposts' => 18,   'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");

$postslist = get_posts( $args );

echo '<ul id="latest_stories">';

foreach ($postslist as $post) :  setup_postdata($post); ?> 

<li><a href="<?php the_permalink(); ?>" title="<?php the_title();?>">

<span class="s_thumb"> <?php the_post_thumbnail( 'full' ); ?> </span>

<span class="s_title"> <?php the_title(); ?> </span></a>

<span class="s_cotegories">More Stories from <?php the_category( ', ' ); ?></span></li>

<?php endforeach; ?>