Unable to load file at runtime inside vite config in a monorepo
Opened this issue · 0 comments
laytong commented
Current Behavior
In a new monorepo, our config looks roughly as follows.
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'
import { nxCopyAssetsPlugin } from '@nx/vite/plugins/nx-copy-assets.plugin'
import { readFileSync } from 'fs'
import * as path from 'node:path'
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
return {
root: __dirname,
cacheDir: '../../node_modules/.vite/apps/invoice-manager',
...(!env.CI
? {
server: {
https: {
key: readFileSync(path.resolve(__dirname, env.SSL_KEY_FILE)), // <-- PROBLEM IS THESE 2 VARS
cert: readFileSync(path.resolve(__dirname, env.SSL_CERT_FILE)),
},
port: parseInt(env.PORT),
host: env.HOST,
open: true,
},
}
: {}),
preview: {
port: 4300,
host: 'localhost',
},
plugins: [react(), nxViteTsPaths(), nxCopyAssetsPlugin(['*.md'])],
// Uncomment this if you are using workers.
// worker: {
// plugins: [ nxViteTsPaths() ],
// },
build: {
outDir: '../../dist/apps/invoice-manager',
emptyOutDir: true,
reportCompressedSize: true,
commonjsOptions: {
transformMixedEsModules: true,
},
},
})
It looks like nx is doing some kind of transpiling/pre-parsing of the vite config at the root (rather than the project root).
When i run the project it fails with 2 errors that look like
TypeError [ERR_INVALID_ARG_TYPE]: The "paths[1]" argument must be of type string. Received undefined
If i replace the env var with
server: {
https: {
key: readFileSync(path.resolve(__dirname, './__certs__/local-key.pem')),
cert: readFileSync(path.resolve(__dirname, './__certs__/local-cert.pem')),
},
port: parseInt(env.PORT),
host: env.HOST,
open: true,
},
Expected Behavior
when nx parses the vite.config it should use the local .env value and use relative path to load file OR leave it to vite to handle.
GitHub Repo
No response
Steps to Reproduce
- Have a vite/react monorepo
- Have a local .env and a file at the project root that is referenced via readFileSync in your vite.config.ts
- Attempt to build vite
Nx Report
Node : 22.6.0
OS : darwin-x64
Native Target : x86_64-macos
pnpm : 9.12.1
nx : 20.2.2
@nx/js : 20.2.2
@nx/jest : 20.2.2
@nx/eslint : 20.2.2
@nx/workspace : 20.2.2
@nx/devkit : 20.2.2
@nx/eslint-plugin : 20.2.2
@nx/module-federation : 20.2.2
@nx/playwright : 20.2.2
@nx/react : 20.2.2
@nx/vite : 20.2.2
@nx/web : 20.2.2
@nx/webpack : 20.2.2
typescript : 5.5.4
---------------------------------------
Registered Plugins:
@nx/vite/plugin
@nx/eslint/plugin
@nx/playwright/plugin
@nx/vite/plugin
---------------------------------------
⚠️ Unable to construct project graph.
Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it.
Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it.
Failure Logs
NX Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it. See errors below.
Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it.
An error occurred while processing files for the @nx/vite/plugin plugin.
- apps/PROJECT_NAME/vite.config.ts: The "paths[1]" argument must be of type string. Received undefined
TypeError [ERR_INVALID_ARG_TYPE]: The "paths[1]" argument must be of type string. Received undefined
at Object.resolve (node:path:1128:7)
at /<dir>/code/monorepo/apps/my-app/vite.config.ts:51:49
at loadConfigFromFile (file:///Users/<dir>/code/monorepo/node_modules/.pnpm/vite@5.4.11_@types+node@18.16.9_less@4.1.3_sass@1.82.0_stylus@0.64.0_terser@5.37.0/node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:66538:62)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async resolveConfig (file:///Users/<dir>/code/monorepo/node_modules/.pnpm/vite@5.4.11_@types+node@18.16.9_less@4.1.3_sass@1.82.0_stylus@0.64.0_terser@5.37.0/node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:66140:24)
at async buildViteTargets (/Users/<dir>/code/monorepo/node_modules/.pnpm/@nx+vite@20.2.2_@babel+traverse@7.26.4_@swc-node+register@1.9.2_@swc+core@1.5.29_@swc+helpers_22bq4ltvecv5ntqgmslm365xyu/node_modules/@nx/vite/src/plugins/plugin.js:113:29)
at async createNodesInternal (/Users/<dir>/code/monorepo/node_modules/.pnpm/@nx+vite@20.2.2_@babel+traverse@7.26.4_@swc-node+register@1.9.2_@swc+core@1.5.29_@swc+helpers_22bq4ltvecv5ntqgmslm365xyu/node_modules/@nx/vite/src/plugins/plugin.js:79:18)
at async /Users/<dir>/code/monorepo/node_modules/.pnpm/nx@20.2.2_@swc-node+register@1.9.2_@swc+core@1.5.29_@swc+helpers@0.5.15__@swc+types@0.1.12_ty_hlx4aefcfstgkdtdtlybcnzkyy/node_modules/nx/src/project-graph/plugins/utils.js:10:27
at async Promise.all (index 0)
at async createNodesFromFiles (/Users/<dir>/code/monorepo/node_modules/.pnpm/nx@20.2.2_@swc-node+register@1.9.2_@swc+core@1.5.29_@swc+helpers@0.5.15__@swc+types@0.1.12_ty_hlx4aefcfstgkdtdtlybcnzkyy/node_modules/nx/src/project-graph/plugins/utils.js:8:5)
An error occurred while processing files for the @nx/vite/plugin plugin.
- apps/PROJECT_NAME/vite.config.ts: The "paths[1]" argument must be of type string. Received undefined
TypeError [ERR_INVALID_ARG_TYPE]: The "paths[1]" argument must be of type string. Received undefined
at Object.resolve (node:path:1128:7)
at /Users/<dir>/code/monorepo/apps/my-app/vite.config.ts:51:49
at loadConfigFromFile (file:///Users/<dir>/code/monorepo/node_modules/.pnpm/vite@5.4.11_@types+node@18.16.9_less@4.1.3_sass@1.82.0_stylus@0.64.0_terser@5.37.0/node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:66538:62)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async resolveConfig (file:///Users/<dir>/code/monorepo/node_modules/.pnpm/vite@5.4.11_@types+node@18.16.9_less@4.1.3_sass@1.82.0_stylus@0.64.0_terser@5.37.0/node_modules/vite/dist/node/chunks/dep-CB_7IfJ-.js:66140:24)
at async buildViteTargets (/Users/<dir>/code/monorepo/node_modules/.pnpm/@nx+vite@20.2.2_@babel+traverse@7.26.4_@swc-node+register@1.9.2_@swc+core@1.5.29_@swc+helpers_22bq4ltvecv5ntqgmslm365xyu/node_modules/@nx/vite/src/plugins/plugin.js:113:29)
at async createNodesInternal (/Users/<dir>/code/monorepo/node_modules/.pnpm/@nx+vite@20.2.2_@babel+traverse@7.26.4_@swc-node+register@1.9.2_@swc+core@1.5.29_@swc+helpers_22bq4ltvecv5ntqgmslm365xyu/node_modules/@nx/vite/src/plugins/plugin.js:79:18)
at async /Users/<dir>/code/monorepo/node_modules/.pnpm/nx@20.2.2_@swc-node+register@1.9.2_@swc+core@1.5.29_@swc+helpers@0.5.15__@swc+types@0.1.12_ty_hlx4aefcfstgkdtdtlybcnzkyy/node_modules/nx/src/project-graph/plugins/utils.js:10:27
at async Promise.all (index 0)
at async createNodesFromFiles (/Users/<dir>/code/monorepo/node_modules/.pnpm/nx@20.2.2_@swc-node+register@1.9.2_@swc+core@1.5.29_@swc+helpers@0.5.15__@swc+types@0.1.12_ty_hlx4aefcfstgkdtdtlybcnzkyy/node_modules/nx/src/project-graph/plugins/utils.js:8:5)
Package Manager Version
pnpm 9.12.1
Operating System
- macOS
- Linux
- Windows
- Other (Please specify)
Additional Information
No response