Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Actions for NodeJS - 'Error: Cannot find module' for local file

I have a GitHub workflow file for a Node.js 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 }}

with a project structure as follows:

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

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

> [email protected] 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');

I've tried a number of things:

  • 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

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.

like image 762
Tom Avatar asked Oct 13 '20 13:10

Tom


People also ask

Can not find module node path?

To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import path with the following line of code import * as path from 'path' .

Can not find module import?

The "Error [ERR_MODULE_NOT_FOUND]: Cannot find module" occurs when you set the type attribute to module in your package. json file, but omit the file extension when importing. To solve the error, specify the extension when importing local files.

Can not find module index JS?

If you are getting the "Cannot find module" error when trying to run a local file, make sure that the path you passed to the node command points to a file that exists. For example, if you run node src/index. js , make sure that the path src/index. js points to an existing file.

What is index JS in node?

Index of Node. js is the file that runs initially once the Node. js code is executed. It is responsible for your application's startup, routing, and other functions.


1 Answers

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

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

like image 67
Tom Avatar answered Nov 15 '22 05:11

Tom