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 iftype
property inpackage.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 andrequire
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
3 Answers
Reset to default 4ES 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
版权声明:本文标题:javascript - Why I can't use 'require' built in module in node? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744227794a2596178.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论