JS无法导入NPM模块

发布于 2025-01-27 17:57:42 字数 370 浏览 2 评论 0原文

我无法使用例如从'fs'';在我的main.js文件中导入npm模块,该文件与index.html链接在一起。 连接JS文件具有属性type =“模块”的脚本标签。 浏览器控制台中的错误会引发错误:und typeError:无法解析模块指定器“ FS”。相对引用必须以“/”,“ ./”或“ ../” ../code>开始。 但是NPM模块必须仅通过指向模块名称,没有路径来连接,我已经在另一个项目中做到了,并且它可以正常工作。

然后,我尝试将相对路径指向FS模块。但是node_modules没有文件夹'fs'。相反,它在每个文件夹名称的开始中包含了几个带有“ FS”的文件夹。

那一刻,我完全困惑:(

I can't import npm module using for example import fs from 'fs'; in my main.js file that linked with index.html.
Script tag that connect JS file has attribute type="module".
Error in console of browser throws error: Uncaught TypeError: Failed to resolve module specifier "fs". Relative references must start with either "/", "./", or "../"..
BUT npm modules must be connected by pointing only module name, without path and I already did that in another project and it worked correctly.

Then I tried to point relative path to fs module. But node_modules doesn't have folder 'fs'. Instead it contains several folders with 'fs' in start of each folder name.

At that moment I was completely confused :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓦然回首 2025-02-03 17:57:42

您需要将其作为导入 *作为'fs'';的fs导入。您可能还存在fs在网络中导入的问题,但这是另一回事。

可以找到更多详细信息在这里

编辑:问题是“无法导入NPM模块”。正如迈克尔·弗洛雷斯(Michael Flores)在评论中指出的那样,在原始答案中提出了建议,无论如何,这在fs不是模块中都无法安装 - 它是node.js的一部分

You need to import it as import * as fs from 'fs';. You may also have problems with fs import in the web, but that's a different issue.

More details can be found here

EDIT: The question was "Cannot import npm module". As pointed out by Michael Flores in comment and suggested in the original answer, this won't work anyway in web as fs is not module you can install - it's part of Node.JS

囍孤女 2025-02-03 17:57:42

fs是在节点服务器上运行时可用的模块,而不是在浏览器中运行。

fs is a module available when running on a Node server, not in the browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文