vite: @vitejs/app - react-ts - antd-mobile -error
Describe the bug
this is no error in dev mode but in production mode, when i was building, i got this message
although built sucessfully, but when i use http-server to visit the page, there are some error in console.
Uncaught TypeError: Failed to resolve module specifier “indexof”. Relative references must start with either “/”, “./”, or “…/”.
Reproduction
1、use npm init @vitejs/app to generate project from ‘react-ts’ 2、just import sth from antd-mobile 3、npm run build 4、cd dist && httpserver ./
and there will be the message above in console
System Info
Output of npx envinfo --system --npmPackages vite,@vitejs/plugin-vue --binaries --browsers
:
System:
OS: macOS 11.2.3
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 264.75 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 12.16.1 - /usr/local/bin/node
Yarn: 1.22.4 - /usr/local/bin/yarn
npm: 6.13.4 - /usr/local/bin/npm
Browsers:
Chrome: 89.0.4389.90
Safari: 14.0.3
npmPackages:
vite: ^2.1.0 => 2.1.2
Used package manager:
Logs
> Uncaught TypeError: Failed to resolve module specifier "indexof". Relative references must start with either "/", "./", or "../".
Before submitting the issue, please make sure you do the following
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Provide a description in this issue that describes the bug.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 9
- Comments: 22 (4 by maintainers)
I have same problem
Closing as it’s a dependency issue explained in https://github.com/vitejs/vite/issues/2670#issuecomment-905495115. Thanks for the deep dive @fatso83!
@xubaobao19940428 @sugar0828 I don’t think those issues are related to this. If you have reproduction, please open an issue/discussion for it.
单独安装
npm install indexof
可以解决这个问题,亲测可用I solve the problem by:
This was my problem because I was controlling the CDN path of the packaged files through an environment variable and my environment variable was incorrect @bluwy
OK, so I have figured this one out after a deep dive. This issue should just be closed, as the issue can be fixed by upgrading the right dependencies. @anncwb was right in this regard, but finding out what is the hard part. Read on to understand why.
In the following code, I have used the reproduction case given by @Jackson-p. This works just as well as my case and my own project, as it is caused by the exact same dependency.
I tried to find out what had imported
indexof
by grepping all package.json files:So, if you inspect the output and know how
package.json
is structured, you will notice that only one of these lines come from thedependencies
section (following the “<name>”: “<version>” pattern). That is the line from the package file ofcomponent-classes
which has a dependency oncomponent-indexof
. As could be seen here:So, somewhat surprisingly, no package here actually has declared any dependency on a package called exactly
indexof
! This threw me off for a while, but thecomponent-classes
did mentionindexof
in some other field, thebrowser
field (also in the grep report above):Usually, the browser field indicates to bundlers what the entry point should be when targetting browsers, and that is usually a local file with a relative path, not a package name, as here, so I got a bit curious. Manually removing the field did nothing, so I checked out the source code and it turns out this was added in this commit: https://github.com/component/classes/commit/d9954893d44eb89a69349c13253603c3454d82ec
If you inspect the diff, you see it also contained this little snippet:
And that’s it! We have an undeclared dependency on
indexof
. It’s not in package.json, but Rollup (which is used by Vite) does see it and tells us about it.So there are three different solutions here
var index = require('component-indexof');
npm install indexof
rc-collapse
, but I see they are already working on alpha 5.0.0, so that has already happened 😃Related: rollup/rollup#2096 rollup/rollup-plugin-babel#201
I have the same confusion