简体   繁体   English

在 webpack 中的 js 文件中导入 moment.js

[英]Import moment.js in js file in webpack

In my project I use webpack and npm.在我的项目中,我使用 webpack 和 npm。 I installed moment.js : npm install moment .我安装了 moment.js : npm install moment Then I want to import it in my app.js file: import moment from "moment" .然后我想在我的 app.js 文件中导入它: import moment from "moment"

But it doesn't work.但它不起作用。 I get: Can't resolve moment in ...我得到:无法解决...的时刻

I try let moment = require('moment');我尝试let moment = require('moment'); but I get the same error.但我得到了同样的错误。

But I can require it in my webpack.config.js file without errors.但是我可以在我的 webpack.config.js 文件中要求它而不会出错。

My app.js file is located on /myapp/frontend/app.js我的 app.js 文件位于/myapp/frontend/app.js

My webpack.config.js file on: /myapp/webpack.config.js我的 webpack.config.js 文件位于: /myapp/webpack.config.js

So, please explain why I can't require moment in my app.js and how can I do this?所以,请解释为什么我不能在我的 app.js 中要求 moment 以及我该怎么做?

My config file:我的配置文件:

const webpack = require("webpack");
const NODE_ENV = process.env.NODE_ENV || "development"
const path = require('path');

//example of successfull importing
let moment = require('moment');
console.log(moment(new Date()));

module.exports = {
    context: __dirname + "/frontend",
    entry: {
        app:"./app"
    },
    output: {
        path: path.resolve(__dirname,"./public/js"),
        publicPath:"/public/js/",//public path in internet
        filename: "build.js"
    },

    watch: NODE_ENV == "development",
    watchOptions:{
        aggregateTimeout:100//default = 300
    },
    devtool: NODE_ENV == "development"?"cheap-inline-module-source-map":"cheap-source-map",
                                      //cheap-inline-module-source-map - to see sources in build.js
                                      //eval - fastest

    resolve:{
        modules: ['node-modules'],
        extensions:['.js']
    },
    module:{
        loaders:[{
            test: /\.js$/,
            loader:"babel-loader?presets[]=es2015",
            exclude: /node_modules/
        }]
    }
};

if(NODE_ENV == "production"){
    module.exports.plugins.push(
        new webpack.optimize.UglifyJsPlugin({
            compress:{
                warnings:false,
                unsafe:true,
                drop_console:true
            }
        })
    );
}

It is my tree structure without node_modules folder:这是我没有 node_modules 文件夹的树结构:

这是我没有 node_modules 文件夹的树结构

SOLVING OF PROBLEM : problem was in my configuration:问题解决:问题出在我的配置中:

  resolve:{
            modules: ['node-modules'],
            extensions:['.js']
        },

There is node-modules is wrong, must be node_modules .node-modules是错误的,必须是node_modules Simple typo..简单的错别字。。

Without knowing a bit more about your file structure it's difficult to be certain as to why, but the issue is probably that your webpack config is not finding the moment module in your node_modules.在不了解更多文件结构的情况下,很难确定原因,但问题可能是您的 webpack 配置没有在 node_modules 中找到 moment 模块。

As a test, ran the following:作为测试,运行以下命令:

//webpack.js
const path = require('path');

module.exports = {
  entry: path.join(__dirname, '..', 'public', 'js', 'index.js'),
  output: {
    filename: 'app.js',
    path: path.resolve(__dirname, '..', 'public', 'dist'),
  },
};

and then with moment and jquery installed via npm install --save jquery moment , I made a index.js file:然后通过npm install --save jquery moment moment 和 jquery,我创建了一个 index.js 文件:

import $ from jquery;
import moment from moment;

const m = moment(); 

No build errors and no runtime errors when included on the HTML page.包含在 HTML 页面中时,没有构建错误和运行时错误。 Try starting simply first and then build up from there on your webpack config.尝试先简单地开始,然后在您的 webpack 配置中从那里开始构建。 Also, I'm not sure if webpack does anything with package.json but I noticed you didn't signal the --save option.另外,我不确定 webpack 是否对 package.json 执行任何操作,但我注意到您没有发出 --save 选项的信号。 It's a good habit to get into.入门是个好习惯。

For my issue, I have change import {moment} from 'moment' to import * as moment from 'moment';对于我的问题,我已将import {moment} from 'moment'更改为import * as moment from 'moment'; and it's working now!它现在开始工作了!

就我而言,当我放置window.moment = require('moment');时,问题就解决了window.moment = require('moment');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM