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 · 49 comments
Closed

TS2307: Cannot find module 'moment'. #5486

gayankasun opened this issue May 1, 2020 · 49 comments

Comments

@gayankasun
Copy link

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

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

@gayankasun
Copy link
Author

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

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

@acharyaks90
Copy link

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

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 commented May 1, 2020

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

"moment": "^2.25.0",

@gayankasun
Copy link
Author

Capture
All moment imports returning error

@gayankasun
Copy link
Author

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

111

@codebrent
Copy link

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

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

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

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

@ijgithub
Copy link

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.

@leafqi
Copy link

leafqi commented May 1, 2020

its not working with 2.24.0 for me

@miluxmilux
Copy link

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

@zhuyuedlut
Copy link

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

@Poyoman39
Copy link

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

Can try with resolutions field in package.json

Add

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

@galaxyfeeder
Copy link

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.

@shanmuganathanvel
Copy link

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

@tomfordweb
Copy link

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

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

@azalpacir
Copy link

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

@andrewstec
Copy link

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

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

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

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

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

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

@flowck
Copy link

flowck commented May 1, 2020

Downgraded to 2.24.0, and it's working fine.

@ichernev
Copy link
Contributor

ichernev commented May 1, 2020

2.25.1 released

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

Tested 2.25.1 , issue is gone. Thanks

@andrewstec
Copy link

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

@mike-marcacci
Copy link

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

@gayankasun
Copy link
Author

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

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

For me it was

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

@quizdeveloper
Copy link

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

npm install moment --save

@xgqfrms
Copy link

xgqfrms commented Oct 17, 2023

"moment": "^2.29.4",

// ❌
// import moment from "moment";
// ✅
import * as moment from 'moment';

https://stackoverflow.com/questions/61537526/typescript-error-cannot-find-module-moment

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

Successfully merging a pull request may close this issue.