Hey guys, when i tried to run the following im getting an error " can only be default-imported using the 'esModuleInterop' flag" - node.js

E:\React-Projects\video-chat\node_modules\ts-node\src\index.ts:421
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/server.ts:1:8 - error TS1259: Module '"E:/React-Projects/video-chat/node_modules/#types/express/index"' can only be default-imported using the 'esModuleInterop' flag
1 import express, {Application} from "express";
~~~~~~~
node_modules/#types/express/index.d.ts:108:1
108 export = e;
~~~~~~~~~~~
This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.src/server.ts:2:8 - error TS1259: Module '"E:/React-Projects/video-chat/node_modules/#types/socket.io/index"' can only be default-imported using the 'esModuleInterop' flag
2 import socketIO,{Server as SocketIOServer} from "socket.io";
~~~~~~~~
node_modules/#types/socket.io/index.d.ts:16:1
16 export = SocketIO;
~~~~~~~~~~~~~~~~~~
This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
at createTSError (E:\React-Projects\video-chat\node_modules\ts-node\src\index.ts:421:12)
at reportTSError (E:\React-Projects\video-chat\node_modules\ts-node\src\index.ts:425:19)
at getOutput (E:\React-Projects\video-chat\node_modules\ts-node\src\index.ts:553:36)
at Object.compile (E:\React-Projects\video-chat\node_modules\ts-node\src\index.ts:758:32)
at Module.m._compile (E:\React-Projects\video-chat\node_modules\ts-node\src\index.ts:837:43)
at Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
at Object.require.extensions. [as .ts] (E:\React-Projects\video-chat\node_modules\ts-node\src\index.ts:840:12)
at Module.load (internal/modules/cjs/loader.js:1001:32)
at Function.Module._load (internal/modules/cjs/loader.js:900:14)
at Module.require (internal/modules/cjs/loader.js:1043:19)
[nodemon] app crashed - waiting for file changes before starting...

I believe it is because your tsconfig.json file should have the esModuleInterop set to true in the compilerOptions section. Hope it helps

Related

Cannot require firebase

I am working on an Express based backend API with Firebase. I have some issues with Firebase configuration.
When I try to require firebase I get the following error.
const firebase = require('firebase');
node:internal/modules/cjs/loader:499
throw e;
^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /home/***/Desktop/Express/DemoBackend/node_modules/firebase/package.json
at new NodeError (node:internal/errors:387:5)
at throwExportsNotFound (node:internal/modules/esm/resolve:439:9)
at packageExportsResolve (node:internal/modules/esm/resolve:718:3)
at resolveExports (node:internal/modules/cjs/loader:493:36)
at Module._findPath (node:internal/modules/cjs/loader:533:31)
at Module._resolveFilename (node:internal/modules/cjs/loader:942:27)
at Module._load (node:internal/modules/cjs/loader:804:27)
at Module.require (node:internal/modules/cjs/loader:1022:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/home/***/Desktop/Express/DemoBackend/firebase_config.js:1:18)
{
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
I am working with NodeJS 18.6.0
Thanks for your help!

How to use nanoid without import

I am stuck with an issue,
I have to generate a 6 digit alphanumeric CODE which should be unique and for that i am using nanoid,
Now when i code this:
const {nanoid} = require("nanoid");
const ID = nanoid();
I got error:
const {nanoid} = require("nanoid");
^
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\HP\Desktop\test\node_modules\nanoid\index.js from C:\Users\HP\Desktop\test\server.js not supported.
Instead change the require of index.js in C:\Users\HP\Desktop\test\server.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\HP\Desktop\test\server.js:1:18) {
code: ←[32m'ERR_REQUIRE_ESM'←[39m
}
if I code this:
import { nanoid } from 'nanoid'
const id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
i got error:
(node:4636) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\HP\Desktop\test\server.js:4
import { nanoid } from 'nanoid'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
Now I have tried changing package.json file
"type":"module"
but my project use babel and it will automatically convert import to require and as result first error come again.
could you please tell me how to do nanoid with require'
Thank you
This is a feature, not a bug. See the changelog for details of the breaking change in version 4.0
https://github.com/ai/nanoid/issues/365

I want to know why I get that error: Cannot find module

code of app.js
code of app.js
code of index.js
code of index.js
folder structure
folder structure
this is the error that I get in the terminal:
**
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\onli\Desktop\mssqlRest\src\app' imported from C:\Users\onli\Desktop\mssqlRest\index.js
Did you mean to import ../src/app.js?
at finalizeResolution (internal/modules/esm/resolve.js:271:11)
at moduleResolve (internal/modules/esm/resolve.js:694:10)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:805:11)
at Loader.resolve (internal/modules/esm/loader.js:88:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:241:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:72:40)
at link (internal/modules/esm/module_job.js:71:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
[nodemon] app crashed - waiting for file changes before starting...
**

Cannot find module when importing pure Typescript package

I am trying to import a Typescript class from one of my Node packages and I am getting the following error:
module.js:549
throw err;
^
Error: Cannot find module '#sap/api'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
In this file:
import { Plugin } from "#sap/api";
new Plugin();
Which imports from this index.ts file:
export { Plugin } from "./src/plugin";
Which refers to "src/plugin.ts":
export class Plugin {
constructor() {}
onLoad() {}
onUnload() {}
onReload() {}
}
When I shift click on the import in VSCode it takes me to the correct file.
What am I doing wrong? I am new to Typescript, so sorry if it is a nooby mistake.

SyntaxError when declaring a class in TypeScript

I'm getting an error when declaring a class:
// This is all on my test1.ts file content
class WDesign {
wModel: string;
wQuer: string;
}
let logWDesign = (wd : WDesign) => {
console.log(wd.wModel + " " + wd.wQuer);
}
let wd1 : WDesign;
wd1.wModel = "SDS-9985";
wd1.wQuer = "escarni";
logWDesign(wd1);
Then, to show on Node command prompt:
>node test1.ts
I get the following:
[filePath].test1.ts:3
wModel: string;
^
SyntaxError: Unexpected token :
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
Typescript ver 3.1.3
Node ver. 8.12.0
Visual Studio Code 1.28.2
You firstly have to transpile the .ts file .js by simply running the command
tsc
node test1.js
This will generate javascript corresponding to your typescript files. Node cannot directly understand typescript. You have to give it javascript code.
You can use typescript-node-starter code provided by Typescript here to save configuration hustle.
Post comments if further help is required.

Resources