sst / ion

SST v3
https://sst.dev
MIT License
1.88k stars 221 forks source link

Error: Failed to build function: No matching export in "../../../node_modules/sst/index.js" for import "Resource" v0.0.384 #476

Closed yashwanth2804 closed 4 months ago

yashwanth2804 commented 4 months ago
sst.config.ts

/// <reference path="./.sst/platform/config.d.ts" />

export default $config({
  app(input) {
    return {
      name: "hono-v2",
      removal: input?.stage === "production" ? "retain" : "remove",
      home: "aws",
    };
  },
  async run() {
    const secret = new sst.Secret("MONGOURI");

    const hono = new sst.aws.Function("Hono", {
      url: true,
      link: [secret],
      handler: "index.handler",
    });

    return {
      api: hono.url,
    };
  },
});
import { Resource } from "sst"
import { Hono } from "hono";
import { MongoClient } from "mongodb";
import { handle } from "hono/aws-lambda";

// Cached connection
let cachedClient = null;

// Function to connect to MongoDB
async function connectToDatabase() {
    if (cachedClient) {
        // Use the cached client
        return cachedClient;
    }
    // const client = new MongoClient("mongodb+srv://<username>:<password>@serverlessinstance.a8sna3ao.mongodb.net/?retryWrites=true&w=majority&appName=ServerlessInstancebooks");
    console.log(Resource.MONGOURI.value);
    const client = new MongoClient(Resource.MONGOURI.value);

    cachedClient = await client.connect();
    return cachedClient;
}

const app = new Hono()
    .get("/gets", async (c) => {
        // Connect to MongoDB
        const client = await connectToDatabase();
        const db = client.db('test');
        const collection = db.collection('sessions');

        // Perform database operations
        const result = await collection.findOne({});
        return c.json(result);
    });

export const handler = handle(app);

Error I am getting

App:        hono-v2
   Stage:      dev

|  Error        Error: Failed to build function: No matching export in "../../../node_modules/sst/index.js" for import "Resource"
|  at file:///root/sst/hono-v2/.sst/platform/src/components/aws/function.ts:1134:21
|  at processTicksAndRejections (node:internal/process/task_queues:95:5) {
|  promise: Promise { <rejected> [Circular *1] }
|  }

×  Failed
   Error: Failed to build function: No matching export in "../../../node_modules/sst/index.js" for import "Resource"
       at file:///root/sst/hono-v2/.sst/platform/src/components/aws/function.ts:1134:21
       at processTicksAndRejections (node:internal/process/task_queues:95:5) {
     promise: Promise { <rejected> [Circular *1] }
   }

I am using version sst ion SST ❍ ion 0.0.384 I have set secret with clic sst set secret command

yashwanth2804 commented 4 months ago

Solved it , I mistakenly used npm i sst instead of npm i sst@ion , after this i got access to Resource thing