16

I am unable to import scss file in below format.

import * as styles from './index.scss';

getting below error :

can not find module './index.scss'

I want to use class name like below :

className={styles.anyClassName}
1

5 Answers 5

32

In order to import SCSS as object in TS like this :

import * as styles from './index.scss';
  • Create file 'global.d.ts' in your source directory (or outside, but must be included in the src directory defined in tsconfig)
  • Add the following code inside
declare module '*.scss' {
  const content: {[className: string]: string};
  export = content;
}

https://basarat.gitbooks.io/typescript/docs/types/ambient/d.ts.html

Sign up to request clarification or add additional context in comments.

1 Comment

Did not working for me!
3

I came across this question recently, and figured it might be useful to add an answer that helps do this at the time the project is generated (using create-react-app) and without the need to eject or modify the webpack configs:

create-react-app my-app --scripts-version=react-scripts-scss-ts

Comments

2

Be sure you have react-scripts-scss-ts installed.

If not run:

 npm i react-scripts-scss-ts --save

Comments

-1

One of this way you work with .scss style on your create-react-app project. you need some steps on your projects.

  1. git commit all changes
  2. run yarn run eject from your project command prompt.
  3. Go to the location and open the file config/webpack.config.dev.js approximately line #173. Replace test: /\.css$/, with test: /\.s?css$/,
  4. Go to the location and open the file config/webpack.config.prod.js approximately line #185. Replace test: /\.css$/, with test: /\.s?css$/,
  5. add your .scss file on your component like

    import './App.scss';
    

    and uses like

    <div className="your-styled-class-name">
        <p>Styed Text</p>
    </div>
    

That's it.

6 Comments

Hi, I want to import as import * as styles from 'xyz.css'. That is not working.
if using create-react-app command then follow this answer. stackoverflow.com/questions/49856468/…
Yes i am using create-react-project with typescript. The above approach also not works. Please check git repo: github.com/mdarif-k/React-Typescript1.git
hi. I have updated my answer. this time you create a fresh project then try this way. I have checked it.
Ejecting the app seems like it should be a solution of last resort, as it introduces a bunch of configuration and infrastructure that's probably best left under the hood into your codebase. Flexibility can be nice, but clarity is often nicer, and configuring webpack can turn into a pretty deep rabbit hole. Just my two cents.
|
-1

I solved that issue using Run Powershell as Administrator and execute that code: Set-ExecutionPolicy RemoteSigned Press A And Enter

Refer this if not clear: After npm updating packages .ps1 cannot be loaded because running scripts is disabled on this system

and refer this alsp: No such file or directory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.