Flip0
Flip0

Reputation: 11

NestJS + Webpack + pnpm workspace Error: Cannot find module

I have pnpm workspace and I want to import it to my nestjs app Webpack compiled successfully, ide don`t show error and I got strange path for module in error

Import

import { deviceController } from 'int-api-types/src/controller/device/device';

Webpack result

module.exports = require("int-api-types/src/controller/device/device");

Error

Error: Cannot find module 'C:\programs\PyScripts\apps\server\node_modules\int-api-types\src\controller\device\devicesrc\controller\device\device\src\controller\device\device.ts'
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:1430:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:1419:15)
    at resolveExports (node:internal/modules/cjs/loader:653:14)
    at Function._findPath (node:internal/modules/cjs/loader:742:31)
    at Function._resolveFilename (node:internal/modules/cjs/loader:1380:27)
    at defaultResolveImpl (node:internal/modules/cjs/loader:1050:19)
    at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1055:22)
    at Function._load (node:internal/modules/cjs/loader:1204:37)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:234:24) {
  code: 'MODULE_NOT_FOUND',
  path: 'C:\\programs\\PyScripts\\apps\\server\\node_modules\\int-api-types'
}

Webpack config

const nodeExternals = require('webpack-node-externals');
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
const path = require('path');

module.exports = function (options, webpack) {
    return {
        ...options,
        entry: ['webpack/hot/poll?100', options.entry],
        externals: [
            nodeExternals({
                allowlist: ['webpack/hot/poll?100'],
            }),
        ],
        plugins: [
            ...options.plugins,
            new webpack.HotModuleReplacementPlugin(),
            new webpack.WatchIgnorePlugin({
                paths: [/\.js$/, /\.d\.ts$/],
            }),
            new RunScriptWebpackPlugin({
                name: options.output.filename,
                autoRestart: false,
            }),
        ],
        resolve: {
            symlinks: false,
            extensions: ['.ts', '.json', '...'],
            alias: {
                '@': path.resolve(__dirname, './src/'),
            },
        },
    };
};

tsconfig.json

{
  "extends": "./../../tsconfig.json",
  "compilerOptions": {
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "resolveJsonModule": true,
    "experimentalDecorators": true,
    "strictNullChecks": false,
    "removeComments": true,
    "target": "ES2021",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
  },
  "include": ["src", "swagger", "package.json"]
}

./../../tsconfig.json (from which tsconfig.json is inherited)

{
    "compilerOptions": {
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "skipLibCheck": true,
        "composite": true,
        "lib": ["es2020"],
        "module": "ES2022",
        "moduleResolution": "bundler",
        "target": "es2022",
        "isolatedModules": true,
        "moduleDetection": "force",
        "resolveJsonModule": true,
        "declaration": true,

        /* Linting */
        "strict": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noFallthroughCasesInSwitch": true,
        "noUncheckedSideEffectImports": true,
    },
}

int-api-types package.json

{
    "name": "int-api-types",
    "version": "1.0.0",
    "type": "module",
    "keywords": [],
    "author": "",
    "license": "ISC",
    "description": "",
    "devDependencies": {
        "@types/node": "^22.10.1",
        "typescript": "^5.7.2"
    },
    "exports": {
        "./*": "./**/*.ts"
    },
    "peerDependencies": {
        "@nestjs/common": "^10.0.0",
        "@nestjs/swagger": "^11.0.3"
    }
}

Remove pnpm-lock, node_modules, but without result

Try to toggle resolve.symlinks, no results too

Also tried to add resolve.alias, also no results

'int-api-types': path.resolve(
   __dirname,
   './../../packages/api-types',
),

Upvotes: 1

Views: 64

Answers (0)

Related Questions