nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.45k stars 278 forks source link

fs.d.ts comes with identifier conflicts on fresh install #4268

Closed ryanab00 closed 9 months ago

ryanab00 commented 9 months ago

Version

v19.9.0

Platform

Microsoft Windows NT 10.0.19045.0 x64

Subsystem

No response

What steps will reproduce the bug?

Simply create a new vite with typescript enviorment.

  1. npm create vite@latest
  2. Choose vue
  3. Choose Typescript
  4. cd to new project
  5. npm install
  6. npm install --save-dev @types/node 6a (optional, issues persists with, or without it, just doing it because it was reccommended). Add "types": ["node"] to tsconfig.json after compileroptions
  7. Go to 'node_modules\@types\node\fs.d.ts' and you will see the following error.

Definitions of the following identifiers conflict with those in another file: Type, promises, PathLike, PathOrFileDescriptor, TimeLike, NoParamCallback, BufferEncodingOption, EncodingOption, OpenMode, Mode, Stats, StatsFs, Dirent, Dir, ReadStream, WriteStream, statSync, lstatSync, ReadPosition, WriteFileOptions, WatchEventType, WatchListener, StatsListener, BigIntStatsListener, F_OK, R_OK, W_OK, X_OK, COPYFILE_EXCL, COPYFILE_FICLONE, COPYFILE_FICLONE_FORCE, O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC, O_APPEND, O_DIRECTORY, O_NOATIME, O_NOFOLLOW, O_SYNC, O_DSYNC, O_SYMLINK, O_DIRECT, O_NONBLOCK, S_IFMT, S_IFREG, S_IFDIR, S_IFCHR, S_IFBLK, S_IFIFO, S_IFLNK, S_IFSOCK, S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG, S_IRGRP, S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH, UV_FS_O_FILEMAPts(6200)

How often does it reproduce? Is there a required condition?

I created two separate environments to make sure I didn't add anything to break it, or if I was using it improperly, but the error is shown right from the start of each of these applications, meaning I haven't added anything to them yet, they are just the base template from npm.

What is the expected behavior? Why is that the expected behavior?

fs should be able to be imported and used, but it is not recognized as a function. Upon further inspection there is an error with the node module itself.

What do you see instead?

After adding a txt file to the assets folder, and running a simple function to test the FileReader, error shows saying fs is not a function. This is most likely due to the error in the module itself.

import * as fs from 'node:fs'

const filePath = '/src/assets/file.txt';

try {
  const data: string = fs.readFileSync(filePath, 'utf8');
  console.log('File contents:', data);
} catch (err) {
  console.error('Error reading file:', err);
}

image image

import * as fs from 'fs'

const filePath = '/src/assets/file.txt';

try {
  const data: string = fs.readFileSync(filePath, 'utf8');
  console.log('File contents:', data);
} catch (err) {
  console.error('Error reading file:', err);
}

image image

Additional information

Not sure if I did anything wrong, or if my test function implementation is incorrect. If anyone can help me with this that would be greatly appreciated.

ljharb commented 9 months ago

This is a problem with your vue app. Do note that fs doesn't work in a browser, so anything client-rendered can't read from the filesystem.

ryanab00 commented 9 months ago

This is a problem with your vue app. Do note that fs doesn't work in a browser, so anything client-rendered can't read from the filesystem.

So I have to read it in a separate file first? Even so, won't the error in the module still persist?

ljharb commented 9 months ago

I can't speak to how your types are set up, but yes, you have to read it on the server.

Since this isn't a node question - more of a vue and typescript question - I'm going to close this.

ryanab00 commented 9 months ago

As I said it's a fresh install, it shouldn't have any errors in the module, implementation of it isn't what I am focusing on.

meyfa commented 9 months ago

Node.js does not publish its own types. The @types/node you installed, of which fs.d.ts is a part, is instead created by the community at DefinitelyTyped. If you found errors in that package, please report them there, as there is nothing that can be done here.

ljharb commented 9 months ago

ok, but that's still not a problem with node, it's a problem with the types for the module, or with vite, or with vue.

ryanab00 commented 9 months ago

I see, thanks for the help.