我创建了一个新的 nx Angular monorepo,并在尝试使用此代码查看环境变量时
export class AppComponent implements OnInit {
ngOnInit() {
console.log(process.env);
}
}
我在控制台中收到此错误。
错误参考错误:流程未定义
将 @types/node 添加到我的 package.json 并将 types:["node"] 添加到我的 tsconfig.app.json 不起作用,我不确定还可以尝试什么。
Package.json:
{
"name": "@process-test/source",
"version": "0.0.0",
"license": "MIT",
"scripts": {},
"private": true,
"dependencies": {
"@angular/animations": "~19.0.0",
"@angular/common": "~19.0.0",
"@angular/compiler": "~19.0.0",
"@angular/core": "~19.0.0",
"@angular/forms": "~19.0.0",
"@angular/platform-browser": "~19.0.0",
"@angular/platform-browser-dynamic": "~19.0.0",
"@angular/router": "~19.0.0",
"rxjs": "~7.8.0",
"zone.js": "~0.15.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "~19.0.0",
"@angular-devkit/core": "~19.0.0",
"@angular-devkit/schematics": "~19.0.0",
"@angular/cli": "~19.0.0",
"@angular/compiler-cli": "~19.0.0",
"@angular/language-service": "~19.0.0",
"@eslint/js": "^9.8.0",
"@nx/angular": "20.3.0",
"@nx/devkit": "20.3.0",
"@nx/eslint": "20.3.0",
"@nx/eslint-plugin": "20.3.0",
"@nx/jest": "20.3.0",
"@nx/js": "20.3.0",
"@nx/playwright": "20.3.0",
"@nx/web": "20.3.0",
"@nx/workspace": "20.3.0",
"@playwright/test": "^1.36.0",
"@schematics/angular": "~19.0.0",
"@swc-node/register": "~1.9.1",
"@swc/core": "~1.5.7",
"@swc/helpers": "~0.5.11",
"@types/jest": "^29.5.12",
"@types/node": "^18.16.9",
"@typescript-eslint/utils": "^8.13.0",
"angular-eslint": "^19.0.2",
"eslint": "^9.8.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-playwright": "^1.6.2",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"jest-preset-angular": "~14.4.0",
"nx": "20.3.0",
"prettier": "^2.6.2",
"ts-jest": "^29.1.0",
"ts-node": "10.9.1",
"tslib": "^2.3.0",
"typescript": "~5.6.2",
"typescript-eslint": "^8.13.0"
}
}
tsconfig.app.json:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": ["node"]
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"],
"exclude": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts"]
}
实际上这个错误非常简单。它无法读取该进程,因为它尚未定义。这与 NodeJS 中的情况不同。您需要导入您的环境(首先您需要定义文件替换和任何内容),然后您可以读取您的环境变量。请查看doc