I'm am trying to import a module called datasource-sql
https://www.npmjs.com/package/datasource-sql
Every time I'm trying to run my project, I have this error :
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)
I of course installed said package with npm i datasource-sql
Also tried rm -rf node_module && package-lock.json && npm install
Here is my package.json :
{
...
"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"
]
}
Here is the file that requires 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;
The file where I'm calling it:
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);
});
I suspect this being a problem tied with typescript, even if at this point I am clueless. Just in case, here is my tsconfig.json :
{
"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"]
}
It also may worth mentioning that I have a .nvmrc file that contains 14.15.4
at the root of my folder.
Why isn't node able to find 'datasource-sql' ?