最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - importing fs in nodejs - Stack Overflow

programmeradmin5浏览0评论

hello everyone!

I'm trying to import fs module in nodejs

first I was using require to import it like this

const fs = require('fs');

It was working fine for a while but suddenly stopped working and I'm getting this Error now

Error: Uncaught ReferenceError: require is not defined

so I tried using Import to import it like this

import fs from 'node:fs'

and I tried many other ways for importing it using import and I'm getting this two Errors

Error1: GET node:fs net::ERR_UNKNOWN_URL_SCHEME

Error2: Uncaught TypeError: Failed to resolve module specifier "fs". Relative references must start with either "/", "./", or "../".

Is there any other ways for importing module in Nodejs? or the problem is my code? I've had many problems with fs module in nodejs, I've tried googling and I couldn't find any solution for this one, I appreciate you guys helping me.

I'm using it to load a local file in Electronjs

Thanks.

hello everyone!

I'm trying to import fs module in nodejs

first I was using require to import it like this

const fs = require('fs');

It was working fine for a while but suddenly stopped working and I'm getting this Error now

Error: Uncaught ReferenceError: require is not defined

so I tried using Import to import it like this

import fs from 'node:fs'

and I tried many other ways for importing it using import and I'm getting this two Errors

Error1: GET node:fs net::ERR_UNKNOWN_URL_SCHEME

Error2: Uncaught TypeError: Failed to resolve module specifier "fs". Relative references must start with either "/", "./", or "../".

Is there any other ways for importing module in Nodejs? or the problem is my code? I've had many problems with fs module in nodejs, I've tried googling and I couldn't find any solution for this one, I appreciate you guys helping me.

I'm using it to load a local file in Electronjs

Thanks.

Share Improve this question asked May 13, 2022 at 19:28 Hoshmand QadirHoshmand Qadir 351 gold badge1 silver badge4 bronze badges 5
  • try this: import * as fs from 'fs'; – devdude19289 Commented May 13, 2022 at 19:30
  • 1 declare "type" : "monjs" in package.json – Arjis Chakraborty Commented May 13, 2022 at 19:31
  • Does this answer your question? How to use FS module inside Electron.Atom\WebPack application? – Abir Taheer Commented May 13, 2022 at 19:32
  • Google CommonJs(using the require syntax) and ES6 modules(newer with import) . They are different types of way of importing and have different extension as well as changes that could should be made in package.json – innocent Commented May 13, 2022 at 19:32
  • I tried that already but it's not working thanks anyway. – Hoshmand Qadir Commented May 13, 2022 at 22:29
Add a ment  | 

2 Answers 2

Reset to default 3

Try:

import fs from 'fs'

If you want the readfileSync directly, try:

import {readfileSync} from 'fs'

Also ensure that you add this to your package.json:

"type": "module"

Also ensure that you add this to your package.json:

"type": "module"
发布评论

评论列表(0)

  1. 暂无评论