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

TS2307: Cannot find module 'moment'. #5486

Closed
gayankasun opened this issue May 1, 2020 · 48 comments
Closed

TS2307: Cannot find module 'moment'. #5486

gayankasun opened this issue May 1, 2020 · 48 comments

Comments

@gayankasun
Copy link

@gayankasun gayankasun commented May 1, 2020

I m having issue with importing moment in to my angular 8 project.

Angular version = 8.3.26
Type script version = 3.6.2
moment version = 2.25.0

i'm getting error TS2307: Cannot find module 'moment'. and i have used
import moment from 'moment'; .

i have add "moduleResolution": "node" and "allowSyntheticDefaultImports": true, in tsConfig

can any one help on this

@codebrent
Copy link

@codebrent codebrent commented May 1, 2020

try version 2.24, my tests started to fail on new version but fine with previous

@gayankasun
Copy link
Author

@gayankasun gayankasun commented May 1, 2020

try version 2.24, my tests started to fail on new version but fine with previous

I have tried. but same error came.

@codebrent
Copy link

@codebrent codebrent commented May 1, 2020

what does the line for moment in package.json look like?

@acharyaks90
Copy link

@acharyaks90 acharyaks90 commented May 1, 2020

its working with 2.24.0 may be deployment error in 2.25.0
Please deploy the new version with fix.

@rilyu
Copy link

@rilyu rilyu commented May 1, 2020

if you use yarn, add resolutions block to package.json to override versions of sub-dependency, it work for me

  "resolutions": {
    "moment": "2.24.0"
  }
@v1shva
Copy link

@v1shva v1shva commented May 1, 2020

Seems that Typescript module resolution can't find moment. Using a relative path works ./node_modules/moment/moment. @codebrent Thanks, it's now working with 2.24.0 version.

@jafin
Copy link

@jafin jafin commented May 1, 2020

In 2.25 this has been added to package.json. But if I understand no corresponding location exists for the typedef file. Perhaps this broke the typings?

"typesVersions": {
    ">=3.1": {
      "*": [
        "ts3.1-typings/*"
      ]
    }
  },
@gayankasun
Copy link
Author

@gayankasun gayankasun commented May 1, 2020

its working with 2.24.0 may be deployment error in 2.25.0
Please deploy the new version with fix.

No its not working for me

@gayankasun
Copy link
Author

@gayankasun gayankasun commented May 1, 2020

what does the line for moment in package.json look like?

"moment": "^2.25.0",

@gayankasun
Copy link
Author

@gayankasun gayankasun commented May 1, 2020

Capture
All moment imports returning error

@gayankasun
Copy link
Author

@gayankasun gayankasun commented May 1, 2020

what does the line for moment in package.json look like?

111

@codebrent
Copy link

@codebrent codebrent commented May 1, 2020

try changing to "moment": "2.24.0", make sure that there isn't a ^ in it. delete node_models and re npm install. also if you have package-lock.json you may need to delete it. Otherwise npm will keep installing 2.25

@mthmulders
Copy link

@mthmulders mthmulders commented May 1, 2020

I'm seeing the same issue, and according to the Dependabot Compatibility score for moment, I'm not the only one.

Downgrading to 2.24.0 may be a a work-around, but it effectively prevents at least some users from using 2.25.0.

@OrmEmbaar
Copy link

@OrmEmbaar OrmEmbaar commented May 1, 2020

try changing to "moment": "2.24.0", make sure that there isn't a ^ in it. delete node_models and re npm install. also if you have package-lock.json you may need to delete it. Otherwise npm will keep installing 2.25

The safer, canonical way to do this is simply npm install moment@2.24.0. You do not need to delete anything or change anything manually.

I can confirm that downgrading to 2.24 fixes the issue.

@gayankasun
Copy link
Author

@gayankasun gayankasun commented May 1, 2020

try changing to "moment": "2.24.0", make sure that there isn't a ^ in it. delete node_models and re npm install. also if you have package-lock.json you may need to delete it. Otherwise npm will keep installing 2.25

The safer, canonical way to do this is simply npm install moment@2.24.0. You do not need to delete anything or change anything manually.

I can confirm that downgrading to 2.24 fixes the issue.

In locally its running after down grade to 2.24.0 . but when i deploy this to Azure to its giving same error.
azure error

My question is if it is same package.json running both local and azure why its causing error on import moment.
i have attached error log in server as well

@shernshiou
Copy link

@shernshiou shernshiou commented May 1, 2020

I can confirm 2.25.0 give this issue but not at 2.24.0.

@ijgithub
Copy link

@ijgithub ijgithub commented May 1, 2020

I also confirm that 2.25.0 give this issue. Moment 2.24.0 works fine.

I experienced this problem during docker image build.

alan-agius4 added a commit to alan-agius4/angular-cli that referenced this issue May 1, 2020
Moment 2.25.0 typings are broken and results in red ci, for the time being we use a previous version of moment

See: moment/moment#5486
@leafqi
Copy link

@leafqi leafqi commented May 1, 2020

its not working with 2.24.0 for me

@miluxmilux
Copy link

@miluxmilux miluxmilux commented May 1, 2020

Downgrading fixed the issue here too. Maybe the TypeScript version may play a role in having the bug or not?

@zhuyuedlut
Copy link

@zhuyuedlut zhuyuedlut commented May 1, 2020

I use moment-timezone
Its not working with fixing moment version to 2.24.0 in package.json

@Poyoman39
Copy link

@Poyoman39 Poyoman39 commented May 1, 2020

@zhuyuedlut @leafqi

its not working with 2.24.0

you probably did not downgrade to 2.24.0. Remove your package-lock file and node_modules folder, then run this command :

npm install --save --save-exact moment@2.24
npm install

@simoami
Copy link

@simoami simoami commented May 1, 2020

I'm also experiencing the same issue in a typescript project and the build is failing all of a sudden because we didn't pin the dependency in package.json.

error TS2307: Cannot find module 'moment'.
error Command failed with exit code 2.

pinning the version to "2.24.0" fixes the issue.

@abhinavsingi
Copy link

@abhinavsingi abhinavsingi commented May 1, 2020

Can try with resolutions field in package.json

Add

"resolutions": {
"moment": "2.24.0"
}

@leafqi
Copy link

@leafqi leafqi commented May 1, 2020

@zhuyuedlut @leafqi

its not working with 2.24.0

you probably did not downgrade to 2.24.0. Remove your package-lock file and node_modules folder, then run this command :

npm install --save --save-exact moment@2.24
npm install

thx. my problem has been solved. its working with 2.24.0
correct usage : "moment": "2.24.0"
wrong usage : "moment": "^2.24.0"

@shanmuganathanvel
Copy link

@shanmuganathanvel shanmuganathanvel commented May 1, 2020

"moment": "2.24.0" is working for me also..

@tomfordweb
Copy link

@tomfordweb tomfordweb commented May 1, 2020

Same, locking in 2.24.0 fixed it.

This is on an angular 7.2 app built with webpack ^4.2.0

@kumar-sanjeet1
Copy link

@kumar-sanjeet1 kumar-sanjeet1 commented May 1, 2020

Yes, 2.24.0 is working fine. Does anyone know what is the change they've made?

rikoe added a commit to AdaptiveConsulting/ReactiveTraderCloud that referenced this issue May 1, 2020
@azalpacir
Copy link

@azalpacir azalpacir commented May 1, 2020

Same, issue got fixed by setting fixed version to "2.24" instead of "^2.24"

@andrewstec
Copy link

@andrewstec andrewstec commented May 1, 2020

Do we have an ETA for this fix?

It's Friday and I am wondering if I should downgrade to 2.24.0 before the weekend.

@andrewstec
Copy link

@andrewstec andrewstec commented May 1, 2020

try changing to "moment": "2.24.0", make sure that there isn't a ^ in it. delete node_models and re npm install. also if you have package-lock.json you may need to delete it. Otherwise npm will keep installing 2.25

Thanks. Issue got fixed by downgrading the version to 2.24.0

You are no longer living in the moment though :)

@ArunKumar-Annamalai
Copy link

@ArunKumar-Annamalai ArunKumar-Annamalai commented May 1, 2020

try changing to "moment": "2.24.0", make sure that there isn't a ^ in it. delete node_models and re npm install. also if you have package-lock.json you may need to delete it. Otherwise npm will keep installing 2.25

Thanks, Issue got fixed by downgrading version from 2.25.0 to 2.24.0. Also I did "^2.24.0" to "2.24.0"

@Unkn0wn0x
Copy link

@Unkn0wn0x Unkn0wn0x commented May 1, 2020

If the downgrade von 2.25.0 to 2.24.0 does not work and you get the same error again, try to uninstall the module completely and re-install it again.

Using npm:

  1. npm uninstall moment
  2. npm install moment@2.24.0

Hope it helps.

Cheers
Unkn0wn0x

@J1445
Copy link

@J1445 J1445 commented May 1, 2020

Any Idea when the fix will be released ? I see this is a broken issue and I don't have access to make changes in Master to set the moment version to 2.24 now and production builds is failing left and right.
Please share release ETA thanks

@WaseemRakab
Copy link

@WaseemRakab WaseemRakab commented May 1, 2020

I had this issue also, and i fixed it
by removing those lines
"typesVersions": { ">=3.1": { "*": [ "ts3.1-typings/*" ] } },
lines 150-156
in
.\node_modules\moment\package.json
and after i did this, it's working completely fine with version 2.25

@azalpacir
Copy link

@azalpacir azalpacir commented May 1, 2020

I had this issue also, and i fixed it
by removing those lines
"typesVersions": { ">=3.1": { "*": [ "ts3.1-typings/*" ] } },
lines 150-156
in
.\node_modules\moment\package.json
and after i did this, it's working completely fine with version 2.25

this will work locally, but for CI pipelines, I doubt

thdk added a commit to thdk/team-timesheets that referenced this issue May 1, 2020
@sandman45
Copy link

@sandman45 sandman45 commented May 1, 2020

Thanks everyone :D 2.24.0 worked for me as well..

@flowck
Copy link

@flowck flowck commented May 1, 2020

Downgraded to 2.24.0, and it's working fine.

@ichernev
Copy link
Contributor

@ichernev ichernev commented May 1, 2020

2.25.1 released

@ichernev ichernev closed this May 1, 2020
@WaseemRakab
Copy link

@WaseemRakab WaseemRakab commented May 1, 2020

Tested 2.25.1 , issue is gone. Thanks

@andrewstec
Copy link

@andrewstec andrewstec commented May 1, 2020

Yeah, I tested this too and didn't experience any issues.

@mike-marcacci
Copy link

@mike-marcacci mike-marcacci commented May 1, 2020

Indeed, upgraded minutes after it was published and all is well: thanks for the speedy fix!

@gayankasun
Copy link
Author

@gayankasun gayankasun commented May 2, 2020

Finally it works for me. downgrading to 2.24.0 working. but make sure to put "moment": "2.24.0"
instead of "moment": "^2.24.0"
azure error

@darwintnt
Copy link

@darwintnt darwintnt commented May 3, 2020

version 2.25.1 solved the Cannot find module 'moment' problem, but I am testing in an Angular 8 application and it throws the following error:

WARNING in ./node_modules/moment/src/lib/locale/locales.js
Module not found: Error: Can't resolve './locale' in '/home/project/node_modules/moment/src/lib/ locale'

has anyone else experienced this failure?

@jzrandall
Copy link

@jzrandall jzrandall commented May 3, 2020

I'm also getting warnings in 2.25.1 about some of the other functions I'm referencing:

"export 'utc' (imported as 'moment') was not found in 'moment'
"export 'max' (imported as 'moment') was not found in 'moment'
"export 'min' (imported as 'moment') was not found in 'moment'
@Coding-Kiwi
Copy link

@Coding-Kiwi Coding-Kiwi commented Sep 11, 2020

For me it was

    "moment": "2.24.0",
    "moment-timezone": "^0.5.31",
@quizdeveloper
Copy link

@quizdeveloper quizdeveloper commented Oct 3, 2020

I think you can update moment to version 2.29.0

npm uninstall moment
npm install moment@2.29.0

TS2307: Cannot find module 'moment' in Angular 8

@novelmedicare
Copy link

@novelmedicare novelmedicare commented Jun 1, 2021

npm install moment --save

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

Successfully merging a pull request may close this issue.