Closed HexaField closed 3 hours ago
This is because Vitest has additional configuration for resolution: https://github.com/vitest-dev/vitest/blob/2a50464d58e98f58fed513971a570a952081bfef/packages/vitest/src/node/plugins/index.ts#L91-L93
For your use case (migration from ts-node
), it's likely that doing the same would make sense. Adding this to vite.config.ts
can fix your repro https://stackblitz.com/edit/vitejs-vite-eql4jh?file=vite.config.ts
This is because Vitest has additional configuration for resolution:
For your use case (migration from
ts-node
), it's likely that doing the same would make sense. Adding this tovite.config.ts
can fix your repro https://stackblitz.com/edit/vitejs-vite-eql4jh?file=vite.config.ts
Thanks so much. I have been digging into the code for a few days straight and hadn't found this. You are a life saver.
I'll leave it to you to triage and see if this should be implemented as a default to keep environments the same,
I think we'll close the issue for now. If we get more reports about confusing resolutions, then we can revisit.
Btw, have you considered using tsx https://github.com/privatenumber/tsx instead of vite-node? Unless you need Vite plugin to customize something, tsx would be more closer to bare NodeJs execution and might be better fit for your use case.
I think we'll close the issue for now. If we get more reports about confusing resolutions, then we can revisit.
Btw, have you considered using tsx https://github.com/privatenumber/tsx instead of vite-node? Unless you need Vite plugin to customize something, tsx would be more closer to bare NodeJs execution and might be better fit for your use case.
I had trouble with it with conditional require
calls that couldn't easily be turned into top level await import()
calls since they relied on variable dynamic imports. But I will try it again at some point if I find any issues with vite-node.
Ok I just tried again, the immediate error I get is __dirname is not defined in ES module scope
which I know is solvable, but I feel like if that failed many other things would. We have a very large and complicated project.
Hmm, good point. vite-node
currently allows cjs globals but tsx
might be more strict about it. We don't necessary think such compat is desirable in all cases since it goes further away from esm semantics, but it certainly helps some scenarios. I'm glad it's working for your project.
Describe the bug
We are trying to migrate from
ts-node
andmocha
tovite-node
andvitest
for our server runtime and test runner, but are seeing differences in the behavior ofvite-node
as it runs servers vs tests.When running tests, it behaves as expected according to the ESM specification, but when running as a server, it seems to handle import modules incorrectly.
Hookstate is a state management library that extends functionality of React. It overrides
useEffect
(among other functions) in order to update itself internally. When runningvite-node
from the CLI this behavior is not observed. (read more here https://github.com/avkonst/hookstate/issues/412)In the provided reproduction, running
npm run test
to run vitest will successfully log thatuseEffect
imported from React has been overridden by hookstate. When runningnpm run dev
it will throw an error showing that it has not been overridden.The behavior is also correct when running vite as the client/browser runtime.
Reproduction
https://stackblitz.com/edit/vitejs-vite-pnw4th?file=package.json&terminal=dev
https://github.com/HexaField/vite-node-vitest-esm-import-differences
System Info
Used Package Manager
npm
Validations