从自定义路径预加载 env 文件

2024-01-10

我试图在节点应用程序启动时预加载 .env 文件。

我有以下配置,但每当我运行时npm run start:dev我收到错误。

"scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "prestart": "npm run build",
    "start": "node .",
    "start:dev": "node -r dotenv/config . dotenv_config_path=/.dev.env",
    "start:test": "node -r dotenv/config . dotenv_config_path=/.test.env",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

or

"scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "prestart": "npm run build",
    "start": "node .",
    "start:dev": "node -r dotenv/config . dotenv_config_path=./.dev.env",
    "start:test": "node -r dotenv/config . dotenv_config_path=./.test.env",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

我的应用程序结构如下所示

另外,我已经阅读了有关不要使用多个配置文件的内容,因此我知道其缺点,但我只是想尝试看看这是如何工作的。

任何帮助将不胜感激。谢谢!!!

更新#1——错误

Error: Cannot find module 'C:\Development\index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)
    at Function.Module._load (internal/modules/cjs/loader.js:506:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] /cdn-cgi/l/email-protection start:dev: `node -r dotenv/config index.js dotenv_config_path=/.dev.env`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] /cdn-cgi/l/email-protection start:dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\nikhil\AppData\Roaming\npm-cache\_logs\2019-06-05T10_11_48_699Z-debug.log

日志档案 - -

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'run',
1 verbose cli   'start:dev' ]
2 info using [email protected] /cdn-cgi/l/email-protection
3 info using [email protected] /cdn-cgi/l/email-protection
4 verbose run-script [ 'prestart:dev', 'start:dev', 'poststart:dev' ]
5 info lifecycle [email protected] /cdn-cgi/l/email-protection~prestart:dev: [email protected] /cdn-cgi/l/email-protection
6 info lifecycle [email protected] /cdn-cgi/l/email-protection~start:dev: [email protected] /cdn-cgi/l/email-protection
7 verbose lifecycle [email protected] /cdn-cgi/l/email-protection~start:dev: unsafe-perm in lifecycle true
8 verbose lifecycle [email protected] /cdn-cgi/l/email-protection~start:dev: PATH: C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Development\node_modules\.bin;
9 verbose lifecycle [email protected] /cdn-cgi/l/email-protection~start:dev: CWD: C:\Development
10 silly lifecycle [email protected] /cdn-cgi/l/email-protection~start:dev: Args: [ '/d /s /c',
10 silly lifecycle   'node -r dotenv/config index.js dotenv_config_path=/.dev.env' ]
11 silly lifecycle [email protected] /cdn-cgi/l/email-protection~start:dev: Returned: code: 1  signal: null
12 info lifecycle [email protected] /cdn-cgi/l/email-protection~start:dev: Failed to exec start:dev script
13 verbose stack Error: [email protected] /cdn-cgi/l/email-protection start:dev: `node -r dotenv/config index.js dotenv_config_path=/.dev.env`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:301:16)
13 verbose stack     at EventEmitter.emit (events.js:182:13)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:182:13)
13 verbose stack     at maybeClose (internal/child_process.js:962:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
14 verbose pkgid [email protected] /cdn-cgi/l/email-protection
15 verbose cwd C:\Development
16 verbose Windows_NT 10.0.16299
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "start:dev"
18 verbose node v10.13.0
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 1
22 error [email protected] /cdn-cgi/l/email-protection start:dev: `node -r dotenv/config index.js dotenv_config_path=/.dev.env`
22 error Exit status 1
23 error Failed at the [email protected] /cdn-cgi/l/email-protection start:dev script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

问题出在 package.json 文件中的脚本部分。

没有 prestart:dev 可以在启动服务器之前构建项目。

  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "prestart": "npm run build",
    "prestart:dev": "npm run build",
    "prestart:test": "npm run build",
    "start": "node -r dotenv/config . dotenv_config_path=./.env",
    "start:dev": "node -r dotenv/config . dotenv_config_path=./.env.dev",
    "start:test": "node -r dotenv/config . dotenv_config_path=./.env.test",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从自定义路径预加载 env 文件 的相关文章

随机推荐

  • 如何监听 1200 个设备 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我正在使用德尔福 我有一个设备 设备
  • bash 中的三单引号是什么意思?

    我在 bash 中见过很多单引号和双引号以及反引号的用法 但从未见过接下来的内容 下面的文字记录是什么意思 它似乎表明 bash 认为三重单引号是有意义的 并且进一步似乎表明这个东西中的单引号也有特殊的含义 可以进行插值 我没有找到这方面的
  • 如何在 VSCode 中使用 Flake8?

    我的 VSCode 使用本地安装的 anaconda 环境 位于默认目录 将其放置在 Program Files 中 因此 我无法通过 VSCode 安装 flake8 出现权限错误 如果我在本地更新 conda 环境来安装 flake8
  • Windows 调度程序不执行批处理脚本,但脚本在命令行中运行良好

    我想创建以今天的日期作为文件夹名称的文件夹 然后 我想将数据从远程计算机移动到新创建的文件夹 我写了一个批处理脚本 看起来像 我的批处理脚本 名称 运行 bat ECHO off SET CurrentDate date 4 4 date
  • 为什么在 Xcode 11 上,UICollectionViewCell 一旦滚动就会改变大小(我已经在 sizeForItem AtIndexPath 中设置了大小:)?

    我在表视图单元格中有集合视图 我使用笔尖作为我的集合视图单元格 其中我对图像视图和标签使用自动布局 并且它处于自由格式模式 我在 tableviewcell 类中设置单元格大小 该类通过以下方法处理其中的 collectionview 委托
  • 根据一列从表中删除重复行

    我创建了一个包含 4 个表的多个内部联接的表 但结果返回了重复的记录 这是我正在使用的代码 SELECT tblLoadStop LoadID tblCustomer CustomerID tblLoadMaster BillingID t
  • Spinner 项目的自定义布局

    我在警报对话框中有一个微调器 我想减少微调项目之间的填充 因此我实现了以下内容 spinner row xml
  • Javascript 使用 Genie 效果最小化

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 我想最小化
  • 地图函数返回的列表在使用一次后消失[重复]

    这个问题在这里已经有答案了 我是Python新手 我正在使用 Python 3 3 2 我很难弄清楚为什么以下代码会给我一个错误 strList 1 2 3 intList map int strList largest max intLi
  • MVC4 - 有没有办法将根路由到“正常”未处理的 html 页面?

    我有一个 MVC4 应用程序 但我主要将它用于 WebAPI 部分 我想要一个 普通的旧 HTML 文件发送回用户 然后将使用 KnockoutJS 或 KendoUI 从 webapi 控制器中提取 JSON 我知道我可以这样做 rout
  • Java 可以用来确定下载的持续时间吗?

    只是问一下 你会如何做这件事 我知道有一些方法可以获取总体百分比来通知用户下载进度 但我不知道如何在一段时间内执行类似的操作 例如 下载完成所需时间 5 分钟 我所知道的就是百分比 写出写入的字节 然后将其除以长度 然后将其转换为百分比 如
  • android 服务 onBind SecurityException

    我不知道为什么但是在一些设备我的服务不允许绑定 java lang RuntimeException Unable to create application mypackage MyApplication java lang Securi
  • Ruby NET::HTTP 在正文之前读取标头(没有 HEAD 请求)?

    我使用 Net HTTP 和 Ruby 来抓取 URL 我不想抓取流音频 例如 http listen2 openstream co 334 http listen2 openstream co 334 事实上我只想抓取Html内容 所以没
  • Apache mod-auth-mysql 与 phpass 加密密码 (Wordpress)

    我需要对 WordPress 主站点之外的某些网页进行密码保护 用户更愿意使用他们在 WordPress 中已有的用户名和密码 显而易见的解决方案似乎是使用 Apace 模块进行基于 Mysql 的身份验证 mod auth mysql 然
  • HTML 输入数字步骤无需验证

    在 HTML 中 您可以添加step属性为数字类型的输入 我想要这个是因为我喜欢它如何通过仅按向上 向下键来简化用户的输入 我发现此属性的另一个功能是 它强制用户停留在该间隔内 HTML 通过验证来实现这一点 您可以手动输入任何值 但它不会
  • jQuery 选择器中的通配符

    我正在尝试使用通配符来获取 id 以 jander 开头的所有元素的 id 我试过 jander jander 但它不起作用 我知道我可以使用元素的类来解决它 但也可以使用通配符 div div div div 要获取所有以 jander
  • achartengine x 轴标签与值相比发生偏移

    我注意到我的时间表的 Xlabel 与 X 不同步 价值观 这些点应该位于标签的正上方 在左侧 可以 但是它向右移动 我不知道如何解决这个问题 我得到什么 http tinypic com r 2uqj905 7 http tinypic
  • Mysql:不允许从函数返回结果集

    我写了一个函数但收到此错误不允许从函数返回结果集 DELIMITER CREATE FUNCTION getTestFunction p ParentID int p ListName nvarchar 50 p Type nvarchar
  • matlab中识别相邻像素

    我们假设 A 是 1 1 1 1 1 1 1 2 2 3 3 3 4 4 2 2 3 3 4 4 2 2 2 3 4 4 4 4 3 3 5 5 5 5 5 5 我需要识别与特定强度值相邻的所有数字 例如 强度 1 3 和 4 与强度值 2
  • 从自定义路径预加载 env 文件

    我试图在节点应用程序启动时预加载 env 文件 我有以下配置 但每当我运行时npm run start dev我收到错误 scripts lint tslint project tsconfig json build tsc prestar