Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module not found: Error: Can't resolve '../moment' with Webpack and TypeScript #4945

Closed
pmwmedia opened this issue Jan 11, 2019 · 9 comments
Closed

Comments

@pmwmedia
Copy link

pmwmedia commented Jan 11, 2019

Describe the bug

If I try to compile my TypeScript app with webpack, I get an error for each locale that '../moment' can't be resolved.

This error is repeated for every other locale:

ERROR in ./node_modules/moment/locale/km.js
Module not found: Error: Can't resolve '../moment' in 'C:\PROGS\dev\var\private\moment-test\node_modules\moment\locale'
 @ ./node_modules/moment/locale/km.js 5:50-70
 @ ./node_modules/moment/locale sync ^\.\/.*$
 @ ./node_modules/moment/moment.js
 @ ./src/app.ts

To Reproduce

src/app.ts

import * as moment from "moment";
console.log(moment().format("YYYY"));

package.json

{
	"private": true,
	"dependencies": {
		"moment": "2.23.0"
	},
	"devDependencies": {
		"awesome-typescript-loader": "5.2.1",
		"typescript": "3.2.2",
		"webpack": "4.28.3",
		"webpack-cli": "3.2.1"
	}
}

tsconfig.json

{ "include": [ "./src/" ] }

webpack.config.js

module.exports = {
    entry: "./src/app.ts",
    output: { filename: "bundle.js" },
    resolve: { extensions: [".ts"] },
    module: { rules: [ { test: /\.ts$/, use: { loader: "awesome-typescript-loader" } } ] }
}

Expected behavior

yarn run webpack -p should work without any error.

Moment-specific environment

  • Windows 10
  • Node 10.15.0
  • moment 2.23.0
  • TypeScript 3.2.2
  • Webpack 4.28.3
  • Time zone: CET (UTC+1)

Please run the following code in your environment and include the output:

console.log((new Date()).toString())
console.log((new Date()).toLocaleString())
console.log((new Date()).getTimezoneOffset())
console.log(navigator.userAgent)
console.log(moment.version)

This code fails with the same errors!

@marwahaha
Copy link
Member

Thanks for opening this request.
This is a great question for Stack Overflow.

@pmwmedia
Copy link
Author

pmwmedia commented Jan 17, 2019

This is a bug, isn't it? Stack Overflow is good for question, but nobody will fix bugs there.

I interpret your answer to mean that you do not support WebPack with TypeScript. Too bad!

@marwahaha
Copy link
Member

There are various tutorials on the internet about using Moment with Webpack and TypeScript. It seems that many others use them together just fine. If more people have this issue, and there is enough community knowledge to fix it, then we can try to do so.

@gary-archer
Copy link

I had the same problem by the way - seems to be caused by moment locales, which I didn't care about. I found a solution in this online article, which was good enough in my case, to ignore the locales plugin:

plugins: [
new webpack.IgnorePlugin(/^./locale$/, /moment$/)
]

This seems like a pretty mainstream usage scenario, so I'd recommend linking to the above page from the TypeScript section of the docs.

@marwahaha
Copy link
Member

@authguidance-examples PRs are welcome at https://github.com/moment/momentjs.com !

@sanjayyadav19
Copy link

I was facing the same issue, here was the issue-

  • I installed it using customised command npm i --save react-moment, which just got added in package.json file but lib was not there in node_modules, so to do so I execute
  • npm install --save moment react-moment
  • and now it is working as expected.

@adrihle
Copy link

adrihle commented Jul 13, 2019

anyone know why always show like "a few seconds ago"? whatever the date is it

@emidsnithin
Copy link

i added the moment in reactjs : import * as moment from "moment"; and in the dependecy injection added : "moment": "2.23.0", but getting the

undefined error... in reactjs,
Kindly help...

@chiefkana
Copy link

Why isn't this branch closed? Last comment on December 2nd, 2019. @marwahaha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants