SERVER.JS RESUME DOTENV
const dotenv = require('dotenv-safe');
this.dotenv = dotenv.load();
问题:
1)我无法运行nodemon,如果它仅具有.env文件,则仅在包含.env和.env.example文件时运行,并且我想知道为什么和如何正确匹配它。
2)如何将.env
插入/env
文件夹而又不出现问题所在? >
3)在我的package.json
的开始脚本中是以下"start_dev": "nodemon app/backend/src/start.js"
,但是它给出了以下错误:
nodemon app / backend / src / start.js
[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *. *
[nodemon] starting `node app / backend / src / start.js`
consign v0.1.6 Initialized in C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel
fs.js: 115
throw err;
^
Error: ENOENT: no such file or directory, open '.env.example'
at Object.openSync (fs.js: 436: 3)
at Object.readFileSync (fs.js: 341: 35)
C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel \ node_modules \ dotenv-safe \ index.js: 27: 45)
at new Application (C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel \ app \ backend \ src \ config \ server.js: 11: 32)
at-the-object. <anonymous> (C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel \ app \ backend \ src \ config \ server.js: 65: 18)
at Module._compile (internal / modules / cjs / loader.js: 688: 30)
at Object.Module._extensions..js (internal / modules / cjs / loader.js: 699: 10)
at Module.load (internal / modules / cjs / loader.js: 598: 32)
at tryModuleLoad (internal / modules / cjs / loader.js: 537: 12)
at Function.Module._load (internal / modules / cjs / loader.js: 529: 3)
at Module.require (internal / modules / cjs / loader.js: 636: 17)
at require (internal / modules / cjs / helpers.js: 20: 18)
at aliasce-app-analyticals-panel \ app \ backend \ src \ start.js: 1: 78)
at Module._compile (internal / modules / cjs / loader.js: 688: 30)
at Object.Module._extensions..js (internal / modules / cjs / loader.js: 699: 10)
at Module.load (internal / modules / cjs / loader.js: 598: 32)
[nodemon] app crashed - waiting for file changes before starting ...
如果我在C:\Users\username\Documents\DEVELOPMENT\NEORIS\ ALIANSCE\aliansce-app-analyticals-panel\app\ ackend\src directory
中运行nodemon
可行!
答案 0 :(得分:1)
我看过这行代码-https://github.com/rolodato/dotenv-safe/blob/master/index.js#L27
它尝试读取文件.env.example
,但在当前文件夹中找不到它(运行pwd
进行检查)
可能是3种解决问题的方法
1)运行
cd app/backend/src
nodemon start.js
2)将文件.env
,.env.example
移动到父文件夹(aliansce-app-analyticals-panel
),然后运行nodemon app/backend/src/start.js
3)完全不要使用dotenv-safe
。使用像这样的简单脚本
只看这个简单的例子:
答案 1 :(得分:0)
在@ OlegDover和yargs的帮助下,我设法从另一个路径传递了.env
文件,并在开发过程中使用nodemon进行了热重载。
例如$ nodemon --watch /path/to/.env server.js --envPath=path/to/.env
将获取对.env文件的更改并重新开始部署。
.env
EXAMPLE_HOST=myhost
EXAMPLE_PORT=5566
env.js
/*
* Module dependencies
*/
const fs = require("fs");
const yargs = require("yargs");
/*
* Custom class to update process.env from custom filepath
* Ref: https://github.com/olegdovger/pizza-delivery-api/blob/master/lib/env.js
*/
class Env {
constructor(envPath) {
this.variables = [];
this._setup(envPath);
}
_setup(envPath) {
try {
const data = fs.readFileSync(envPath, {
encoding: "utf-8",
});
const stringArray = data.split("\n");
this.variables = stringArray.map((string) => {
const arr = string.split("=");
return {
name: arr[0],
value: arr[1],
};
});
} catch (err) {
console.error("Unable to load .env;", err);
}
}
load() {
this.variables.forEach((variable) => {
process.env[variable.name] = variable.value;
});
}
}
/*
* Load .env from argv filepath
*/
const argv = yargs.argv;
new Env(argv["envPath"]).load();
/**
* Register
*/
module.exports = {
EXAMPLE_HOST: process.env.EXAMPLE_HOST || "localhost",
EXAMPLE_PORT: Number(process.env.EXAMPLE_PORT) || 12345,
};
server.js
const { EXAMPLE_HOST, EXAMPLE_PORT } = require("./env");
然后可以在项目中加载/使用环境变量,如果没有在.env
文件中定义它们,则具有后备值。
即如果EXAMPLE_HOST
中没有.env
,则此值将默认为localhost