Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't import fs module #5630

Closed
mehmetalpsumer opened this issue May 19, 2020 · 8 comments
Closed

Can't import fs module #5630

mehmetalpsumer opened this issue May 19, 2020 · 8 comments

Comments

@mehmetalpsumer
Copy link

I am trying to import "fs" module

import {
  writeFileStrSync,
} from "https://deno.land/std/fs/mod.ts";

but I have the following error:

error: TS2345 [ERROR]: Argument of type '{ type: string; }' is not assignable to parameter of type 'string'.
    await Deno.symlink(originSrcFilePath, dest, {
                                                
    at https://deno.land/std/fs/copy.ts:117:49

TS2345 [ERROR]: Argument of type '{ type: string; }' is not assignable to parameter of type 'string'.
    Deno.symlinkSync(originSrcFilePath, dest, {
                                              
    at https://deno.land/std/fs/copy.ts:141:47

TS2345 [ERROR]: Argument of type '{ type: string; }' is not assignable to parameter of type 'string'.
    await Deno.symlink(src, dest, {
                                  
    at https://deno.land/std/fs/ensure_symlink.ts:33:35

TS2345 [ERROR]: Argument of type '{ type: string; }' is not assignable to parameter of type 'string'.
    Deno.symlinkSync(src, dest, {
                                
    at https://deno.land/std/fs/ensure_symlink.ts:65:33

Found 4 errors.

I run the app with unstable flag.

TS version: 3.7.2

@tkgalk
Copy link
Contributor

tkgalk commented May 19, 2020

  1. What version of Deno do you use? With Deno 1.0 you should be using TypeScript 3.9.2 (Deno ships with TypeScript, you can see that by typing deno --version in the terminal).
  2. A good practice is to lock versions of modules you are importing. So import { writeFileStrSync } from "https://deno.land/std@0.51.0/fs/mod.ts" instead. Notice the @0.51.0.

The following code on Deno 1.0 works for me:

import { writeFileStrSync } from "https://deno.land/std@0.51.0/fs/mod.ts";
writeFileStrSync("test.txt", "hello world");

Running with deno run --unstable --allow-write <filename.ts>.

@mehmetalpsumer
Copy link
Author

I upgraded my Typescript to 3.9.2 but still had the same issue.

However, adding version lock solved the problem. Thanks @galkowskit!

@KevinGimbel
Copy link

Can we discuss this further? I've had the exact same issue with the fs module and the error is resolved by specifying a version (0.51.0) - using 0.52.0 breaks my code as well as 0.50.0; Only 0.51.0 seems to work.

Why is this happening? And how should I, as a new user of Deno, know about this? I was just following the example code from the README at https://deno.land/std/fs#writefilestr - maybe there could be a note about pinning to a version instead of using master? The first time I checked the page I also didn't see the version switch at the top.

@tkgalk
Copy link
Contributor

tkgalk commented May 23, 2020

There is a note about it. Literally in the How To Use for std (https://deno.land/std#how-to-use). I agree the examples in the particular packages (like std/fs) should have version locked examples in them. Keep in mind, std is not stable yet.

image

There is a reason std it is not 1.0 yet. I'd guess the error comes from type changes that could be introduced in Deno but were not backwards compatible so old std versions got broken. Have you tried 0.52.0 on Deno 1.0.2?

It works for me.

import { writeFileStrSync } from "https://deno.land/std@0.52.0/fs/mod.ts";
writeFileStrSync("text.txt", "file content");

Works with deno run --unstable --allow-write <filename.ts>. Hope it helps!

@KevinGimbel
Copy link

I'm sorry, I missed the note in the README of std - I was looking at the README of the fs module (https://deno.land/std@0.52.0/fs).

I'm not sure which docs I read but it caught me off guard and as someone with no experience in TypeScript I was very confused until I found this issue thread.

@tkgalk
Copy link
Contributor

tkgalk commented May 24, 2020

Yeah. It would be nice if the docs showed the versionlocked examples.

@shakilkhan12
Copy link

I have this code but does not work for me

import { readJson, readJsonSync } from "https://deno.land/std/fs/mod.ts";

const f = await readJson("./names.json");
console.log(f);

@mehmetalpsumer
Copy link
Author

I have this code but does not work for me

import { readJson, readJsonSync } from "https://deno.land/std/fs/mod.ts";

const f = await readJson("./names.json");
console.log(f);

Did you run your app with --unstable flag? Also try to change your imports to import { readJson, readJsonSync } from "https://deno.land/std@0.52.0/fs/mod.ts";. This was the solution provided above and worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants