admin管理员组

文章数量:1400007

I'm trying to initialise a very simple server using node. As I understand 'require' and 'HTTP' are built-in modules which I can use.

So my code looks as the following:

const http = require('http');
const server = http.createServer(() => {
  console.log("I hear you!");
});
server.listen(3000);

when i run node server.js i get the following error:

const http = require('http');
             ^
ReferenceError: require is not defined
    at file:///D:/Project/ZTM/recobrain-backend/server.js:1:14
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

I tried to install 'require' both locally and globally with the same result.

Finally, I managed to get it work with this piece of code:

import * as http from 'http'; 

The question is, why require syntax doesn't work?

I'm trying to initialise a very simple server using node. As I understand 'require' and 'HTTP' are built-in modules which I can use.

So my code looks as the following:

const http = require('http');
const server = http.createServer(() => {
  console.log("I hear you!");
});
server.listen(3000);

when i run node server.js i get the following error:

const http = require('http');
             ^
ReferenceError: require is not defined
    at file:///D:/Project/ZTM/recobrain-backend/server.js:1:14
    at ModuleJob.run (internal/modules/esm/module_job.js:152:23)
    at async Loader.import (internal/modules/esm/loader.js:166:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)

I tried to install 'require' both locally and globally with the same result.

Finally, I managed to get it work with this piece of code:

import * as http from 'http'; 

The question is, why require syntax doesn't work?

Share Improve this question asked Jan 15, 2021 at 10:15 Király RolandKirály Roland 1713 silver badges10 bronze badges 6
  • require is not a module. By default, node modules use monJS syntax but you can now use ES modules as well in node. If the extension of your script file is .mjs or if type property in package.json file is set to "module", then you need to use ES module syntax. – Yousaf Commented Jan 15, 2021 at 10:18
  • Is your package type set to module? (in your package.json) if so that's why. – itsezc Commented Jan 15, 2021 at 10:18
  • do you have anything which is similar to ""type": "module"" in your package.json. if yes remove that or use "import" instead – Akshay Kumar Commented Jan 15, 2021 at 10:18
  • yes, I have: "type": "module" in my package.json – Király Roland Commented Jan 15, 2021 at 10:21
  • "yes, I have: "type": "module" in my package.json" - @KirályRoland that is the reason require is not working. Remove it and require will work BUT then ES module syntax won't work. You can use either one of them, but not both. – Yousaf Commented Jan 15, 2021 at 10:21
 |  Show 1 more ment

3 Answers 3

Reset to default 4

ES Modules in Node 14 and after no longer have require by default.

just put this code at the top of your file:

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

Source: https://nodejs/api/modules.html#modules_module_createrequire_filename

Just remove type: "module" from package.json and you should be able to use require() or alternatively you can set it to type: "monjs"

Node 22 now finally has landed support for using CommonJS modules and ES modules alongside.

https://nodejs/en/blog/announcements/v22-release-announce

本文标签: javascriptWhy I can39t use 39require39 built in module in nodeStack Overflow