【问题标题】:how to load moment.js into webpack如何将 moment.js 加载到 webpack
【发布时间】:2017-01-24 16:57:39
【问题描述】:

我想将 moment.js 作为插件加载到 webpack 中。有谁知道该怎么做?尝试在我的主干视图中调用 define('moment') 时收到错误“时刻”未定义

这是我的 webpack 配置:

app.use(webpackMiddleware(webpack({
    context: __dirname + '/src',
    entr: './admin-entry.js',
    entry: {
        'admin-entry': './admin-entry.js',
        'admin-login': './admin-login.js'
    },
    output: {
        path: '/webpack',
        publicPath: '/webpack',
        filename: '[name].js'
    },

    module: {
        noParse: [
            new RegExp(jqueryPath),
            new RegExp(lodashPath),
            new RegExp(qPath),
            new RegExp(cookiePath),
            new RegExp(momentPath),
            new RegExp(dateRangePickerPath)
        ]

    },
    resolve: {
        alias: {
            'jquery': jqueryPath,
            'lodash': lodashPath,
            'underscore': lodashPath,
            'backbone': backbonePath,
            'cookie': cookiePath,
            'daterangepicker': dateRangePickerPath,
            'moment': momentPath,
            'q': qPath
        },
        modulesDirectories: [
            'node_modules'
        ]
    },
    devtool: 'eval-source-map',
    plugins: [
        new webpack.SourceMapDevToolPlugin('webpack/[file].map', null, '[absolute-resource-path]', '[absolute-resource-path]')
    ]
}), {
    noInfo: false,
    quiet: false,
    lazy: false,
    // switch into lazy mode
    // that means no watching, but recompilation on every request
    watchOptions: {
        aggregateTimeout: 300,
        poll: true
    },
    publicPath: '/webpack',
    // public path to bind the middleware to
    // use the same as in webpack

    stats: {
        colors: true
    }
}));

【问题讨论】:

  • 您是否通过 npm 安装了时刻?对我来说,alias: { "moment": 'moment/min/moment-with-locales' 没问题

标签: javascript webpack momentjs


【解决方案1】:

首先,确保您使用 npm 命令安装 moment

npm 安装时刻

现在你应该在你的 node_modules 文件夹中有时间了。然后你应该将 moment 作为全局变量添加到你的项目中,所以你应该更改 Webpack 来为你解决它。为此,请更改你的 webpack.config.js 文件,如下所示:

    plugins: [
      .... 
      new webpack.ProvidePlugin({
          moment : 'moment'
      })
      ....
    ]

【讨论】:

    猜你喜欢
    • 2014-10-12
    • 2016-10-21
    • 2018-04-13
    • 2021-03-03
    • 2015-11-28
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多