I want to know why I get that error: Cannot find module - node.js

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...
**

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

Why node throws the error: Cannot find module * imported from * using type="module" in package.json?

Using svelte-kit generator for the first time today generated a package.json with a "type": "module" in it.
Now I wanna call a postbuild script with:
"scripts": {
"postbuild": "node ./postbuild/index.js",
}
which contains:
const { tailwindExtractor } = require("tailwindcss/lib/lib/purgeUnusedStyles");
...
Because of the new error require is not defined I changed the ./postbuild/index.js file to this:
import { tailwindExtractor } from "tailwindcss/lib/lib/purgeUnusedStyles";
...
but I'm still getting an error calling that file from node:
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\kit\node_modules\tailwindcss\lib\lib\purgeUnusedStyles' imported from C:\kit\tools\postbuild\index.js
Did you mean to import tailwindcss/lib/lib/purgeUnusedStyles.js?
at finalizeResolution (internal/modules/esm/resolve.js:276:11)
at moduleResolve (internal/modules/esm/resolve.js:699:10)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:810:11)
at Loader.resolve (internal/modules/esm/loader.js:86:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:230:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:56:40)
at link (internal/modules/esm/module_job.js:55:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
What the heck is going on?
I'm using npm 7.8.0 and node v14.16.0.
I tried also with --experimental-modules: I get the same error!
This is the file I need to import: https://github.com/tailwindlabs/tailwindcss/blob/master/src/lib/purgeUnusedStyles.js

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

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

$ Is not defined scripts Gulp task

Hi I am trying to run my scripts task in gulp. My scripts path is defined as follows:
scripts: {
coffee: [
'app/assets/scripts/**/*.coffee'
],
js: [
'src/js/*',
'src/js/**/*'
],
map: ['src/js/**/*.map']
},
My original file paths are located in root/src/js/ and I want my scripts to be placed in their minified version inside root/app/assets/scripts/.
My gulp task is as follows:
gulp.task('scripts', function() {
return gulp.src('app/scripts/**/*.js')
.pipe($.if('*.js', $.uglify({preserveComments: 'some'})))
.pipe($.sourcemaps.init())
.pipe($.concat('app.js'))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest('app/assets/scripts'));
});
But for some reason I keep getting this error
[13:28:46] ReferenceError: $ is not defined
at Gulp.<anonymous> (C:\Users\Myuser\apps\myapp\gulpfile.js:165:9)
at module.exports (C:\Users\Myuser\apps\myapp\node_modules\gulp\node_
modules\orchestrator\lib\runTask.js:34:7)
at Gulp.Orchestrator._runTask (C:\Users\Myuser\apps\myapp\node_module
s\gulp\node_modules\orchestrator\index.js:273:3)
at Gulp.Orchestrator._runStep (C:\Users\Myuser\apps\myapp\node_module
s\gulp\node_modules\orchestrator\index.js:214:10)
at Gulp.Orchestrator.start (C:\Users\Myuser\apps\myapp\node_modules\g
ulp\node_modules\orchestrator\index.js:134:8)
at C:\Users\Myuser\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:129:2
0
at process._tickCallback (node.js:355:11)
at Function.Module.runMain (module.js:503:11)
at startup (node.js:129:16)
at node.js:814:3
UPDATE
Hey I think you are both right. $ was not defined and should be $ = require('gulp-load-plugins')();. However now that I am running scripts, I get the following error:
[15:24:46] TypeError: undefined is not a function
at Gulp.<anonymous> (C:\Users\Myuser\apps\myapp\gulpfile.js:166:13)
at module.exports (C:\Users\Myuser\apps\myapp\node_modules\gulp\node_
modules\orchestrator\lib\runTask.js:34:7)
at Gulp.Orchestrator._runTask (C:\Users\Myuser\apps\myapp\node_module
s\gulp\node_modules\orchestrator\index.js:273:3)
at Gulp.Orchestrator._runStep (C:\Users\Myuser\apps\myapp\node_module
s\gulp\node_modules\orchestrator\index.js:214:10)
at Gulp.Orchestrator.start (C:\Users\Myuser\apps\myapp\node_modules\g
ulp\node_modules\orchestrator\index.js:134:8)
at C:\Users\Myuser\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:129:2
0
at process._tickCallback (node.js:355:11)
at Function.Module.runMain (module.js:503:11)
at startup (node.js:129:16)
at node.js:814:3
Again, another vague error "undefined is not a function", is this explicit? Is there a $ function missing?
You probably need to use gulp-load-plugins package:
var $ = require('gulp-load-plugins')({lazy: true});

Resources