简体   繁体   English

为什么JavaScript中的require模块无法导入fs库?

[英]Why is require module in JavaScript not able to import the fs library?

The javascript code is as follows: javascript代码如下:

const fs = require('fs');

function init() {
     alert("Done!");
}

init();

While executing the Javascript code, I am not able to get the alert Done! 执行Javascript代码时,我无法收到警报Done! on my webpage. 在我的网页上。 On further analysis I came to a conclusion that it is most probably because the require statement is not working (there was an alert when I commented the require statement). 经过进一步分析,我得出一个结论,很可能是因为require语句不起作用(当我评论require语句时出现警报)。 Why is it so? 为什么会这样呢?

require is not available in the browser but is used in NodeJS. require在浏览器中不可用,但在NodeJS中使用。 If you want to use it in your browser, you'll need a building tool like Browserify or Webpack . 如果要在浏览器中使用它,则需要一个诸如BrowserifyWebpack之类的构建工具。

By the way, the File System package is available in NodeJS only. 顺便说一下, 文件系统包仅在NodeJS中可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM