Next.js: webpack.DefinePlugin is not working on next.config.js

Created on 5 Jul 2017  路  7Comments  路  Source: vercel/next.js

next.config.js

image

pages/index.js

image

result

image

Does it mean I lost something @@?

Most helpful comment

I wanna submit that this issue gets reopened.

While you have solved the underlining issue for @hank7444 there may be other use-cases and workflows that still depend on DefinePlugin.

All to say I believe the issue still stands that the variables defined in webpack.DefinePlugin do not exist in the bundled files.

All 7 comments

I wanna submit that this issue gets reopened.

While you have solved the underlining issue for @hank7444 there may be other use-cases and workflows that still depend on DefinePlugin.

All to say I believe the issue still stands that the variables defined in webpack.DefinePlugin do not exist in the bundled files.

Completely agree, this is still not possible. ReferenceError is always thrown. @timneutkens your solution has caveats as says the readme

why did you close this issue,what's the solution? i also met this problem too recently!

what's the solution? i also met this problem too recently! @timneutkens

it works for me

const webpack = require('webpack'); // eslint-disable-line
const FRONTEND_ENV_KEYS = [
    'NODE_ENV',
    'IS_STATIC',
    'API_URL',
    'CONTENTFUL_SPACE',
    'CONTENTFUL_ACCESS_TOKEN'
];

const envPlugin = FRONTEND_ENV_KEYS.reduce(
    (result, key) =>
        Object.assign({}, result, {
            [`process.env.${key}`]: JSON.stringify(process.env[key])
        }),
    {}
);

module.exports = {
    webpack(cfg) {
        // make selected env vars avail on client
        cfg.plugins.push(new webpack.DefinePlugin(envPlugin));
        return cfg;
    }
    // exportPathMap: require('./next.exportPathMap').exportPathMap
};

^ not sure why everyone downvoted this. it works fine and it's something I've been using in production for at least a year now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

havefive picture havefive  路  3Comments

pie6k picture pie6k  路  3Comments

formula349 picture formula349  路  3Comments

irrigator picture irrigator  路  3Comments

timneutkens picture timneutkens  路  3Comments