vitest-dev / vitest

Next generation testing framework powered by Vite.
https://vitest.dev
MIT License
12.93k stars 1.16k forks source link

Module not found: Error: Can't resolve 'module' in '.../node_modules/local-pkg' #1981

Closed JackWP1 closed 2 years ago

JackWP1 commented 2 years ago

Describe the bug

When building with Webpack 5 we get the following error from Vitest that cannot be resolved by reinstalling the node_modules or clearing the yarn.lock file. NB. The correct version of local-pkg (0.4.2) is present in the node_modules file.

Any help on this error would be greatly appreciated.

Module not found: Error: Can't resolve 'module' in '.../node_modules/local-pkg'

setup:

// package.json

{
    "dependencies": {
        "@apollo/client": "^3.6.8",
        "@reach/alert": "^0.17.0",
        "@reach/dialog": "^0.17.0",
        "@reach/rect": "^0.17.0",
        "@reach/window-size": "^0.17.0",
        "classnames": "^2.3.1",
        "date-fns": "^2.28.0",
        "dotenv": "^16.0.0",
        "graphql": "^16.5.0",
        "immer": "^9.0.15",
        "ip-cidr": "^3.0.4",
        "react": "^18.2.0",
        "react-copy-to-clipboard": "^5.1.0",
        "react-dom": "^18.2.0",
        "react-ga": "^3.3.1",
        "react-helmet": "^6.1.0",
        "react-markdown": "^8.0.3",
        "react-router-dom": "^6.3.0",
        "react-router-hash-link": "^2.4.3",
        "react-scripts": "^5.0.1",
        "react-table": "^7.8.0",
        "remark-gfm": "^3.0.1",
        "use-immer": "^0.7.0"
    },
    "scripts": {
        "start": "CI=true craco start",
        "build": "craco build",
        "test": "vitest run",
        "test:report": "vitest run --coverage",
        "test:reportci": "vitest run --coverage",
        "test:watch": "vitest watch",
        "jest": "craco test --watchAll=false --maxWorkers=50%",
        "jest:report": "craco test --coverage --watchAll=false --maxWorkers=50%",
        "jest:reportci": "craco test --coverage --watchAll=false --detectOpenHandles --runInBand",
        "jest:watch": "craco test --watchAll=true --maxWorkers=25% --cache",
        "view-coverage": "open ./coverage/lcov-report/index.html",
        "storybook": "start-storybook -p 6006 -s public",
        "build-storybook": "build-storybook -s public",
        "generate": "plop"
    },
    "browserslist": [
        ">0.2%",
        "not dead",
        "not op_mini all"
    ],
    "devDependencies": {
        "@apollo/react-testing": "^4.0.0",
        "@axe-core/react": "^4.4.3",
        "@babel/core": "^7.18.5",
        "@babel/preset-env": "^7.18.2",
        "@babel/preset-react": "^7.17.12",
        "@babel/preset-typescript": "^7.17.12",
        "@craco/craco": "^6.4.3",
        "@storybook/addon-a11y": "^6.4.19",
        "@storybook/addon-actions": "^6.4.19",
        "@storybook/addon-essentials": "^6.4.19",
        "@storybook/addon-knobs": "^6.4.0",
        "@storybook/addon-links": "^6.4.19",
        "@storybook/addon-postcss": "^2.0.0",
        "@storybook/builder-webpack5": "^6.4.19",
        "@storybook/manager-webpack5": "^6.4.19",
        "@storybook/preset-create-react-app": "^4.1.0",
        "@storybook/preset-scss": "^1.0.3",
        "@storybook/react": "^6.4.19",
        "@svgr/webpack": "^6.2.1",
        "@testing-library/jest-dom": "^5.16.1",
        "@testing-library/react": "^12.1.2",
        "@testing-library/react-hooks": "^8.0.1",
        "@testing-library/user-event": "^13.5.0",
        "@types/jest": "^28.1.3",
        "@types/node": "^16.10.2",
        "@types/react": "^18.0.14",
        "@types/react-copy-to-clipboard": "^5.0.0",
        "@types/react-dom": "^18.0.5",
        "@types/react-helmet": "^6.1.5",
        "@types/react-router-hash-link": "^2.4.5",
        "@types/react-table": "^7.7.11",
        "@uui/schema": "^0.56.21",
        "@uui/ui-generators": "^2.6.18",
        "babel-jest": "^29.0.1",
        "craco-module-federation": "^1.1.0",
        "eslint": "^8.23.0",
        "eslint-plugin-simple-import-sort": "^7.0.0",
        "eslint-plugin-storybook": "^0.5.11",
        "jest": "^29.0.1",
        "jest-html-reporter": "^3.6.0",
        "jest-watch-typeahead": "^2.1.1",
        "node-canvas": "^2.9.0",
        "node-polyfill-webpack-plugin": "^1.1.4",
        "node-sass": "^7.0.1",
        "plop": "^3.1.0",
        "postcss": "^8.4.14",
        "postcss-scss": "^4.0.4",
        "prettier": "^2.7.1",
        "sass": "^1.54.8",
        "storybook": "^6.4.19",
        "storybook-preset-craco": "^0.0.6",
        "stylelint": "^14.2.0",
        "stylelint-config-css-modules": "^2.3.0",
        "stylelint-config-standard-scss": "^3.0.0",
        "ts-jest": "^28.0.8",
        "typescript": "^4.7.4",
        "vite": "^3.0.9",
        "vite-plugin-react-svg": "^0.2.0",
        "vitest": "^0.22.1",
        "waait": "^1.0.5",
        "web-vitals": "^2.1.2",
        "webpack": "^5.73.0"
    },
    "scarfSettings": {
        "enabled": false
    }
}

// vite.config.ts

/// <reference types="vitest" />
import { defineConfig } from "vite";

export default defineConfig({
    test: {
        globals: true,
        environment: "jsdom",
        css: true,
        deps: {
            inline: [
                // "@apollo/client",
                "ts-invariant",
            ],
            fallbackCJS: true,
            interopDefault: true,
        },
    },
    plugins: [
        {
            name: "load-svg",
            enforce: "pre",
            transform(_, id) {
                if (id.endsWith(".svg")) {
                    return `export default () => (<svg src="${id} />)`;
                }
            },
        },
    ],
});

// tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["dom", "dom.iterable", "esnext"],
        "allowJs": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "noEmit": true,
        "jsx": "react-jsx",
        "noFallthroughCasesInSwitch": true,
        "types": ["vitest/globals"]
    },
    "include": ["src"]
}

Reproduction

yarn && yarn build

System Info

System:
    OS: macOS 12.5.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 1.00 GB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.13.2 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.1.2 - /usr/local/bin/npm
    Watchman: 2022.07.04.00 - /opt/homebrew/bin/watchman
  Browsers:
    Brave Browser: 102.1.39.111
    Chrome: 104.0.5112.101
    Firefox Developer Edition: 105.0
    Safari: 15.6.1

Used Package Manager

yarn

Validations

github-actions[bot] commented 2 years ago

Hello @JackWP1. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.