簡體   English   中英

NodeJs 找不到模塊。 路徑和名稱正確

[英]NodeJs Cannot Find Module. Path and name are correct

我正在嘗試調試 NodeJs 中的一個簡單錯誤。 錯誤:找不到模塊“./schema/schema”。 我嘗試使用字符串創建一個虛擬文件並嘗試在我的項目根目錄中使用,但出現了相同的錯誤。 我試圖刪除大約 3x 的文件夾和文件,但錯誤仍然存在。 這是我的代碼:

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');
});

我的架構文件

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
});

我的項目是如何組織的:

在此處輸入圖像描述

我試圖在另一台機器上克隆這個項目並且錯誤仍然存在。 有人知道嗎? 文件中沒有拼寫錯誤,沒有空格,我也嘗試刪除節點模塊並運行yarn ,但沒有成功。

正確的語法是:

import sampleModule = require('modulename');

或者

import * as sampleModule from 'modulename';

然后用 --module commonjs 編譯你的 TypeScript。

您的文件具有.ts擴展名。

它找不到該文件,因為它沒有.js.cjs文件擴展名。

如果您使用的是 TypeScript,請使用:

  • import / export而不require modules.exports
  • typescript 編譯器

如果您不使用 TypeScript,請使用.js文件擴展名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM