Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs : TypeError: require(...) is not a function

I am trying to require a file and afterwards pass it to a var. I am following this tutorial to create an authentication system. After writing the server.js file and trying to compile I got a BSON error therefore I changed the line that required the release version of it in mongoose.

Here are my code and error:

server.js

require('./app/routes')(app, passport); 

Error

require('./app/routes')(app, passport);                    ^  TypeError: require(...) is not a function            at Object.<anonymous> (d:\Node JS learning\WorkWarV2\server.js:38:24)            at Module._compile (module.js:434:26)            at Object.Module._extensions..js (module.js:452:10)            at Module.load (module.js:355:32)            at Function.Module._load (module.js:310:12)            at Function.Module.runMain (module.js:475:10)            at startup (node.js:117:18)            at node.js:951:3  Process finished with exit code 1 

I have read that this usually means that requireJS is not getting loaded properly yet I am not aware why or how to fix it.

Edit due to comment:

As asked, here is the result of console.log(require);

like image 226
taigi100 Avatar asked Oct 08 '15 06:10

taigi100


People also ask

Is not a function node js require?

The "require(...) is not a function" error occurs for multiple reasons: Forgetting to place a semicolon between the require call and an IIFE. Calling the result of require() when the imported file does not have a default export of a function. Having cyclic dependencies (imports and exports between the same modules)

How do you fix require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.

What is require () in JavaScript?

1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.

Is not a function TypeError?

A TypeError: "x" is not a function occurs when a function is called on an object that does not contain the called function. When calling a built-in function that expects a callback function argument, which does not exist. When the called function is within a scope that is not accessible.


1 Answers

For me, when I do Immediately invoked function, I need to put ; at the end of require().

Error:

const fs = require('fs')  (() => {   console.log('wow') })() 

Good:

const fs = require('fs');  (() => {   console.log('wow') })() 
like image 134
mCY Avatar answered Sep 29 '22 07:09

mCY