-
Notifications
You must be signed in to change notification settings - Fork 292
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
Avoid the use of __dirname
when importing files dynamically
#216
Comments
A fix was attempted here in #217, but that had to be reverted unfortunately. Will hopefully come around on this again at some point soon. |
IMO, It's not an enhancement but a bug fix! No one can really onboard now 2.0 without this feature. Any complex server-side application needs files not only |
+1 .
|
@revskill10 can you share the exact code patterns you are using here? That would help a lot as this issue is all about finding the specific triggers that won't "overclassify". Is the pattern |
@guybedford I'm trying to deploy this example from const nodeStats = path.resolve(
__dirname,
'../../public/dist/node/loadable-stats.json',
) How Now 2 offer this ability ? |
@revskill10 that code example should definitely be working fine because of the usage of |
I ran into this specific problem when using the npm module @guybedford I think A repoducable example that fails - when const { send } = require('micro')
const microQuery = require('micro-query')
const unfluff = require('unfluff')
const fetch = require('node-fetch')
module.exports = async (req, res) => {
const query = microQuery(req)
if (!query.url)
return send(res, 400, { error: 'URL parameter missing' })
const fetchRes = await fetch(query.url)
const text = await fetchRes.text()
return send(res, 200, {
url: query.url,
...unfluff(text)
})
} Exception: {
"errno": -2,
"code": "ENOENT",
"syscall": "open",
"path": "/var/task/user/api/content/data/stopwords/stopwords-en.txt"
} (Note: Obviously this a bad example of a thing to expose publicly, but is simplified from what I am trying to do for illustration.) I tried and failed to find a way to patch the module for this. The module tries to load the txt files at run time using a variable name for the language code. Even adding a dozen or so I even tried including all the files needed alongside my function like this, and referencing them from my own function in the hope the bundler would see them (and so they would be accessible when the module tried to load them) but that didn't work either and resulted in the same error:
I don't see any way to work around this and I would be grateful even for hacky workaround. The module in this case is sufficiently complicated to be non-trivial to try and maintain a fork with different behaviour. I'll port it to be compatible if I have to, but I'm loathe to as I won't be able to maintain it properly. |
@iaincollins it seems the code you're referring to here is the following pattern: getFilePath = function (language) {
return path.join(__dirname, '..', 'data', 'stopwords', 'stopwords-' + language + '.txt');
}; note that this is actually |
Fixed in #378 |
Greetings!
Not sure if this issue would fit better on
now-builders
, but here we go.Is there a way to avoid the necessity of
__dirname
when trying to import files locally?This is our current usage:
index.js
:Many users face problems when doing
readFile( "./temp.txt")
, and the solution is above. But is there a way to avoid it?The text was updated successfully, but these errors were encountered: