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

Failing to load Sass from loadPreprocessor #5815

Closed
7 tasks done
Snugug opened this issue Nov 23, 2021 · 13 comments
Closed
7 tasks done

Failing to load Sass from loadPreprocessor #5815

Snugug opened this issue Nov 23, 2021 · 13 comments

Comments

@Snugug
Copy link
Contributor

Snugug commented Nov 23, 2021

Describe the bug

I have a Sass file I'm including directly from my HTML. I'm getting a an error, Preprocessor dependency "sass" not found. Did you install it? when it tries to compile my Sass. Digging through Vite's code, I see that resolved from loadPreprocessor is correctly finding the path to my Sass instance (/node_modules/sass/sass.default.dart.js). However, the preprocessor not found error gets thrown when trying to require it. This is where things get weird:

  • The file that it points to for Sass exists
  • If I require it using the same absolute path Vite finds in a top-level file, it requires fine.
  • If I do the same, but the top-level file is in Vite's dist folder, it still requires fine.
  • requireing Sass at that exact line in Vite; (return (loadedPreprocessors[lang] = require(resolved));) makes Sass throw the following error:
TypeError: Cannot read property 'indexOf' of undefined
    at /.../repo/node_modules/sass/sass.dart.js:95027:12
    at Object.applyHooksTransformer (/.../repo/node_modules/sass/sass.dart.js:6871:14)
    at Object.initHooks (/.../repo/node_modules/sass/sass.dart.js:6851:251)
    at Object.initNativeDispatchContinue (/.../repo/node_modules/sass/sass.dart.js:6817:9)
    at Object.initNativeDispatch (/.../repo/node_modules/sass/sass.dart.js:6811:9)
    at Object.getNativeInterceptor (/.../repo/node_modules/sass/sass.dart.js:8705:13)
    at Object.getInterceptor$x (/.../repo/node_modules/sass/sass.dart.js:8961:16)
    at Object.set$info$x (/.../repo/node_modules/sass/sass.dart.js:8992:16)
    at Object.main (/.../repo/node_modules/sass/sass.dart.js:2420:9)
    at main1 (/.../repo/node_modules/sass/sass.dart.js:17897:9)

It only throws this error when it gets required from loadPreprocessor in Vite. Requiring it on its own elsewhere, everything seems to work!

Reproduction

Minimal failure case here. My best guess as to the problem is that Squoosh uses Node workers and simply requiring it throws Sass into thinking it's in a browser.

System Info

System:
    OS: macOS 12.0.1
    CPU: (12) x64 Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
    Memory: 390.18 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.18.1 - ~/.volta/tools/image/node/14.18.1/bin/node
    npm: 6.14.15 - ~/.volta/tools/image/node/14.18.1/bin/npm
  npmPackages:
    vite: ^2.7.0-beta.8 => 2.7.0-beta.8

Used Package Manager

npm

Logs

vite:load 5.52ms [fs] /sass/style.scss?direct +81ms
3:55:13 PM [vite] Internal server error: Preprocessor dependency "sass" not found. Did you install it?
  Plugin: vite:css
  File: /.../repo/src/sass/style.scss?direct
      at loadPreprocessor (/.../repo/node_modules/vite/dist/node/chunks/dep-d87be5e9.js:20620:15)
      at scss (/.../repo/node_modules/vite/dist/node/chunks/dep-d87be5e9.js:20625:20)
      at compileCSS (/.../repo/node_modules/vite/dist/node/chunks/dep-d87be5e9.js:20362:40)
      at async TransformContext.transform (/.../repo/node_modules/vite/dist/node/chunks/dep-d87be5e9.js:20011:50)
      at async Object.transform (/.../repo/node_modules/vite/dist/node/chunks/dep-d87be5e9.js:42740:30)
      at async doTransform (/.../repo/node_modules/vite/dist/node/chunks/dep-d87be5e9.js:57398:29)
  vite:time 67.47ms /sass/style.scss +61ms

Validations

Snugug added a commit to Snugug/vite that referenced this issue Nov 23, 2021
Related to vitejs#5815, going through this function I found that the module was being resolved correctly but an error was being thrown during `require`, which the default error message swallowed. If I follow other uses of the debug helpers, this should throw the standard error unless the user's in debug mode, then it should debug the load error and throw the full stack trace.
@Snugug
Copy link
Contributor Author

Snugug commented Dec 1, 2021

Digging into this a little bit more, it looks like this error is coming from a code path being hit in Sass that tells it it's being loaded in a browser, but it appears to only be triggering in my codebase, not in a new clean codebase, and I'm unsure why

@Snugug
Copy link
Contributor Author

Snugug commented Dec 1, 2021

After digging into this and successfully reproducing a minimal case, I believe this is a Sass issue and not a Vite issue, so closing.

@Snugug Snugug closed this as completed Dec 1, 2021
@Snugug
Copy link
Contributor Author

Snugug commented Dec 1, 2021

Sorry, jumped the gun here, and sorry for the churn, it's not a pure Sass issue, it's an issue with how Vite and Sass interact at this edge case. A basic test to see if it's just a Sass problem (below) still requires as expected, whereas in Vite it doesn't:

const sass = require('sass');
const {ImagePool} = require('@squoosh/lib')

const file = sass.renderSync({file: './style.scss'});
console.log(file);

Same params as original issue

@Snugug Snugug reopened this Dec 1, 2021
@bluwy
Copy link
Member

bluwy commented Dec 4, 2021

@Snugug Is the repro updated with your findings? I'm also confused why the code is written in CJS.

@Snugug
Copy link
Contributor Author

Snugug commented Dec 15, 2021

@bluwy Yes, the linked repro case (here for ease of not needing to dig) is updated. As for CJS, because it's valid? And until mode of the Node ecosystem moves over it seems like a better format at the moment? Either way, it's broken when vite.config.js is an ESM, too.

@bluwy
Copy link
Member

bluwy commented Dec 16, 2021

I see. Thanks for the update! I didn't notice the CJS is for the plugin code, but yes that makes sense for the issue.

@dchenk
Copy link

dchenk commented Dec 28, 2021

I'm getting essentially the same bug when using vite-ssr:

vite-ssr


  vite v2.7.7 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

 -- SSR mode ready in 346ms.

12:52:55 PM [vite] Error when evaluating SSR module /src/components/AuthDialog/index.tsx:
Error: Cannot find module '/Users/user/repo/node_modules/sass/sass.default.dart.js' imported from '/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js'
    at viteResolve (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:67808:25)
    at Function._resolveFilename (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:67822:20)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at loadPreprocessor (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:20622:45)
    at scss (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:20637:20)
    at compileCSS (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:20367:40)
    at async TransformContext.transform (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:20015:50)
    at async Object.transform (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:42847:30)
Error: Preprocessor dependency "sass" failed to load:
Cannot find module '/Users/user/repo/node_modules/sass/sass.default.dart.js' imported from '/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js'
    at loadPreprocessor (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:20629:29)
    at scss (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:20637:20)
    at compileCSS (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:20367:40)
    at async TransformContext.transform (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:20015:50)
    at async Object.transform (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:42847:30)
    at async doTransform (/Users/user/repo/node_modules/vite/dist/node/chunks/dep-4a9cff06.js:57929:29)

@Crayfisher-zari
Copy link

I have same error with using @squoosh/lib.

When squoosh module imported, fail to load sass.

import { ImagePool } from "@squoosh/lib";
export default defineConfig({...})

@vanillajonathan
Copy link

You need to install the Sass preprocessor.

npm install sass --save-dev

@sapphi-red
Copy link
Member

After updating vite to latest (v2.9.5), I got a more specified error.

$ npm run build

> vite-project@0.0.0 build
> vite build

vite v2.9.6 building for production...
transforming (1) index.html
✓ 3 modules transformed.
[vite:css] Preprocessor dependency "sass" failed to load:
Cannot read properties of undefined (reading 'indexOf')
file: C:/Users/green/Downloads/vite-wmr5kn/src/style.scss
error during build:
TypeError: Cannot read properties of undefined (reading 'indexOf')
    at C:\Users\green\Downloads\vite-wmr5kn\node_modules\sass\sass.dart.js:95542:12
    at Object.applyHooksTransformer (C:\Users\green\Downloads\vite-wmr5kn\node_modules\sass\sass.dart.js:6909:14)
    at Object.initHooks (C:\Users\green\Downloads\vite-wmr5kn\node_modules\sass\sass.dart.js:6889:251)      
    at Object.initNativeDispatchContinue (C:\Users\green\Downloads\vite-wmr5kn\node_modules\sass\sass.dart.js:6855:9)
    at Object.initNativeDispatch (C:\Users\green\Downloads\vite-wmr5kn\node_modules\sass\sass.dart.js:6849:9)
    at Object.getNativeInterceptor (C:\Users\green\Downloads\vite-wmr5kn\node_modules\sass\sass.dart.js:8743:13)
    at Object.set$info$x (C:\Users\green\Downloads\vite-wmr5kn\node_modules\sass\sass.dart.js:9030:16)      
    at Object.main (C:\Users\green\Downloads\vite-wmr5kn\node_modules\sass\sass.dart.js:2469:9)
    at main1 (C:\Users\green\Downloads\vite-wmr5kn\node_modules\sass\sass.dart.js:17999:9)
Error: Preprocessor dependency "sass" failed to load:
Cannot read properties of undefined (reading 'indexOf')
    at loadPreprocessor (C:\Users\green\Downloads\vite-wmr5kn\node_modules\vite\dist\node\chunks\dep-3397b401.js:30394:29)
    at scss (C:\Users\green\Downloads\vite-wmr5kn\node_modules\vite\dist\node\chunks\dep-3397b401.js:30402:20)
    at compileCSS (C:\Users\green\Downloads\vite-wmr5kn\node_modules\vite\dist\node\chunks\dep-3397b401.js:30029:40)
    at async Object.transform (C:\Users\green\Downloads\vite-wmr5kn\node_modules\vite\dist\node\chunks\dep-3397b401.js:29653:55)
    at async ModuleLoader.addModuleSource (C:\Users\green\Downloads\vite-wmr5kn\node_modules\rollup\dist\shared\rollup.js:22161:30)

Thanks to the error, I found this happens without Vite.
For reproduction, run the script below.

const { ImagePool } = require('@squoosh/lib');
const sass = require('sass')

const res = sass.renderSync({ data: '.foo {color: red}' })
console.log(res)

This script fails with the same error. If you comment out the first line, it works without error.

I guess this is caused by @squoosh/lib globally declaring navigator variable. sass thinks it is running in browser because of it.

For a workaround add global.navigator = undefined like below. It worked for me. (Not sure if squoosh will work)

/* eslint-env node */
const { defineConfig } = require('vite');
const path = require('path');

const { ImagePool } = require('@squoosh/lib');

global.navigator = undefined

module.exports = defineConfig({
  root: 'src',
});

Closing as it is not a bug with Vite.

@sapphi-red
Copy link
Member

sapphi-red commented Apr 29, 2022

@Snugug Sorrry I noticed this comment after posting the previous comment.
Does this work as expected?
It does not work in my environment nor stackblitz. https://stackblitz.com/edit/node-ern8h9?file=index.js

@vaasav-kumar
Copy link

You need to install the Sass preprocessor.

npm install sass --save-dev

installing only sass preprocessor is not enough.. still am getting the dependency error

@sapphi-red
Copy link
Member

I got it.
#5815 (comment) worked because sass is loaded before squoosh.

const sass = require('sass');
const { ImagePool } = require('@squoosh/lib'); // global.navigator is declared inside

#5815 (comment) did not work because sass is loaded after squoosh.

const { ImagePool } = require('@squoosh/lib'); // global.navigator is declared inside
const sass = require('sass');

Vite loads config before loading preprocessors. So it was happening.
Closing as it is not a bug with Vite.

@vaasav-kumar Please create a new issue with reproduction if you are still having the error.

@github-actions github-actions bot locked and limited conversation to collaborators Jun 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants