NpmInstallPlugin - TypeError:undefined不是函数

时间:2016-06-19 02:02:00

标签: javascript node.js npm webpack

我是webpack的超级新手。我正在用babel-loader开始一个新项目并做出反应。但是,我在启动服务器时看到此错误:

/home/mfebrianto/dev/mfebrianto/food/Menubook/node_modules/npm-install-webpack-plugin/src/plugin.js:32
  this.options = Object.assign(installer.defaultOptions, options);
                       ^

TypeError: undefined is not a function
    at new NpmInstallPlugin (/home/mfebrianto/dev/mfebrianto/food/Menubook/node_modules/npm-install-webpack-plugin/src/plugin.js:32:25)
    at Object.<anonymous> (/home/mfebrianto/dev/mfebrianto/food/Menubook/webpack.config.js:79:13)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at module.exports (/usr/lib/node_modules/webpack-dev-server/node_modules/webpack/bin/convert-argv.js:80:13)
    at Object.<anonymous> (/usr/lib/node_modules/webpack-dev-server/bin/webpack-dev-server.js:55:48)

这是我的webpack.config.js:

const path = require('path');
const merge = require('webpack-merge');
const webpack = require('webpack');
const NpmInstallPlugin = require('npm-install-webpack-plugin');

const TARGET = process.env.npm_lifecycle_event;
const PATHS = {
    app: path.join(__dirname, 'app'),
    build: path.join(__dirname, 'build')
};

process.env.BABEL_ENV = TARGET;

const common = {
// Entry accepts a path or an object of entries. We'll be using the
// latter form given it's convenient with more complex configurations.
    entry: {
        app: PATHS.app
    },
    resolve: {
        extensions: ['', '.js', '.jsx']
    },
    output: {
        path: PATHS.build,
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
                 // Test expects a RegExp! Note the slashes!
                 test: /\.css$/,
                 loaders: ['style', 'css'],
                 // Include accepts either a path or an array of paths.
                 include: PATHS.app
            },
            // Set up jsx. This accepts js too thanks to RegExp
            {
                test: /\.jsx?$/,
                // Enable caching for improved performance during development
                // It uses default OS directory by default. If you need something
                // more custom, pass a path to it. I.e., babel?cacheDirectory=<path>
                loaders: ['babel?cacheDirectory'],
                // Parse only app files! Without this it will go through entire project.
                // In addition to being slow, that will most likely result in an error.
                include: PATHS.app
            }
        ]
    }
};

// Default configuration. We will return this if
// Webpack is called outside of npm.
if(TARGET === 'start' || !TARGET) {
    module.exports = merge(common, {
        devtool: 'eval-source-map',
        devServer: {
            contentBase: PATHS.build,
            // Enable history API fallback so HTML5 History API based
            // routing works. This is a good default that will come
            // in handy in more complicated setups.
            historyApiFallback: true,
            hot: true,
            inline: true,
            progress: true,
            // Display only errors to reduce the amount of output.
            stats: 'errors-only',
            // Parse host and port from env so this is easy to customize.
            //
            // If you use Vagrant or Cloud9, set
            // host: process.env.HOST || '0.0.0.0';
            //
            // 0.0.0.0 is available to all network devices unlike default
            // localhost
            host: process.env.HOST,
            port: process.env.PORT || 9000
        },
        plugins: [
            new webpack.HotModuleReplacementPlugin(),
            new NpmInstallPlugin({
                save: true, // --save
                peerDependencies: true
            })
        ]
    });
}

if(TARGET === 'build') {
    module.exports = merge(common, {});
}

如何让NpmInstallPlugin在我的项目中运行?

1 个答案:

答案 0 :(得分:0)

确保使用节点版本4或更高版本。 您可以在控制台中使用命令检查它:

node -v

此问题可能与not supporting Object.assign() in node version 0.x有关。