soldair / node-walkdir

Walk a directory tree emitting events based on the contents. API compatable with node-findit. Walk a tree of any depth. Fast! Handles permission errors. Stoppable. windows support. Pull requests are awesome. watchers are appreciated.
MIT License
131 stars 22 forks source link

fs is not a function (typescript,vue) #38

Closed thoni56 closed 5 years ago

thoni56 commented 5 years ago

I have an issue I can't understand. I´m trying to use walkdir in a Vue project on Node.js with typescript and I get

Vue warn]: Error in mounted hook: "TypeError: fs[(intermediate value)(intermediate value)(intermediate value)] is not a function"

and

vue.runtime.esm.js?2b0e:1887 TypeError: fs[(intermediate value)(intermediate value)(intermediate value)] is not a function
    at statter (walkdir.js?16d7:140)
    at walkdir (walkdir.js?16d7:269)
    at TuneFinder.findTunesFrom (TuneFinder.ts?4f44:7)

And the offending code is

import walk from 'walkdir';
import { Vue } from 'vue-property-decorator';

export default class TuneFinder extends Vue {

    public findTunesFrom(directory: string) {
        const emitter = walk(directory);

        emitter.on('file', (path, stat) => {
            console.log(path);
        })
}

Line 140 in walkdir.js is this

        fs[options.find_links?'lstat':'stat'](path,statAction);

I can understand that there needs to be a dependency on fs but do I need to do anything particular to make that resolved?

thoni56 commented 5 years ago

Is fsevents required? npm i gives this warning

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.7 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.7: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
soldair commented 5 years ago

where is this code running? in node,a browser, electron etc? runtime versions would be helpful.

fsevents is truly optional so don't worry about that and fs is built into node so i don't see how it could be missing.

soldair commented 5 years ago

are you perhaps compiling the code for walkdir itself with typescript? your typescript should only compile the code in your project.

soldair commented 5 years ago

i think ill need a stand alone reproduction of this to help. if you make one or figure it out yourself please post the result here.

thoni56 commented 5 years ago

Thanks for responding! This is a Vue electron-builder project. I just realized that that code is probably running in the Renderer and I suppose fs isn't available there (I have limited experience in JavaScript land...). In that case fs actually wouldn't be a function (although I would have been put on the right track by a message like "fs cannot be resolved".)

I'll check this and get back.

thoni56 commented 5 years ago

Ok, that seems to be it. fs is not available in the renderer.