javascript - Lỗi: Không thể tìm thấy module 'datasource-sql'
Tôi đang cố gắng nhập một module có tên là datasource-sql
https://www.npmjs.com/package/datasource-sql
Mỗi khi tôi cố gắng chạy dự án của mình, tôi gặp lỗi này:
service_1 | Error: Cannot find module 'datasource-sql'
service_1 | Require stack:
service_1 | - /code/src/MyDatabase.js
service_1 | - /code/src/server.ts
service_1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
service_1 | at Function.Module._load (internal/modules/cjs/loader.js:725:27)
Tất nhiên tôi đã cài đặt gói đó với npm i datasource-sql
Cũng thử rm -rf node_module && package-lock.json && npm install
Đây là file package.json của tôi:
{
...
"main": "dist/server",
"scripts": {
"test": "jest",
"migration": "knex migrate:latest",
"migration:rollback": "knex migrate:rollback --all",
"seed": "knex seed:run",
"init:db": "npm run migration && npm run seed",
"build": "rm -rf dist && tsc",
"start": "node dist/server",
"watch": "nodemon",
"lint": "eslint"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/config": "0.0.38",
"@types/express": "^4.17.11",
"@types/graphql": "^14.5.0",
"@types/jest": "^26.0.22",
"@types/node": "^14.17.21",
"eslint": "^7.24.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.22.1",
"jest": "^26.6.3",
"nodemon": "^2.0.7",
"ts-jest": "^26.5.0",
"ts-node": "^9.1.1",
"typescript": "^4.1.3"
},
"dependencies": {
"apollo-server-express": "^2.19.2",
"axios": "^0.21.1",
"config": "^3.3.3",
"datasource-sql": "^1.6.0",
"express": "^4.17.1",
"graphql": "^15.5.0",
"knex": "^0.95.2",
"pg": "^8.5.1"
},
"keywords": [
"Typescript",
"GraphQL",
"Node",
"Javascript"
]
}
Đây là tệp yêu cầu datasource-sql :
const { SQLDataSource } = require("datasource-sql");
const MINUTE = 60;
class MyDatabase extends SQLDataSource {
getFruits() {
return this.knex
.select("*")
.from("restaurant")
.cache(MINUTE);
}
}
module.exports = MyDatabase;
Tệp mà tôi đang gọi nó:
import { ApolloServer, gql } from 'apollo-server-express';
import express from 'express';
import config from 'config';
const MyDatabase = require("./MyDatabase");
const knexConfig = {
client: 'pg',
connection: config.get('database'),
};
const db = new MyDatabase(knexConfig);
const typeDefs = gql`
type Country {
code: String
locales: [String]
}
type Restaurant {
restaurantUuid: String
name: String
allowReview: Boolean
images: [String]
country: Country
}
type Query {
restaurants: [Restaurant]
}
`;
const resolvers = {
Query: {
...
],
},
};
const main = async () => {
const app = express();
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({ db })
});
await server.start();
server.applyMiddleware({ app });
app.listen({ port: config.get('server.port') }, () => console.info(
`🚀 Server ready and listening at ==> http://localhost:${config.get('server.port')}${
server.graphqlPath
}`,
));
};
main().catch((error) => {
console.error('Server failed to start', error);
});
Tôi nghi ngờ đây là một vấn đề liên quan đến TypeScript, mặc dù ở thời điểm này tôi không biết gì. Chỉ để đề phòng, đây là tsconfig.json của tôi:
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es2020",
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"skipLibCheck": true,
"declaration": false,
"noFallthroughCasesInSwitch": true,
"composite": false,
"noImplicitAny": true,
"moduleResolution": "node",
"lib": ["dom", "es2020", "esnext.asynciterable"],
"sourceMap": true,
"emitDecoratorMetadata": true,
"strict": false,
"experimentalDecorators": true,
"outDir": "dist",
"rootDir": "src",
"baseUrl": "..",
"paths": {
"*": ["node_modules/*", "src/types/*"]
},
"resolveJsonModule": true
},
"include": [
"exercise/src/**/*",
"./src/**/*.ts",
"./src/**/*.tsx", "config/*",
"exercise/src/**/*.json"]
}
Cũng đáng để nhắc rằng tôi có một tệp .nvmrc chứa 14.15.4 ở thư mục gốc của tôi.
Tại sao Node không thể tìm thấy 'datasource-sql'?