Can not use of require() to include any module - node.js

I'm trying to use
require('http');
to include any module. But I'm getting this error:
Uncaught ReferenceError: require is not defined
Note: http is just an example. It occcurs for any module.

Related

Getting error while using node js "chalk" module

I want to use node js chalk module but I am getting the following error
let chalk = require('chalk');
^
Error [ERR_REQUIRE_ESM]: require() of ES Module F:\Bhaskar\Learning\CompleteWebDevelopmentCourse\node_modules\chalk\source\index.js from F:\Bhaskar\Learning\CompleteWebDevelopmentCourse\NodeJs\npm.js not supported.
Instead change the require of index.js in F:\Bhaskar\Learning\CompleteWebDevelopmentCourse\NodeJs\npm.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (F:\Bhaskar\Learning\CompleteWebDevelopmentCourse\NodeJs\npm.js:1:13) {
code: 'ERR_REQUIRE_ESM'
}

While importing in node js module not found error occurring

I am using module type in node
So exported the route
as
export default router ;
But while importing as
import authRouter from './Routes/AuthRoute.js';
Module not found error occurs

Import ES Module

I am trying to create a standalone node.js project. The steps I followed are -
Created a new directory and initialised it with npm init.
Installed the new module for node-fetch.
Trying to import the fetch module using const fetch = require("node-fetch"); statement.
Getting the following error -
const fetch = require("node-fetch");
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/jatin/Desktop/test-app/node_modules/node-fetch/src/index.js from /Users/jatin/Desktop/test-app/index.js not supported. Instead change the require of /Users/jatin/Desktop/test-app/node_modules/node-fetch/src/index.js in /Users/jatin/Desktop/test-app/index.js to a dynamic import() which is available in all CommonJS modules. at Object.<anonymous> (/Users/jatin/Desktop/test-app/index.js:2:15) { code: 'ERR_REQUIRE_ESM' }
The node version I have on my machine is - v16.9.0.
You should use an ES module import instead of require.
import * as fetch from 'node-fetch';
You could also use a dynamic import, as the error message states.
const fetch = import('node-fetch');

Require() a module from a variable value

This works:
require('../../path/to/module')
This doesn't work
const cfg = '../../path/to/module'
require(cfg)
Prints an error in Chrome Dev Tools Console (the app is packed with browserify):
Uncaught Error: Cannot find module '../../path/to/module'
Why does this happen and is there any way I can load a local module using a variable?

Browserify / Electron / AngularJS Error: fs.existsSync is not a function

I'm getting a Error: fs.existsSync is not a function code after I bundle my code using Browserify.
Before I've tried to use require() but I kept getting Error: require is not defined due to require function not supported on client side. Browserify seems a solution to use. I've managed to solve require error with browserify but another one came up - fs.existsSync is not a function.
Here is the error code as shown on my console:
Error: fs.existsSync is not a function
[159]</</sqlite.prototype.connect#http://127.0.0.1:8080/services/bundle.js:169566:35
[160]</<#http://127.0.0.1:8080/services/bundle.js:169992:29
invoke#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:4718:19
enforcedReturnValue#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:4557:37
invoke#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:4718:19
createInjector/protoInstanceInjector<#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:4517:37
getService#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:4664:39
injectionArgs#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:4688:58
instantiate#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:4730:18
$controller#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:10369:28
compile/<#http://127.0.0.1:8080/views/assets/js/vendors/angular-ui-router.js:4081:28
bind/<#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:1247:18
invokeLinkFn#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:9934:9
nodeLinkFn#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:9335:11
compositeLinkFn#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:8620:13
publicLinkFn#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:8500:30
lazyCompilation#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:8844:25
updateView#http://127.0.0.1:8080/views/assets/js/vendors/angular-ui-router.js:4021:23
compile/</<#http://127.0.0.1:8080/views/assets/js/vendors/angular-ui-router.js:3959:11
$broadcast#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:18005:28
transitionTo/$state.transition<#http://127.0.0.1:8080/views/assets/js/vendors/angular-ui-router.js:3352:22
processQueue#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:16383:28
scheduleProcessQueue/<#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:16399:27
$eval#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:17682:28
$digest#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:17495:31
$apply#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:17790:24
done#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:11831:47
completeRequest#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:12033:7
requestLoaded#http://127.0.0.1:8080/views/assets/js/vendors/angular.js:11966:9
<div ui-view="" class="ng-scope">
I'm using electron, angularjs and sqlite to build my app but I'm stuck on this error, any help will be appreciated.
I've managed to solve my error without browserify by enabling nodeIntegration: true in main.js file (electron entry file). Then I was able to load my script (coolscript.js) which has require function inside of it to my html without problems -
<script>
require('coolscript.js')
</script>

Resources