Github Actions for NodeJS-本地文件的“错误:找不到模块" [英] Github Actions for NodeJS - 'Error: Cannot find module' for local file

查看:48
本文介绍了Github Actions for NodeJS-本地文件的“错误:找不到模块"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NodeJS项目的github工作流程文件:

I have a github workflow file for a NodeJS project:

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上运行时,出现以下错误:

Locally, when I run npm test, my tests pass with no reported problems. However, when the tests run on Github Actions, I get the following error:

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..

有问题的文件有相对要求的地方:

Where the file in question has relative requires:

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

我尝试了很多事情:

  • 使用 process.cwd()以及其余的文件夹结构来获取绝对文件路径
  • .js 附加到require
  • 不同版本的节点
  • 我的计算机和Github Action中的节点版本相同
  • using process.cwd() along with the rest of the folder structure to get an absolute file path
  • appending .js to the require
  • different versions of node
  • same version of node on my machine and in the Github Action

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

but nothing seems to resolve the error. My node_modules folder is ignored in .gitignore.

回购是私人的,但我认为这与它无关.

The repo is private, but I don't think that has anything to do with it.

谢谢!:-)

推荐答案

结果证明git在我的文件中未检测到区分大小写的更改.

Turns out git hadn't detected a case sensitive change in my file.

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

Running git mv -f src/models/Credential.js src/models/credential.js and then pushing the changes fixed it.

这篇关于Github Actions for NodeJS-本地文件的“错误:找不到模块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆