安装依赖如下
// package.json
"scripts": {
"dev": "vite",
"test": "vitest",
"build": "vue-tsc && vite build",
"preview": "vite preview",
"eslint": "eslint --fix",
"lint": "npx lint-staged",
"stylelint": "npx stylelint --fix"
},
"dependencies": {
"vue": "3.3.4"
},
"devDependencies": {
"@types/jsdom": "21.1.1",
"@vitejs/plugin-vue": "4.2.3",
"@vue/test-utils": "^2.4.1",
"eslint": "8.47.0",
"happy-dom": "^10.10.4",
"less": "4.2.0",
"lint-staged": "^14.0.0",
"vite": "4.4.9",
"vitest": "0.34.1",
"vue-tsc": "1.0.9"
},
// vite.config.ts
/// <reference types="vitest" />
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
test: {
globals: true,
environment: 'happy-dom',
},
})
启动之后,就会报错
Error: Cannot find package 'happy-dom' imported from /Users/liuxsen/codes/demos/liuxsen-stack/node_modules/.pnpm/[email protected]/node_modules/local-pkg/index.mjs
Did you mean to import [email protected]/node_modules/happy-dom/lib/index.js?
❯ new NodeError node:internal/errors:387:5
❯ packageResolve node:internal/modules/esm/resolve:852:9
❯ moduleResolve node:internal/modules/esm/resolve:901:20
❯ defaultResolve node:internal/modules/esm/resolve:1115:11
❯ nextResolve node:internal/modules/esm/loader:163:28
❯ ESMLoader.resolve node:internal/modules/esm/loader:837:30
❯ ESMLoader.getModuleJob node:internal/modules/esm/loader:424:18
❯ ESMLoader.import node:internal/modules/esm/loader:521:22
❯ importModuleDynamically node:internal/modules/esm/translators:110:35
❯ importModuleDynamicallyCallback node:internal/process/esm_loader:35:14
1
grit136907108 2023-08-21 17:18:28 +08:00
安装 [email protected] 看看
|
2
oppddd OP 可以了,如果使用 pnpm 需要 hoist happay-dom
|