首页 > 解决方案 > 当项目中没有 node_modules 文件夹时,Node.js 在哪里搜索模块?

问题描述

例如,在 Node.js 中创建服务器时,我无法理解 - 如果我的项目中没有 node_modules 文件夹,那么 http 模块来自哪里?

示例如下:

const http = require('http');
const onRequest = (req, res) => {
   res.writeHead(200, {"Content-Type": "text/plain"});
   res.write('Hello World!');
   res.end();
}
const server = http.createServer(onRequest).listen(8000);

先感谢您。

附言

除了这个,我的项目文件夹中没有任何其他文件。

标签: javascriptnode.jsnode-modules

解决方案


如果没有节点模块,则 Node 将搜索该软件包是否已全局安装。
npm install -g packageName
如果未全局安装,则会出错。
但是http包**是节点中的**内置模块
所以它是默认安装的,或者您不必安装它。


推荐阅读