![](/img/trans.png)
[英]Could not find a declaration file for module. '/path/to/module-name.js' implicitly has an 'any' type
[英]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,請使用.js
文件擴展名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.