-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
require is not defined
when importing a node_module that imports a CSS file
#3409
Comments
Vite doesn't use import "react-calendar/dist/Calendar.css"; |
@Shinigami92 the upstream project ( So we expected vitejs to be able to (while ESM-ifying this dependency) rewrite the Is this possible? As far as we understand, vite is capable of bundling/importing CSS directly within our application code, with an |
as a workaround, you can disable the index.html <head>
<script>window.require = () => {}</script>
<script type="module" src="src/main.js"></script>
</head> src/main.js import 'react-calendar/dist/Calendar.css'
// ... |
@hronro yeah, we're using |
@Shinigami92 you'd added the |
You can try this plugin https://github.com/originjs/vite-plugins/tree/main/packages/vite-plugin-commonjs import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'
import { esbuildCommonjs } from '@originjs/vite-plugin-commonjs'
export default defineConfig({
plugins: [reactRefresh()],
optimizeDeps:{
esbuildOptions:{
plugins:[
esbuildCommonjs(['react-calendar','react-date-picker'])
]
}
}
}) |
@ygj6 thanks for the link to that plugin! I have a naive question, but I thought that vitejs did "automatic ESM-ification" of all of my dependencies automatically (via Just wondering why I'd have to configure the Thanks! |
Vite use esbuild for pre-bundling. However, esbuild will not transform require evanw/esbuild#506. |
|
@ChuckJonas do you found any fix? I have same problem
|
@gonzalojpv nope, I went back to |
@ygj6 Hey i want use require in middle of file but import only support at top of file. so how do we handle this things. |
Looks like the error comes up because esbuild pre-bundles // node_modules/.pnpm/react-date-picker@8.4.0_react-dom@17.0.2+react@17.0.2/node_modules/react-date-picker/dist/entry.js
var require_entry = __commonJS({
"node_modules/.pnpm/react-date-picker@8.4.0_react-dom@17.0.2+react@17.0.2/node_modules/react-date-picker/dist/entry.js"(exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
__require("/Users/bjorn/Work/repros/show-vite-issue/node_modules/.pnpm/react-calendar@3.7.0_react-dom@17.0.2+react@17.0.2/node_modules/react-calendar/dist/Calendar.css");
var _DatePicker = _interopRequireDefault(require_DatePicker());
__require("/Users/bjorn/Work/repros/show-vite-issue/node_modules/.pnpm/react-date-picker@8.4.0_react-dom@17.0.2+react@17.0.2/node_modules/react-date-picker/dist/DatePicker.css");
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { "default": obj };
}
var _default = _DatePicker["default"];
exports["default"] = _default;
}
}); Where |
@ChuckJonas did you try opening an issue on okta or js-cookie side? I have exactly the same issue with the same set of libs. UPDATE: It seems Okta just fixed the issue at okta/okta-auth-js@1ef5298 |
Hi! I have the same problem with the "flickity" js library |
Same issue! Resolved by vite configuration instead of configure any third party dependency! Include dep which you intend to be auto transformed like below:
Referenced document: monorepos-and-linked-dependencies |
same issue for package https://github.com/asabaylus/react-command-palette |
This happens not only for css files but also for json files wich are loaded by require. |
I'm using Vue/Vite with Electron, and |
add transformMixedEsModules to vite.config
|
I solved these error on my project doing the insertion above on your index.html : |
Describe the bug
When importing a react component that imports a CSS file, vite fails with the message:
If you dig into the stack trace, you'll see that the
require
statement is from an imported.css
file (line 8 in this image):This issue:
.css
file. We discovered this issue with an internal component library, originally, but were able to reproduce it withreact-date-picker
, which also imports a.css
file.I expected this to work, as the documentation references being able to manage imported static assets like this: https://vitejs.dev/guide/features.html#css. But, perhaps there's an issue when the CSS import occurs in a vendor/node_module.
Reproduction
https://github.com/blimmer/show-vite-issue
You'll observe the error in the console when you visit
localhost:3000
:System Info
Output of
npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers
:Used package manager: npm
Logs
Before submitting the issue, please make sure you do the following
The text was updated successfully, but these errors were encountered: