4

I'm trying to run a node.js script from my node.js server. The problem is that the modules I import with require () do not work anymore while all the modules and files are in the same folder.

var mysql = require('mysql');
        ^

ReferenceError: require is not defined
    at evalmachine.<anonymous>:3:13
    at ContextifyScript.Script.runInThisContext (vm.js:25:33)
    at Object.runInThisContext (vm.js:97:38)
    at Object.<anonymous> (D:\wamp\www\node_server\test_server.js:8:14)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)

Thank you in advance,

4
  • Possible duplicate of Javascript require() function giving ReferenceError: require is not defined Commented Sep 20, 2017 at 10:24
  • Hey this is answered here stackoverflow.com/questions/23603514/… Commented Sep 20, 2017 at 10:24
  • Thanks but it does not work, I call a file, and if this file call one or many modules, I have an error because require is not defined. Commented Sep 20, 2017 at 12:05
  • 2
    @kaushik94 A few months late, but your link has nothing to do with OP's problem. Yours is about client-side require, but here, OP is crearly using server-side (nodejs) Commented Jan 25, 2018 at 16:13

1 Answer 1

2

I had a similar problem - server-side nodejs thinking that require is not defined. It turns out the line "type": "module" in my package.json was causing this error, and once I removed that require worked as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.