In my project I use webpack and npm. I installed moment.js : npm install moment
. Then I want to import it in my app.js file: import moment from "moment"
.
But it doesn't work. I get: Can't resolve moment in ...
I try let moment = require('moment');
but I get the same error.
But I can require it in my webpack.config.js file without errors.
My app.js file is located on /myapp/frontend/app.js
My webpack.config.js file on:/myapp/webpack.config.js
So, please explain why I can't require moment in my app.js and how can I do this?
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:
SOLVING OF PROBLEM: problem was in my configuration:
resolve:{
modules: ['node-modules'],
extensions:['.js']
},
There is node-modules
is wrong, must be node_modules
. Simple typo..