I’m currently migrating a JavaScript project to TypeScript (and this is my first attempt at TS). For some reason, tsc --build
throws a bunch of errors for vitest not being able to resolve the node library definitions:
../node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/index.d.ts(6,23): error TS2307: Cannot find module 'node:http' or its corresponding type declarations.
There are 17 or so similar errors.
This is my tsconfig.json
:
{
"include": ["**/*.ts", "**/*.js", "**/*.mjs", "**/*.mts"],
"exclude": [
"node_modules",
"coverage/**/*",
"dist/**/*",
"test/**/*"
],
"compilerOptions": {
"outDir": "dist",
"baseUrl": "./",
"paths": {
"$types/*": [
"types/*"
]
},
"target": "es6",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"checkJs": true
}
}
Now, vitest obviously doesn’t need any of those files, because the tests all ran and passed when it was pure JS. But now I can’t even transpile the base project, let alone run tests. How do I get vitest to either not care about those, or properly find them?
Based on this conversation, I tried setting target
to ESNext
, but that had no impact. I also tried changing moduleResolution
to Bundler
— but that both seems like the wrong thing (since, at least for the moment, I’m just trying to get pure tsc
to work), and wanted me to make a bunch of other changes.
I also tried setting vitest global types in the tsconfig
:
// tsconfig.json
{
"compilerOptions": {
"types": ["vitest/globals"]
}
}
But that too had no effect.
TBH, I’m not even ready to run tests, as I still have actual typescript errors to work through in my conversion, but I need to get this vitest compile problem out of the way. Any ideas?
For completeness sake, here’s my vitest config:
// vitest.config.mjs
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {}
});
// vitest.workspace.mjs
import { configDefaults, defineWorkspace } from 'vitest/config';
export default defineWorkspace([
'./*',
{
extends: './vitest.config.mjs',
test: {
exclude: [
...configDefaults.exclude,
'**/*.component-{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx'
],
name: 'unit',
environment: './test/prisma/prisma-test-environment.mjs',
setupFiles: ['./test/helpers/setup.mjs']
}
},
{
extends: './vitest.config.mjs',
test: {
include: [ '**/*.component-{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx' ],
name: 'component',
environment: './test/prisma/prisma-test-environment.mjs',
setupFiles: ['./test/helpers/setup.mjs']
}
}
]);
Also, in case it matters, this is in a pnpm
mono-repo setup, where the layout is
/
/node_modules
/package.json
# main project directory
/package
/package/node_modules
/package/package.json
/package/tsconfig.json
/package/vitest.config.mjs
/package/vitest.workspace.mjs