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

Cannot set properties of undefined Type Error for React Moment package #7376

Closed
7 tasks done
iknowhtml opened this issue Mar 18, 2022 · 22 comments
Closed
7 tasks done
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@iknowhtml
Copy link

iknowhtml commented Mar 18, 2022

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

@sapphi-red
Copy link
Member

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.

@ZinkNotTheMetal

This comment was marked as off-topic.

@sapphi-red
Copy link
Member

sapphi-red commented Mar 29, 2022

I digged down a bit more.
moment is resolved by jsnext:main field here.

if (!entryPoint || entryPoint.endsWith('.mjs')) {
for (const field of options.mainFields || DEFAULT_MAIN_FIELDS) {
if (typeof data[field] === 'string') {
entryPoint = data[field]
break
}
}
}

So for a workaround, setting resolve.mainFields to [] will work.

I am not sure whether it is ok to change !entryPoint || entryPoint.endsWith('.mjs') to !options.isRequire && (!entryPoint || entryPoint.endsWith('.mjs')) like #7438.

@iknowhtml
Copy link
Author

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

Thanks @sapphi-red!

@bluwy
Copy link
Member

bluwy commented Apr 3, 2022

Closing as #7582 should fix this issue too.

@bluwy bluwy closed this as completed Apr 3, 2022
@iknowhtml
Copy link
Author

@bluwy , the latest version of Vite (2.9.1) doesn't resolve the issue:
https://stackblitz.com/edit/vitejs-vite-skvjpc?file=package.json

I'm assuming a build with the fix just hasn't been released it.
When can we expect a new version that has the fix?

Thanks!

@patak-dev
Copy link
Member

Yes, it will be in 2.9.2, probably in a few days. We have some CI issues to check first.

@iknowhtml
Copy link
Author

Sounds good. Thank you @patak-dev for the prompt reply!

@sapphi-red
Copy link
Member

Opening because #7582 was reverted by #7737.

@sapphi-red sapphi-red reopened this Apr 14, 2022
@sapphi-red sapphi-red added bug p2-edge-case Bug, but has workaround or limited in scope (priority) and removed pending triage labels Apr 16, 2022
@roddc
Copy link

roddc commented Jul 26, 2022

I am getting this error from a library Ros.js:36 Uncaught TypeError: Cannot set properties of undefined (setting 'socket').

function Ros(options) {
  options = options || {};
  var that = this;
  this.socket = null;
...
}

How do I fix this, the workaround is not working.

I am using 3.0.3 version of vite

@JairTorres1003
Copy link

JairTorres1003 commented Nov 14, 2022

The same thing happened to me and here #6675 (comment) I found the solution for my part, it works fine, you just have to modify the file vite.config.js and also install the plugin

@ismaelpaul
Copy link

The same thing happened to me and here #6675 (comment) I found the solution for my part, it works fine, you just have to modify the file vite.config.js and also install the plugin

It worked for me as well, thanks Jair :)

@deependrasingh002
Copy link

those who created react app with vite can get the issues like that can be resolve by using this just
code

@SuRiRONHeaRt

This comment was marked as off-topic.

@deependrasingh002

This comment was marked as off-topic.

@SuRiRONHeaRt

This comment was marked as off-topic.

@ChristianCaracach-velaONE

The same thing happened to me and here #6675 (comment) I found the solution for my part, it works fine, you just have to modify the file vite.config.js and also install the plugin

Worked for me. Thanks!!

@joseteIIo
Copy link

I am not using CRA for a project and unfortunately the issue persists even with @trdxDeepu 's solution.

@FilippoNardin
Copy link

I had some luck when importing moment.js directly as an ESM module by overriding the module resolution to the source file in node_modules.

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: [
      {
        // Allow moment.js to be used as an ESM module
        find: /^moment$/,
        replacement: path.resolve(__dirname, "./node_modules/moment/moment.js"),
      },
    ],
  },
});

@patak-dev patak-dev removed the bug label Feb 10, 2024
@igorleszczynski
Copy link

igorleszczynski commented Feb 26, 2024

Still problem after npm run build

npm run dev working correctly

@mabedan
Copy link

mabedan commented Jun 17, 2024

those who created react app with vite can get the issues like that can be resolve by using this just code

Beware of using this workaround. This basically tells vite to ignore “browser” property of the package.json of all your dependencies and will break manage packages such as axios, engine-io/client, and more.

@sapphi-red
Copy link
Member

sapphi-red commented Oct 10, 2024

TLDR: for people using moment, set resolve.mainFields: ['browser', 'module', 'jsnext']

Moment only exposes ESM files via the legacy jsnext:main. Because the module field (which was supported by rollup and webpack) was not set for a long time, some packages depending on moment does not expect moment to be ESM (moment/moment@9ce89e7, moment/moment@0c709ba). But later on Vite has been made and included jsnext:main field to resolve.mainFields without knowing that fact. That made those packages depending on moment not to work like the OP in this issue. Given that Vite has been included jsnext:main field by default, it's probably better to avoid removing otherwise it might break other things. So in conclusion, for users using dependencies using moment, please set resolve.mainFields: ['browser', 'module', 'jsnext'] so that the ESM version of moment won't be used.

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Oct 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Oct 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

No branches or pull requests