[英]Will an Electron based app pass system wide nodejs environment variables?
[英]Using environment variables in nx based nodejs app
我已經在 nrwl/nx 工作區中設置了一個包含多個 nodejs 和 angular 應用程序的項目。
我正在嘗試使用 nodejs 應用程序中的環境文件。
我已經設置了這樣的導入: import {environment} from './environments/environment';
然后我運行ng serve my-node-app
並顯示非生產環境。
現在我嘗試執行ng serve my-node-app --prod
以查看該應用程序如何與生產設置配合使用 - 但我收到錯誤消息:
Configuration 'production' could not be found in project my-node-app
。
這是項目的 angular.json 配置:
"ui-server": {
"root": "apps/ui/server",
"sourceRoot": "apps/ui/server/src",
"projectType": "application",
"prefix": "ui-server",
"schematics": {},
"architect": {
"build": {
"builder": "@nrwl/builders:node-build",
"options": {
"outputPath": "dist/apps/ui/server",
"main": "apps/ui/server/src/main.ts",
"tsConfig": "apps/ui/server/tsconfig.app.json",
"assets": ["apps/ui/server/src/assets"]
},
"configurations": {
"production": {
"optimization": true,
"extractLicenses": true,
"fileReplacements": [
{
"replace": "apps/ui/server/src/environments/environment.ts",
"with": "apps/ui/server/src/environments/environment.prod.ts"
}
]
}
}
},
"serve": {
"builder": "@nrwl/builders:node-execute",
"options": {
"buildTarget": "ui-server:build"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"apps/ui/server/tsconfig.app.json",
"apps/ui/server/tsconfig.spec.json"
],
"exclude": ["**/node_modules/**"]
}
},
"test": {
"builder": "@nrwl/builders:jest",
"options": {
"jestConfig": "apps/ui/server/jest.config.js",
"tsConfig": "apps/ui/server/tsconfig.spec.json"
}
}
}
}
我錯過了什么嗎?
我在尋找如何獲取.env
文件中定義的環境變量時發現了這篇文章。
process.env.ENVIRONMENTAL_VARIABLES
在前端部分可以在服務器(例如,在渲染時訪問Angular Universal
),具有.env
中的根Nrwl
monorepo和的WebPack屬性,如:
const dotenv = require('dotenv-webpack');
module.exports = {
plugins: [
new dotenv(),
],
};
不要忘記改變你的angular.json
:
...
"architect": {
"build": {
"builder": "@angular-builders/custom-webpack:browser",
"options": {
"customWebpackConfig": {
"path": "./webpack.browser.config.js",
"replaceDuplicatePlugins": true
},
...
我將自定義webpack
命名為webpack.browser.config.js
。
現在,假設您有一個server/...
,您將其用於某些后端內容,那么您將無法在那里訪問它們。 您需要安裝dotenv
包並在server/main.ts
,假設這是您的服務器的根目錄,需要這個包,這樣:
require('dotenv').config();
注:直到Angular 8
,我們能夠建立還webpack
-server相關的邏輯,在一個文件中,如webpack.server.config.js
。 因此,在webpack.browser.config.js
應用與dotenv
相關的基本相同的代碼是可行的。 但是,它不再起作用了。 Angular CLI Builders 被用來構建和服務 SSR 應用程序。
部署到Firebase
/使用Cloud Functions for Firebase
(以及可能的其他無服務器/FaaS)?
然后在您的functions
文件夾中,您還需要粘貼.env
文件。 我在這里假設您正在部署的functions
。
對於調試,我建議:
console.log(require('dotenv').config({ debug: true }));
可能會節省你很多時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.