I have a legacy typescript React app and now I want to migrate it from webpack to vite, when I build the app using this command it shows an error like this:
> tsc && vite build vite v4.3.5 building for production... ✓ 2 modules transformed. ✓ built in 32ms [vite]: Rollup failed to resolve import "/src/main.tsx" from "/Users/John/source/reddwarf/frontend/snap-web/src/index.html". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to `build.rollupOptions.external` error during build: Error: [vite]: Rollup failed to resolve import "/src/main.tsx" from "/Users/John/source/reddwarf/frontend/snap-web/src/index.html". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to `build.rollupOptions.external` at viteWarn (file:///Users/John/source/reddwarf/frontend/snap-web/node_modules/.pnpm/vite@4.3.5_@types+node@16.18.23/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:46561:23) at onwarn (file:///Users/John/source/reddwarf/frontend/snap-web/node_modules/@vitejs/plugin-react/dist/index.mjs:237:9) at onRollupWarning (file:///Users/John/source/reddwarf/frontend/snap-web/node_modules/.pnpm/vite@4.3.5_@types+node@16.18.23/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:46582:9) at onwarn (file:///Users/John/source/reddwarf/frontend/snap-web/node_modules/.pnpm/vite@4.3.5_@types+node@16.18.23/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:46332:13) at Object.onwarn (file:///Users/John/source/reddwarf/frontend/snap-web/node_modules/.pnpm/rollup@3.21.5/node_modules/rollup/dist/es/shared/node-entry.js:25305:13) at ModuleLoader.handleInvalidResolvedId (file:///Users/John/source/reddwarf/frontend/snap-web/node_modules/.pnpm/rollup@3.21.5/node_modules/rollup/dist/es/shared/node-entry.js:23940:26) at file:///Users/John/source/reddwarf/frontend/snap-web/node_modules/.pnpm/rollup@3.21.5/node_modules/rollup/dist/es/shared/node-entry.js:23900:26 ELIFECYCLE Command failed with exit code 1.
What should I do to solve this problem? I searched from the internet and it seems no one is facing similar issue. This is vite.config.ts
:
import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import path from 'path'; export default defineConfig({ root: path.join(__dirname, 'src'), plugins: [react()], build:{ outDir: "build" } })
This is the command I passed from npm create vite@latest my-vue-app -- --template react-ts
:
import React from 'react' import ReactDOM from 'react-dom/client' import './index.css' ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( <React.StrictMode> <div>ddddd</div> </React.StrictMode>, )
This is index.html
:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" href="/favicon.ico" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="theme-color" content="#000000" /> <meta name="description" content="Web site created using create-react-app" /> <link rel="apple-touch-icon" href="/logo192.png" /> <!-- manifest.json provides metadata used when your web app is installed on a user's mobile device or desktop. See https://developers.googl e.com/web/fundamentals/web-app-manifest/ --> <link rel="manifest" href="/manifest.json" /> <!-- Notice the use of %PUBLIC_URL% in the tags above. It will be replaced with the URL of the `public` folder during the build. Only files inside the `public` folder can be referenced from the HTML. Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will work correctly both with client-side routing and a non-root public URL. Learn how to configure a non-root public URL by running `npm run build`. --> <title>title</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div id="root"></div> <script type="module" src="/src/main.tsx"></script> </body> </html>
P粉0806439752024-01-29 16:27:01
For me it was a package I installed but got an error. The problem is that I forgot that I removed the package from the dependencies in package.json. I solved the problem by adding it. You can also do this
npm uninstall package
npm install package
P粉0424552502024-01-29 09:51:06
Similar to this question, the default vite.config.ts
file is as follows:
import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' // https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], })
If you run the build script using npm run build
it will work because the index.html
file is in the root level of the project. p>
rootDir/ |- src/ |- <src dir contents> |- index.html |- vite.config.ts
By passing a root
attribute like root: path.join(__dirname, 'src')
you need an index.html
> placed in src
Files inside of directory .
Now, since you are migrating from webpack, you may have index.html
file inside the src
directory and you try to modify vite.config.ts
root in
to find the correct entry point.
However, the vite index.html
file is linked to the main.tsx
file as shown below
<script type="module" src="/src/main.tsx"></script>
All you need to do is change the file structure to be the same as what vite gives you by default:
Change your vite.config.ts
file to:
import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' // https://vitejs.dev/config/ export default defineConfig({ plugins: [react()], build:{ outDir: "build" } })
Then move the index.html
file to the root directory.