我正在嘗試除錯 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,但沒有成功。
uj5u.com熱心網友回復:
正確的語法是:
import sampleModule = require('modulename');
或者
import * as sampleModule from 'modulename';
然后使用 --module commonjs 編譯你的 TypeScript。
uj5u.com熱心網友回復:
您的檔案有.ts擴展名。
它找不到檔案,因為它沒有.js或.cjs檔案擴展名。
如果您使用的是 TypeScript,請使用:
import/export而不是require/modules.exports和- 打字稿編譯器
如果您不使用 TypeScript,請使用.js檔案擴展名。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/407776.html
標籤:
上一篇:在JSON檔案中使用字典
下一篇:RjxJs7-訂閱終結器?
