How we can use a node package in NestJS? I'm working on a NestJS project and I'm using microservices. I want to use a NodeJS circuit breaker called opossum.
First is it possible to use a node package in NestJS?
If the answer is yes, how can I use a node package in NestJS?
NestJS does not replace NodeJS. It's not a new runtime, it's just a framework that uses NodeJS and Typescript. Any package that's valid in Node is valid in Nest. Looks like import * as CircuitBreaker from 'opossum' should work. If not try import { CircuitBreaker } from 'opossum'
Related
I tried using new #apollo/server with NestJS instead of apollo-server-express. While using apollo-server-express, the apollo-server-core package is automatically used and no error is thrown. But when I remove apollo-server-express and install #apollo/server as dependency. There is the error.
Error: Cannot find module 'apollo-server-core'
Does anyone have any solution ??
There's no official support for Apollo Server v4 with #nestjs/graphql yet. You'd have to write a completely custom integration for that, or wait for this PR
A package for using Apollo Server 4 from NestJS is available here.
It works with both Express and Fastify.
I need konva to run on nodejs. I used this documentation from the creators themselves.
doc - https://github.com/konvajs/konva#4-nodejs-env
I myself use nestjs.
error
package.json
I solved this problem by changing the import
import Konva from 'konva';
on
const Konva = require('konva/cmj').default;
I create a Theta Token wallet, get its balance with theta.js (#thetalabs/theta-js) in Node Js app. But when I try to get a transaction, I get error: fetch is undefined. I thing, theta.js uses fetch. How can I use theta js in Node.js apps?
I tried add node-fetch lib, but it doesn't help me.
Maybe, there is another lib for Node apps with theta?
You can use
import fetch from 'node-fetch'; globalThis.fetch = fetch;
Since you need a global scope to use fetch here.
It works for me....install isomorphic-fetch using the command:
npm i isomorphic-fetch
and then require it in your file using:
const fetch = require('isomorphic-fetch')
It will work..check this out.
Stripe recently added typings to their stripe-node library with version 8. This replaced the need for the separate #types/stripe from DefinitelyTyped. In a node TypeScript environment using these types is straightforward, but I'm wondering if it's feasible to make use of these types in a client-side TypeScript application (Angular) that compiles to browser JavaScript.
It seems to work to npm install --save stripe and import Stripe from 'stripe' wherever I want to add Stripe typings, but with this being a node library I want to make sure I'm not missing anything obvious. Is there any concern with importing a library designed for node in a client application?
EDIT:
To be clear, I am not asking if it's ok to make full use of stripe-node in my client. That would be a huge security risk. Now that the library has typings built-in, it would be nice if we could reference those types in client-side typescript.
The types for stripe-node won't work for Stripe.js, you'll want to use these instead: https://github.com/stripe/stripe-js
npm install #stripe/stripe-js
See this bit about TypeScript support: https://github.com/stripe/stripe-js#typescript-support
Concerns would be there, only if you use your server secret in your frontend code. As far as you are not doing that & using server library just for the type definitions in frontend, There is no issue at all.
I use a monorepo package to export the TypeScript interfaces from the stripe-node library.
project
└── packages
├── common
└── web
Prevents clashing with Stripe from #stripe/stripe-js
Avoids adding stripe-node to web, which could be used in a bad way (e.g. using secrets)
Allows extending types when stripe API calls are made with expand
// project/packages/common/stripe.ts
import { Stripe } from "stripe";
type StripeInvoice = Stripe.Invoice;
type StripeSubscription = Stripe.Subscription;
type StripeSubscriptionList = Stripe.Response<
Stripe.ApiList<Stripe.Subscription>
>;
type StripeCustomer = Stripe.Customer;
export {
StripeInvoice,
StripeCustomer,
StripeSubscription,
StripeSubscriptionList,
};
// project/packages/web/request.ts
import { StripeSubscriptionList } from "#project/common/stripe";
const listSubscriptions = async () => {
// Wrapper function for the fetch API
return await fetchAPI<StripeSubscriptionList>("subscriptions", {
method: "GET",
});
};
How can I use the 'request' node.js module on the front-end?
normally I would retrieve it like so:
var request = require('request');
but this is not possible on the front-end since require is not recognized.
What is the best way to solve this?
To use node modules in the browser you can use a library called Browserify . This allows you to work with the common module pattern as well as the you can use this package browser-request to get the features of request module