Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
NodeJs Cannot Find Module. Path and name are correct

I'm trying to debug a simple error in NodeJs. Error: Cannot find module './schema/schema'. I tried to create a dummy file with a string and trying to require in my project root and the same error showed up. I tried to delete the folder and the file about 3x and the error persist. Here is my code:

const express = require('express');
const expressGraphQL = require('express-graphql').graphqlHTTP;

const schema = require('./schema/schema');

const app = express();

app.use('/graphql', expressGraphQL({
    schema: schema,
    graphiql: true
}));

app.listen(4000, () => {
    console.log('Listening');
});

My schema file

const graphql = require('axios');
const axios = require('axios');

const {
    GraphQLObjectType,
    GraphQLString,
    GraphQLInt,
    GraphQLSchema,
} = graphql

const CompanyType = new GraphQLObjectType({
    name: 'Company',
    fields: {
        id: { type: GraphQLString },
        name: { type: GraphQLString },
        description: { type: GraphQLString }
    }
});

const UserType = new GraphQLObjectType({
    name: 'User',
    fields: {
        id: { type: GraphQLString},
        firstName: { type: GraphQLString },
        age: { type: GraphQLInt },
        company: {
            type: CompanyType,
            resolve(parentValue, args) {
                axios.get(`http://localhost:3000/companies/${parentValue.companyId}`).then(
                    (response) => response.data
                );
            }
        }
    }
});

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        user: {
            type: UserType,
            args: { id: { type: GraphQLString } },
            resolve(parentValue, args) {
                return axios.get(`http://localhost:3000/users/${args.id}`).then(
                    (response) => response.data
                );
            }
        },
        company: {
            type: CompanyType,
            args: { id: { type: GraphQLString } },
            resolve(parentValue, args) {
                return axios.get(`https://localhost:3000/companies/${args.id}`).then(
                    (response) => response.data
                );
            }
        }
    }
});

module.exports = new GraphQLSchema({
    query: RootQuery
});

How my project is organised:

enter image description here

I tried to clone this project in another machine and the error persist. Does anyone have a clue about this? There are no typos, no spaces in the file, also I tried to delete node modules and run yarn and no success.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your files have a .ts extension.

It can't find the file because it doesn't have a .js or .cjs file extension.

If you're using TypeScript, then use:

  • import/export and not require/modules.exports and
  • a typescript compiler

If you're not using TypeScript then use a .js file extension.

over 4 years ago · Santiago Trujillo Report

0

The correct syntax is:

import sampleModule = require('modulename');

or

import * as sampleModule from 'modulename';

Then compile your TypeScript with --module commonjs.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!