menu

ZEIT

Our mission is to make cloud computing as easy and accessible as mobile computing. You can find our Next.js community here.

Channels
Team

fs.readdirSync(__dirname) didn't work

June 22, 2019 at 1:54pm
The ZEIT community has a new home. This thread is preserved for historical purposes. The content of this conversation may be innaccurrate or out of date. Go to new community home →

fs.readdirSync(__dirname) didn't work

June 22, 2019 at 1:54pm
In a node.js project, I'm trying to loop through all the files in a directory, but an error occurs. Why?
{ Error: ENOENT: no such file or directory, scandir '/var/task/....

June 22, 2019 at 7:55pm
Hard to say without the relevant code.
  • reply
  • like
my guesses would be that your files were not imported by default and so you need to use includeFiles (https://zeit.co/docs/v2/deployments/builders/overview/#including-additional-files) to include the directory to your builds
like-fill
1
  • reply
  • like

June 23, 2019 at 10:06am
Thanks it helped
  • reply
  • like

June 24, 2019 at 11:25am
it's still now working. the same error.
  • reply
  • like
Basically I want to dynamically require modules in node.js by scanning a dirctory. I included the files as you mentioned. It didn't work.
  • reply
  • like
Basically I want to dynamically require modules in node.js by scanning a dirctory. I included the files as you mentioned. It didn't work.
Not possible. Behind the scenes zeit/ncc is used to bundle things and make a small footprint of your app.
  • reply
  • like
Then I guess lots of frameworks won't work on Now.
  • reply
  • like
But the intro on github for zeit/ncc says Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
  • reply
  • like
But the intro on github for zeit/ncc says Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
That is accurate. I will request some assistance on this one since I know that dynamic requires will work for certain packages but I am not sure how it works on your app.
like-fill
1
  • reply
  • like
Currently when you do dynamic requires in your app NCC can fail to import those dependencies. Maybe this will be fixed in a future release, but for now, do a static require.
  • reply
  • like
For now i won't use Now
  • reply
  • like
that line sounds funny :D
  • reply
  • like

June 25, 2019 at 9:25am
I just did a quick test.
This example includes a folder of template files
"includeFiles": [
"templates/**"
]
I have added the following to the code to dump out all the files available inside templates
console.log('listing files...');
fs.readdir('templates/', (err, files) => {
files.forEach(file => {
console.log(file);
});
});
The correct files are listed. I can load these files as part of the file listing.
like-fill
1
  • reply
  • like
Well, I will figure out something
  • reply
  • like
I just did a quick test.
This example includes a folder of template files
"includeFiles": [
"templates/**"
]
I have added the following to the code to dump out all the files available inside templates
console.log('listing files...');
fs.readdir('templates/', (err, files) => {
files.forEach(file => {
console.log(file);
});
});
The correct files are listed. I can load these files as part of the file listing.
A did the same thing. But it depends on the library to be honest. When I talked with the NCC team we couldn't pin down a scope for this.
  • reply
  • like
Guy Bedford just sent me this:
There are three main cases:
  1. Fully analyzable require, despite complex expressions - const x = './local-path.js'; require(condition ? x : y); will always bundle in the requires you might need.
  2. Wildcard require - require('./templates/' + template) will be treated as a wildcard require and bundle in everything in the templates directory as a possible require.
  3. Non-analyzable require - require('unknown') will be turned into a __non_webpack_require('unknown'). To opt-out of analysis you can always use your own __non_webpack_require__ reference directly too.
Edited
  • reply
  • like
private
This channel has been archived