首页 > 解决方案 > 如何修复“语法错误:应为“#”、“\n”、“\r”或[\t],但找到“[”。” yargs 模块检索哪个?

问题描述

我目前正在使用 bitbucket 管道进行持续部署。我已将“npm test”命令放入管道中,因为我使用 mocha 对我的节点项目进行单元测试。

当我在本地 shell 上运行“npm test”来测试我的路由时,它们每个都可以工作。但是当 bitbucket 的临时服务器运行命令时,我检索到这个错误:

stork@1.0.0 test /opt/atlassian/pipelines/agent/build
> mocha tests/**/*.js
/opt/atlassian/pipelines/agent/build/node_modules/mocha/node_modules/yargs/yargs.js:1163
      else throw err
           ^
SyntaxError: Expected "#", "\n", "\r" or [ \t] but "[" found.
npm ERR! Test failed.  See above for more details.

我最近在我的存储库中实现了一个 config.toml 文件,以便在我的代码中放入一些变量(如域、路径等)。从那一刻起,在管道中运行的单元测试不再起作用。

这是 toml 解析的问题吗?

这是我的 toml 文件:

title = "configuration"

[charlie]
domain = "localhost"
path = "/"
port = 3000

任何提示都应该非常感激。

标签: node.jsmocha.jsbitbucket-pipelinesyargstoml

解决方案


经过一番研究,我找到了问题的根源。

在 gitignore 文件中提到了我的 toml 文件,不要将它放在我的远程存储库中。

这就是为什么我必须复制/过去整个 toml 内容以将其放入单个 bitbucket 环境变量中的原因。有了这个技术,我肯定在我的服务器上有一个 config.toml 的 prod 版本,在我的机器上有一个 config.toml 的 dev 版本。

问题是我的 BitBucket 变量已被格式化为一行。实际上,toml 格式对换行敏感。这就是为什么我使用 json 而不是 toml 并且问题已得到解决的原因。


推荐阅读