Moment: TS2307: Cannot find module 'moment'.

Created on 1 May 2020  ·  47Comments  ·  Source: moment/moment

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

Most helpful comment

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

All 47 comments

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

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

I have tried. but same error came.

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

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

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"
  }

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.

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/*"
      ]
    }
  },

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

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

"moment": "^2.25.0",

Capture
All moment imports returning error

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

111

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

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.

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 [email protected]. You do not need to delete anything or change anything manually.

I can confirm that downgrading to 2.24 fixes the issue.

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 [email protected]. 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

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

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

I experienced this problem during docker image build.

its not working with 2.24.0 for me

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

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

@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 [email protected]
npm install

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.

Can try with resolutions field in package.json

Add

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

I also confirm that with 2.25.0 I have the same issue with typescript 3.8.3 and running with ts-node 8.9.1. Downgrading to 2.24.0 solved the problem.

@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 [email protected]
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"

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

Same, locking in 2.24.0 fixed it.

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

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

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

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.

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 :)

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"

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 [email protected]

Hope it helps.

Cheers
Unkn0wn0x

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

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

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

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

Downgraded to 2.24.0, and it's working fine.

2.25.1 released

Tested 2.25.1 , issue is gone. Thanks

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

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

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

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?

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'

For me it was

    "moment": "2.24.0",
    "moment-timezone": "^0.5.31",

I think you can update moment to version 2.29.0

npm uninstall moment
npm install [email protected]

TS2307: Cannot find module 'moment' in Angular 8

Was this page helpful?
5 / 5 - 1 ratings

Related issues

elmart picture elmart  ·  42Comments

electrobabe picture electrobabe  ·  48Comments

OregonianPyro picture OregonianPyro  ·  10Comments

cristianstaicu picture cristianstaicu  ·  24Comments

Brendan-Lucas picture Brendan-Lucas  ·  9Comments