question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Cannot set properties of undefined` Type Error for React Moment package

See original GitHub issue

Describe the bug

I’m migrating one of our projects at my company to Vite from CRA, and I see the following error in the console when I navigate to the locally hosted application in my browser:

image

See also the log file: localhost-1647615535049.log

Needless to say, this wasn’t an issue when we were using CRA.

I’ve tried a number of work arounds myself, but to no avail.

Happy to share if they’re of interest.

Would love to find a fix for this as CRA is painfully slow for running a local development environment and building our project for production.

Thank you!

Reproduction

https://stackblitz.com/edit/vitejs-vite-vvkwaa

System Info

System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
    Memory: 1.05 GB / 16.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 16.2.0 - ~/.nvm/versions/node/v16.2.0/bin/node
    Yarn: 1.22.17 - ~/.nvm/versions/node/v16.2.0/bin/yarn
    npm: 7.13.0 - ~/.nvm/versions/node/v16.2.0/bin/npm
  Browsers:
    Brave Browser: 99.1.36.112
    Chrome: 99.0.4844.74
    Firefox: 94.0.2
    Firefox Developer Edition: 98.0
    Safari: 14.1.2
  npmPackages:
    @vitejs/plugin-react: ^1.2.0 => 1.2.0 
    vite: ^2.8.6 => 2.8.6

Used Package Manager

npm

Logs

N/A (no errors when running vite or vite build)

Validations

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
iknowhtmlcommented, Mar 29, 2022

Can confirm that the above work around resolves the issue: https://stackblitz.com/edit/vitejs-vite-3kngck?file=vite.config.js

Thanks @sapphi-red!

3reactions
sapphi-redcommented, Mar 18, 2022

This issue is simillar to #7053.

  • react-moment(cjs) is importing moment by import (which is transpiled to require) here.
  • moment is resolved to moment/dist/moment.js(esm) by jsnext:main field.

After prebundle, module.exports = { default: Moment } but react-moment expects it to be module.exports = Moment so the error occurs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot set properties of undefined (setting 'duration')
What is happening is that you are trying to access an item in the list using the index listOfSelectedQuestions[index] but the item doesn't ......
Read more >
react-moment - npm
Start using react-moment in your project by running `npm i ... TypeScript icon, indicating that this package has built-in type declarations.
Read more >
How to solve the 'cannot set property of undefined' issue on ...
To access an object key the object has to exist. When it doesn't and you try to access the key, you get the...
Read more >
Guides - Moment.js
Moment.js Guides. Introductory Concepts Mutability Date Math vs Time Math Time Zone vs Offset JavaScript Date Internal Properties. Parsing Guide ...
Read more >
Microsoft.JSInterop.JSException: Cannot set properties of ...
Microsoft.JSInterop.JSException: Cannot set properties of undefined (setting 'moment') TypeError · <pre></pre> for JS codes block · <pre lang=" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found