Skip to content
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 is not defined in Angular 6 app #100

Closed
chrisbenseler opened this issue May 4, 2018 · 13 comments
Closed

__dirname is not defined in Angular 6 app #100

chrisbenseler opened this issue May 4, 2018 · 13 comments

Comments

@chrisbenseler
Copy link

I'm trying to use Sass.js, in the browser, in an angular app.
Reading the docs, there is in the getting started the section "Using Sass.js with a module loader" that says to load
var Sass = require('sass.js/dist/sass');

the browser outputs
"Uncaught ReferenceError: __dirname is not defined
at sass.js:28"

Is it possible to use the sass.js this way?

@rodneyrehm
Copy link
Member

I don't quite understand why you're seeing that error, considering there's a guard for that:

if (typeof __dirname !== 'undefined') {
  return __dirname;
}

@joelfsreis
Copy link
Contributor

I've tried to update my angular app from version 5 to version 6 and I'm having the exact same issue. It was working fine before.

I managed to track the issue to the above guard, but is returning __dirname even with undefined as a value

@rodneyrehm
Copy link
Member

is it possible that the guard is broken by some build tool (e.g. transpiler)?

@joelfsreis
Copy link
Contributor

@rodneyrehm it should be that. Sincerely I don't know what angular 6 changes in that matter from angular 5. I will do some more research on my spare time trying to figure it out

@klsweetwood
Copy link

I don't know if anyone has continued to look into this, but I was running into the same problem with a new Angular 6 project. It looks like it is being broken by webpack. When webpack bundles the file, it changes that guard to "if (true)". I'm guessing it broke between Angular 5 & 6 because of the webpack changes.

I fixed the error by modifying sass.js. I just set a variable to "typeof __dirname !== 'undefined'" before the if statement and used that as the condition. Kinda dumb, but webpack doesn't mess with it. I'm sure there's a better way, but I'm just doing this for an experimental side project and don't want to spend much time on it. I also haven't tried to use Sass yet.. just fixed the error, so I don't know if there are other issues as well.

@rodneyrehm
Copy link
Member

@klbrisson interesting! would you like to send a PR for that?

@klsweetwood
Copy link

@rodneyrehm sure, I can do that. I'll see if there are other places where webpack changes it to true. I was still having trouble getting sass.js to work, but no errors were shown. So there may be other issues with Angular 6.
I ended up switching to use sass.sync.js even though it's not recommended. But my app needs everything synchronous anyways. So, having someone else verify that sass.js still works correctly in an existing project after I make those changes would probably be good to do.

@rodneyrehm
Copy link
Member

@klbrisson please note that "sass.sync.js" does not actually execute libsass synchronously, but rather does it in the main browser thread.

@joelfsreis
Copy link
Contributor

I have an angular app with Angular 5 using sass.js. Tomorrow is a bank holiday and I'll have some spare time and will try to update to Angular 6 with @klbrisson change on sass.js and see if that works. I'll let you know guys asap

@joelfsreis
Copy link
Contributor

@rodneyrehm I've done the upgrade to Angular 6 and fixed the issue on sass.js with webpack. I've created a pull request for that, is needed to be done the npm build to create the newest dist folder.

Sorry for a long time since my last reply. Cheers

@starovoitovs
Copy link

Can you please reference the PR here, really curious how stuff like this could happen

@joelfsreis
Copy link
Contributor

#105

@rodneyrehm
Copy link
Member

fix released as 0.10.12. thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants