【问题标题】:NodeJs Cannot Find Module. Path and name are correctNodeJs 找不到模块。路径和名称正确
【发布时间】:2022-01-10 14:36:21
【问题描述】:

我正在尝试调试 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,但没有成功。

【问题讨论】:

  • 两个答案都是正确的。我生成了tsconfig.json,我必须使用命令ts-node server.ts。我是 Node 中 Ts 的新手,我一直使用 JS.Plus 我不得不将 schema.ts 更改为 export default new GraphQLSchema

标签: javascript node.js typescript


【解决方案1】:

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

找不到文件,因为它没有.js.cjs 文件扩展名。

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

  • import/export 而不是require/modules.exports
  • 打字稿编译器

如果您不使用 TypeScript,请使用 .js 文件扩展名。

【讨论】:

    【解决方案2】:

    正确的语法是:

    import sampleModule = require('modulename');
    

    import * as sampleModule from 'modulename';
    

    然后用 --module commonjs 编译你的 TypeScript。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 2019-07-03
      • 2012-02-04
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多