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.

External SCSS with JSX and Typescript : cannot find module, jsx doesn't exist

See original GitHub issue

Examples bug report

Example app with next-sass

with-external-styled-jsx-sass link

Describe the bug

Lint error when you install sass jsx with typescript

To Reproduce

  1. Create project

yarn create next-app --example with-external-styled-jsx-sass with-external-styled-jsx-sass-app

  1. Add dependencies in package.json
{
  "scripts": {
    "dev": "next",
    "build": "next build",
    "start": "next start",
    "type-check": "tsc"
  },
  "dependencies": {
    "@zeit/next-typescript": "^1.1.1",

    "next": "^8.1.0",
    "react": "^16.8.6",
    "react-dom": "^16.8.6"
  },
  "devDependencies": {
    "node-sass": "^4.7.2",
    "sass-loader": "7.1.0",
    "@types/next": "^7.0.6",
    "@types/react": "^16.4.16",
    "@types/react-dom": "16.0.11",
    "typescript": "3.2.4"
  }
}
  1. yarn install

  2. Add typescript to next config
const withTypescript = require('@zeit/next-typescript')


module.exports = withTypescript({
  webpack: (config, { defaultLoaders }) => {
    config.module.rules.push({
      test: /\.scss$/,
      use: [
        defaultLoaders.babel,
        {
          loader: require('styled-jsx/webpack').loader,
          options: {
            type: 'scoped'
          }
        },
        'sass-loader'
      ]
    })

    return config
  }
})
  1. rename index.js to index.tsx

Expected behavior

yarn dev It work but I don’t know why linter say module are not found or jsx doesn’t exist.

Screenshots

If applicable, add screenshots to help explain your problem. image image

System information

  • OS: Windows
  • Webstorm and VS Code
  • Version of Next.js: ^8.1.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
Ekrekrcommented, Aug 1, 2020

May be useful for someone:

Currently, it’s almost impossible to make SCSS and SASS work in newer versions of Next. At least it’s unreasonably hard.

Instead of using scss or sass, I ended up using MaterialUI and its makeStyles helper, so styles are stored in a separate file with ts extension.

For those who don’t like the idea of using MaterialUI (I don’t like it either, honestly), then styled-jsx is the way. As recommended by official docs.

In case anyone’s seeing this now, sass and css can be added easily using built in module support.

/components/topbar.module.scss

.wrapper {
  background: #000; 
  ...
}

/components/topbar.tsx

import styles from './topbar.module.scss';

export default function Topbar() {
  return (
    <div className={styles.wrapper}>
      ...
    </div>)
}

A global style sheet styles.scss can be placed at the root of the folder too.

styles.scss

html,
body {
  ...
}
2reactions
jerrygreencommented, Oct 15, 2019

May be useful for someone:

Currently, it’s almost impossible to make SCSS and SASS work in newer versions of Next. At least it’s unreasonably hard.

Instead of using scss or sass, I ended up using MaterialUI and its makeStyles helper, so styles are stored in a separate file with ts extension.

For those who don’t like the idea of using MaterialUI (I don’t like it either, honestly), then styled-jsx is the way. As recommended by official docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't import CSS/SCSS modules. TypeScript says "Cannot ...
I was using the typescript-plugin-css-modules plugin, was getting the error still on build, and this resolved it for me.
Read more >
How to use CSS Modules with TypeScript and webpack
You'll get an error: “Cannot find module './Button.css'”. There are several ways to fix that. The easy way. You can bypass TypeScript import...
Read more >
API - ESBuild
This API call is used by the command-line interface if no input files are provided and the --bundle flag is not present. In...
Read more >
Features | Vite
TypeScript #. Vite supports importing .ts files out of the box. Vite only performs transpilation on .ts files and does NOT perform ...
Read more >
FAQs - styled-components
styled-components v5 does not introduce any breaking public API changes, ... </React.Fragment>. See the documentation for createGlobalStyle to see all the ...
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