-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
__dirname returns '/' when js file is built with webpack #1599
Comments
See |
Ok, I set the node: {
__dirname: true
} But now the __dirname is empty, '' |
Could you provide a sample code for retrieving the proper directory name?. I'm currently using a module called app-root-path but I'd like to know if there is a webpack-native way of doing it |
Typically, |
I got the same result after trying the above config. webpack.config.js
src: ./server/index.js
It works for directly run the source. Am I missing something? |
Same 👍 ---UPDATE--- For now, I use this hack: plugins: [
new webpack.DefinePlugin({
$dirname: '__dirname',
}),
] |
@fritx I cannot find any related to your usage in the document. May I know how does it works? |
@goodseller Just replace |
Sorry, I found the hack unnecessary. // the webpack config just works
target: 'node',
node: {
__dirname: false,
__filename: false,
} |
wow, that works for me too, the setting seems counterintuitive. wish we had explanations for what true, false mean in each case. |
if I have a node module requiring in a module for me, through a config file, how can I have webpack include the files that are being required in for me so that they are in the build directory? basically I have a hapijs app, and am trying out webpack for the node build. Glue module reads in a config, and the __dirname is now the build directory, and the modules are still in src since they are not being required in explicitly. thoughts? |
@kellyrmilligan 😵 I think you need to make those modules (in trouble) plugins: [
new webpack.ExternalsPlugin('commonjs', ['a', 'b'])
] or just make them all // https://github.com/fritx/os-web/blob/dev/task%2Fwebpack.server.js
externals: [
(ctx, req, cb) => {
// if (/^\.?\//.test(req)) return cb()
if (/^\.\.?\//.test(req)) return cb() // fixed √
cb(null, `commonjs ${req}`)
},
], I have a project (experimental) 😆 which webpacks both client & server side. |
@kellyrmilligan Have you found a solution? |
I ended up abandoning the approach. Kept on running into brick walls |
I'd like to add that using node: {
__dirname: false
} will also work if you are targeting electron. |
Whatever this flag is doing, setting |
Playing with this a bit, assuming I have the following:
and inside
The following happens based upon what
The unset case seems the most counter-intuitive to me and I have a hard time understanding what the use case of injecting |
FWIW to get the actual behavior in node you need to write your own plugin. This is adapted from the NodeStuffPlugin (which handles the configuration discussed here). Just insert the following into your plugins array: {
apply(compiler) {
function setModuleConstant(expressionName, fn) {
compiler.parser.plugin('expression ' + expressionName, function() {
this.state.current.addVariable(expressionName, JSON.stringify(fn(this.state.module)));
return true;
});
}
setModuleConstant('__filename', function(module) {
return module.resource;
});
setModuleConstant('__dirname', function(module) {
return module.context;
});
} |
For everyone's reference, I just posted a similar issue here: #4303 |
I have the same issue but in thanks |
Still doesn't understand how use it with create-react-app + electron. In dev mode I set |
@Kupstas you might also try process.cwd() (not sure if that would work for you, but it may be worth trying?) |
@bvaughn I think that problem in some other place. In dev mode in __dirname I see |
I am encountering this issue. I will get around it for now by using |
Seems like webpack incorrectly replaces I got around this by applying an updated version of @amasad's custom plugin above: {
apply(compiler) {
function setModuleConstant(expressionName, fn) {
compiler.hooks.normalModuleFactory.tap('MyPlugin', factory => {
factory.hooks.parser.for('javascript/auto').tap('MyPlugin', (parser, _options) => {
parser.hooks.expression.for(expressionName).tap('MyPlugin', expression => {
parser.state.current.addVariable(expressionName, JSON.stringify(fn(parser.state.module)))
return true
})
})
})
}
setModuleConstant('__filename', function (module) {
return module.resource;
});
setModuleConstant('__dirname', function (module) {
return module.context;
});
}
}, |
If you are using webpack to bundle to target |
Also, __dirname was disabled by webpack. ref: - webpack/webpack#1599 - https://qiita.com/_neko/items/0d6bb1a14ac25fbc577e
Related to this issue, the documentation misleadingly indicates that the default value for both |
Fixed in webpack@5, for |
Doesn't work. |
set __dirname: false worked for me. |
I had issues with this working sporadically. I had to clear my cache sometimes. I also changed the So be warned, I searched for where node:internal/process/execution The custom plugin #1599 (comment) is the best solution. |
I have a working express file, which I 'webpack' it but when the bundled file is run the __dirname global changes to '/'. I need the absolute path for certain functions as res.sendFile. Any help?
The text was updated successfully, but these errors were encountered: