admin管理员组文章数量:1406937
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 },
pany: {
type: CompanyType,
resolve(parentValue, args) {
axios.get(`http://localhost:3000/panies/${parentValuepanyId}`).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
);
}
},
pany: {
type: CompanyType,
args: { id: { type: GraphQLString } },
resolve(parentValue, args) {
return axios.get(`https://localhost:3000/panies/${args.id}`).then(
(response) => response.data
);
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
How my project is organised:
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.
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 },
pany: {
type: CompanyType,
resolve(parentValue, args) {
axios.get(`http://localhost:3000/panies/${parentValue.panyId}`).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
);
}
},
pany: {
type: CompanyType,
args: { id: { type: GraphQLString } },
resolve(parentValue, args) {
return axios.get(`https://localhost:3000/panies/${args.id}`).then(
(response) => response.data
);
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
How my project is organised:
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.
-
Both answers are correct. I generated the
tsconfig.json
and I had to use the mandts-node server.ts
. I'm new to Ts in Node, I always used with JS.Plus I had to change theschema.ts
toexport default new GraphQLSchema
– Gabriel Savian Commented Jan 10, 2022 at 15:25
2 Answers
Reset to default 4Your 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 notrequire
/modules.exports
and- a typescript piler
If you're not using TypeScript then use a .js
file extension.
The correct syntax is:
import sampleModule = require('modulename');
or
import * as sampleModule from 'modulename';
Then pile your TypeScript with --module monjs.
本文标签: javascriptNodeJs Cannot Find Module Path and name are correctStack Overflow
版权声明:本文标题:javascript - NodeJs Cannot Find Module. Path and name are correct - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744972581a2635321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论