question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rewrite __dirname relative to output directory when targeting node

See original GitHub issue

As is, __dirname isn’t useful if we want to refer to static resources within a node package. One simple solution is to set __outputdir to be __dirname as is, and just refer to static resources relative to the output directory.

I also experimented with recovering the original __dirname semantic by calculating it relative to the output directory (assuming that the output directory is nested within the context).

Here’s what I mean in terms of a plugin:

function DirnamePlugin() {
}

DirnamePlugin.prototype.apply = function(compiler) {
  compiler.parser.plugin("expression __outputdir", function() {
    this.state.current.addVariable("__outputdir", "__dirname");
  });

  compiler.parser.plugin("expression __moduledir", function() {
    const moduleRelativePathFromContext = path.relative(compiler.context, this.state.module.context);

    const outputDir = "__dirname";
    // TODO: Figoure out the package dir relative to outputdir. Now just assumes it to be the parent of outputdir.
    const packageDir = `${outputDir} + '/../'`;

        this.state.current.addVariable("__moduledir", `${packageDir} + ${JSON.stringify(moduleRelativePathFromContext)}`);
        return true;
    });
}

Right now I use __moduledir to mean __dirname. Would you consider something like this to be merged into NodeStuffPlugin?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
anboxercommented, Mar 20, 2017

I solve this issue by using the DefinePlugin plugin

plugins: [
         new webpack.DefinePlugin({
            "__dirname": "__dirname"
        })
    ]

After adding this definition, webpack will not create VAR INJECTION for __dirname

PS: webpack@2.2.1

0reactions
webpack-botcommented, Oct 4, 2017

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

path.join vs path.resolve with __dirname - node.js
Is there a difference when using both path. join and path. resolve with __dirname for resolving absolute path in Node. js?
Read more >
path.normalize(p) : nodejs API
path.dirname(p) #. Return the directory name of a path. Similar to the Unix dirname command. Example:.
Read more >
How To Use __dirname in Node.js
__dirname is an environment variable that tells you the absolute path of the directory containing the currently executing file.
Read more >
How to avoid relative path hell in JavaScript / TypeScript projects
There's a need to know where both the current and target module are in the folder structure. This is a big hurdle to...
Read more >
file-loader - webpack - JS.ORG
Import (or require ) the target file(s) in one of the bundle's files: ... To get relative path you can use // const...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found