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,

Labibeat
  • 83
  • 1
  • 2
  • 6
  • Possible duplicate of [Javascript require() function giving ReferenceError: require is not defined](https://stackoverflow.com/questions/23603514/javascript-require-function-giving-referenceerror-require-is-not-defined) – kaushik94 Sep 20 '17 at 10:24
  • Hey this is answered here https://stackoverflow.com/questions/23603514/javascript-require-function-giving-referenceerror-require-is-not-defined – kaushik94 Sep 20 '17 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. – Labibeat Sep 20 '17 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) – Jeremy Thille Jan 25 '18 at 16:13

1 Answers1

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.

tschumann
  • 2,776
  • 3
  • 26
  • 42