【问题标题】:Github Actions for NodeJS - 'Error: Cannot find module' for local fileNodeJS 的 Github 操作 - 本地文件的“错误:找不到模块”
【发布时间】:2021-01-27 20:32:48
【问题描述】:

我有一个 Node.js 项目的 GitHub 工作流文件:

name: NodeJS CI

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [10.x]

    steps:
    - uses: actions/checkout@v2
    - name: Using Node version ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test
    - name: Upload code coverage
      run: bash <(curl -s https://codecov.io/bash) -t $CODECOV_TOKEN
      env:
        CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

项目结构如下:

.
├── package-lock.json
├── package.json
├── src
│   ├── api
│   │   ├── calendar.js
│   │   ├── credentials.js
│   │   └── token.js
│   ├── app.js
│   ├── builders
│   │   └── event.js
│   ├── config
│   │   └── index.js
│   ├── loaders
│   │   ├── express.js
│   │   ├── index.js
│   │   └── mongo.js
│   ├── models
│   │   ├── credential.js
│   │   └── token.js
│   ├── scripts
│   │   └── tokenGenerator.js
│   └── services
│       ├── calendar.js
│       ├── credentials.js
│       ├── google.js
│       ├── mongo.js
│       └── token.js
└── tests
    ├── dbHandler.js
    └── services
        ├── calendar.js
        ├── google.js
        └── mongo.js

在本地,当我运行 npm test 时,我的测试通过,没有报告任何问题。但是,当测试在 Github Actions 上运行时,我收到以下错误:

Run npm test

> my-repo@1.0.4 test /home/runner/work/my-repo/my-repo
> nyc --reporter=html mocha 'tests/**/*.js' --exit


Error: Cannot find module '../models/credential'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/runner/work/my-repo/my-repo/src/services/mongo.js:2:140)
etc..

相关文件有相关要求:

const CredentialSchema = require('../models/credential');
const TokenSchema = require('../models/token');

我尝试了很多方法:

  • 使用process.cwd() 以及文件夹结构的其余部分来获取绝对文件路径
  • .js 附加到要求
  • 不同版本的节点
  • 我的机器上和 Github Action 中相同版本的节点

但似乎没有任何方法可以解决该错误。我的node_modules 文件夹在.gitignore 中被忽略。

repo 是私有的,但我认为这与它没有任何关系。

【问题讨论】:

    标签: node.js github mocha.js node-modules github-actions


    【解决方案1】:

    原来 git 没有在我的文件中检测到区分大小写的更改。

    运行 git mv -f src/models/Credential.js src/models/credential.js 然后推送更改修复它。

    【讨论】:

    • ^ 这个。多年来,我一直在使用 GIT 和 Node 生态系统,但它一直让我感到困惑,谢谢。
    • 从 Git 2.0.1 开始,不需要 -f 选项。
    猜你喜欢
    • 2020-06-14
    • 2015-06-21
    • 2020-09-19
    • 2018-08-05
    • 2022-11-23
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多