How to import third party module in node.js with typescript, if there #types is not available? - node.js

I want to import sails-redis, modelling, clean-obj and other 3rd party modules in Node, but I'm using typescript. so it is showing me an error:
Can not find a declaration module for clean-obj
I'm doing this:
import * as cleanObj from 'clean-obj';
It is not working, showing an error.
But if do as below it works, but I don't think is it a proper way in typescript or not.
const cleanObj-obj = require('clean-obj');
I want to import 3rd party module which does not have typings in npm.
Kindly suggest me a proper way to add typings for them.

Related

Nuxt3 - how to use vite-plugin-wasm

I'm building a Nuxt3 application where I need to import and use a 3rd party node package which internally is using webassembly.
In my case, the package I need is https://github.com/higumachan/lindera-js
Where do I import the package?
In one of my components, right at the beginning of the <script> tag.
And afterwards I'm using it in one of the methods.
(Note: I removed unnecessary code below)
<template>
...
</template>
<script>
import * as lindera from "lindera-js";
...
methods: {
doTranspile() {
const tokenized= lindera.tokenize(this.input);
console.log(tokenized);
},
},
...
</script>
What is the Problem?
After dev server compiles everything and I reload the page in the browser, I get the following error:
[vite] Internal server error: "ESM integration proposal for Wasm" is not supported currently.
Use vite-plugin-wasm or other community plugins to handle this.
Alternatively, you can use .wasm?init or .wasm?url.
See https://vitejs.dev/guide/features.html#webassembly for more details.
The Question
How can I use the package without problems in Nuxt3? Do I have to use vite-plugin-wasm and if so, how and where to use/import it?
Or is there any other way to use a package which is using webassembly?
I found some similar questions on SO, but not sure if they can be used like this in Nuxt3 as I'm fairly new to Nuxt in general.
How to use embedded Webassembly in Vite?
How to include an WASM npm module in svelte with vite?

Webpack with Next.js bundles file it is not supposed to in client bundle

I have a Next.js app with mongoose to connect to my mongodb. The models import db.ts to make sure that there is an active connection to the database like so:
import { model, models, Schema } from "mongoose";
import "../../db";
This is the code that connects to my database:
import mongoose from "mongoose";
mongoose.connect("mongodb://admin:admin#localhost:27022/admin");
I have gone ahead and made some serverless functions in next.js and added some database fetching from the models in my getServerSideProps. All of which worked perfectly fine. I can interact with the models, create new Documents, delete them and update them. there are no issues.
The Problem
I recently added a new component: it is at /pages/flashcards/[id].tsx. Just like my other components, this one imports one of my mongoose models. However, for some reason, Webpack feels like it should bundle the model and its import of ../../db and send it and send it over to the client, which results in this error:
TypeError: mongoose__WEBPACK_IMPORTED_MODULE_0___default(...).connect
is not a function
Again: This does not happen with any of my other components which use the exact same models as the component which is having these problems.
The issue occurs because you have the following unused import in the /pages/flashcards/[id] page.
import question from "../../db/models/question";
Any code inside getServerSideProps or getStaticProps, and imports used exclusively by these methods, is removed by Next.js when building the client bundle.
However, since question is not explicitly being used in getServerSideProps, Next.js can't figure out the import is only meant to be used on the server. This means it will be included in both the server and client bundles.
You can use the Next.js Code Elimination tool to verify what Next.js eliminates from the client-side bundle. You'll see that if you run your page's code through it, the import is not removed. However, as soon as you reference and use it inside getServerSideProps, Next.js automatically eliminates it from the imports.
Make sure to always comment out/remove unused imports (there are linting rules to help you do that).
Have you tried upgrading the next npm package to the latest version? (12.0.8 as of this writing). I had a similar issue with Next giving inconsistent errors between different API routes, all configured the same way but some raising the same TypeError you shared. Upgrading the package resolved the issue for me.

import twilio's Authy library in nest js

we usually use below statement to use authy library in node file using js ,mostly by require statement !
const authy = require('authy')('API KEY');
I've moved my code to nest eco system and now How should i do the same using typescript ,as i also want to pass API Key to it ?
I've tried below code as well ,but still it's not working
import { authy } from 'authy'(API KEY)
suggest something !
I have faced a similar issue in my NestJS project when using twillio library.
Currently, I have resolved this by importing it this way:
import authy = require('authy');
If, this doesn't work for you (for any reason e.g. TypeScript compile error), then can you try the following import statement?
import * as Authy from 'authy';
Also, let me know which one works for you.

How to Structure a TypeScript Project like in Java with Modules and Dependencies?

i have a problem with TypeScript and how to separate modules and import them.
In Java with Maven or Gradle, i can have three modules or projects where:
project-dao: Data Access Object classes
project-dto: Data Transfer Object classes
project-services: Services classes
project-dto is transversal to dao and services, so i can include this module like a project dependency easily in both.
In TypeScript i don't know how do make the same. I see examples with imports of compiled code in Javascript with relative paths or using npm publish for later install in another project.
Someone can i help me with some approach for this issue.
Thanks.
#Paolo : I hope your question is about creating different modules and importing them to your root module. If that's the case, you can very well create a new module with #NgModule annotation. if you want to load this module with the root module, just import this in you app.module.ts as
import {customModule} from './custom.module';
If you just want to load it lazily, include this your routing, with loadChildren property.
But, if a class that's already declared in another module, whether an app module, #NgModule, or third-party module, do not add this to the declaration.
From a DTO's perspective, I guess, you need to create an incline object as
var userObject = {
email : req.body.email,
password: req.body.password
}.
UserObject can then be passed to the next layer.

How to register module

I testing Aurelia framework and RethinkDB.
How can I register Require and RethinkDB to access them from Aurelia ?
import {require} from "require" //require.js is missing
r = require("rethinkdb"); // require is not a function
Using the ES6 module system's import syntax removes the need to use requirejs and the require function. SystemJS which Aurelia uses in the sample skeleton application can handle AMD formatted modules (which is the format Require uses).
That being said, rethinkdb would be on your server, not on the client, if I'm not mistaken.
Have you tried
import require from "require";
This would import the entire require module and doesn't expect an export named "require".

Resources