Deno: Can't import fs module

Created on 19 May 2020  路  8Comments  路  Source: denoland/deno

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

Most helpful comment

  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/[email protected]/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/[email protected]/fs/mod.ts";
writeFileStrSync("test.txt", "hello world");

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

All 8 comments

  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/[email protected]/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/[email protected]/fs/mod.ts";
writeFileStrSync("test.txt", "hello world");

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

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

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

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.

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/[email protected]/fs/mod.ts";
writeFileStrSync("text.txt", "file content");

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

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/[email protected]/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.

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

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);

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/[email protected]/fs/mod.ts";. This was the solution provided above and worked for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CruxCv picture CruxCv  路  3Comments

davidbarratt picture davidbarratt  路  3Comments

zugende picture zugende  路  3Comments

kitsonk picture kitsonk  路  3Comments

ry picture ry  路  3Comments