when I am using import chalk from chalk then yargs is not working - node.js

when i am using
const yargs = require('yargs')
then it is not working for chalk
and when i am using import chalk from chalk then it is working for chalk but showing error for yargs

Related

await a promise without async function

I am writing a project in nodejs with express. In my app.js I noticed that I use an await without any async function. It works, but I don't know why.
Only valid if a file exists, otherwise I create it. And that's where I use the await
Here is my code:
import express from "express";
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
const app = express();
const filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(filename);
const filePath = path.join(dirname, "../files/products.json");
const exist = fs.existsSync(filePath);
if (!exist) await fs.promises.writeFile(filePath, JSON.stringify([]));
I would like someone to explain it to me.
Top level await can be used if your package is a module ("type": "module" in your package.json or a .mjs file extension)
https://v8.dev/features/top-level-await

How to import 'into-stream' package?

Code:
const express = require('express');
const app = express();
const cors = require('cors');
const mongoose = require('mongoose');
const multer = require('multer');
const inMemoryStorage = multer.memoryStorage();
const uploadStrategy = multer({ storage: inMemoryStorage }).single('image');
const { BlockBlobClient } = require('#azure/storage-blob');
const getStream = require('into-stream');
I can't use require in importing 'into-stream' module, it gives me ESM error:
Error [ERR_REQUIRE_ESM]: require() of ES Module
I tried adding type:module in package.json however require will not work if I do that. Do I install another version of into-stream? or should I use import rather than require. I am using node.js.
The package doesn't support require() anymore, just like node-fetch mentioned at this thread:
Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
You should add type: "module" to your package.json and then use:
import getStream from "into-stream";
I suggest you put that in the top of your file, just for code style :x
If this doesn't work, downgrade to version 6.0.0, which works with require(), according to the docs: https://www.npmjs.com/package/into-stream/v/6.0.0

Node+ExpressRouter -> CommonJS to ESM --> Import routes --> Error [ERR_MODULE_NOT_FOUND]: Cannot find module

I cant find my mistake after updating the node engine to 14.x and using ESM instead of CommonJS: exampleroute.js
import express from "express";
const router = express.Router();
router.get("/exampleroute", async (req, res) => {
console.log('......')
})
export default router;
server.js
import http from "http";
import express from "express";
import exampleroute from "./routes/exampleroute";
const server = express();
server.use("/exampleroute", exampleroute);
const httpServer = http.createServer(server);
const httpPort = 8080
httpServer.listen(httpPort, () => console.log(`server is running # port: ${httpPort}`));
Leads to: Error [ERR_MODULE_NOT_FOUND]: Cannot find module
What am I doing wrong?
Try using:
import exampleroute from "./routes/exampleroute.js";
ref.
Can't import "mysql2/promise" into ES module (MJS) on Node.js 13 / 14

Nestjs readFileSync return Cannot read property 'readFileSync' of undefined

I trying get file using method readFileSync:
import fs from 'fs';
import path from 'path';
const templateFile = fs.readFileSync(
path.resolve(
__dirname,
'../mail/templates/exampleTemplate.html',
),
'utf-8',
);
Nest still return me error:
TypeError: Cannot read property 'readFileSync' of undefined
I tryied used to path: ./templates/exampleTemplate.html, but result is the same
I have a structure file:
Since you're using Typescript and fs does not have a default export, you have to use import * as fs from 'fs'.
Try
import {readFileSync} from 'fs'
I fix it by change:
import fs from 'fs';
import path from 'path';
to:
import fs = require('fs');
import path = require('path');

Value of type 'Express' has no properties in common with type 'ServerOptions'

I am using typescript in my server and while I am familiar with typescript as I use it a lot I am not too used to it in the back end.
I am creating a socket I am also trying using es6 imports for because it helps with consistency. I am trying to change this:
require('dotenv').config({path: __dirname + '/.env'});
const app = require('express')();
const cors = require('cors');
const PORT = process.env.CHAT_PORT || 3000;
const ENV = process.env.NODE_ENV || 'development';
const server = require('http').createServer(app, {origins: 'http://192.168.x.xx/'});
to this:
import dotenv from 'dotenv';
import * as express from 'express';
import cors from 'cors';
dotenv.config({path:'__dirname' + '/env'});
const app = express();
import http from 'http';
const server = http.createServer(app, {origins: 'http://192.168.x.xx:8100/'})
but with the second one where i have const server = http.create(...)
I get various errors like:
Value of type 'Express' has no properties in common with type 'ServerOptions'.
as well as using types such as const app: Express = express() causes an import of Express from 'net'.
while I love typescript it feel like a complete wrestle in the backend.
Try using AMD modules, here's an AMD loader for NodeJS. npm i amd-loader -D

Resources