Where to Put JavaScript and CSS in a SharePoint Framework Web Part
Question details
The user wants to know the proper architectural locations for adding JavaScript/TypeScript logic and CSS styling when building a custom component in a SharePoint Framework web part.

- Product
- Microsoft SharePoint Framework (SPFx)
- Device & OS
- not provided
- Scenario
- Building a custom user interface component, such as a nested accordion, in a SharePoint Framework project.
- Observed behavior
- Requires clarity on where to properly place the scripting logic and stylesheets within the scaffolded SPFx project structure.
Ensure you have Node.js installed, your SharePoint Framework (SPFx) project is successfully scaffolded, and you have access to a code editor like Visual Studio Code.
Structuring TypeScript Logic and SCSS in SPFx
Follow the standard SPFx architecture by separating TypeScript logic into the web part class and scoping styles into the SCSS module.
SharePoint Framework encourages using TypeScript instead of vanilla JavaScript, and SCSS modules instead of standard CSS. This ensures better maintainability, scoped styling, and strict typing for your web parts.
Open your main '.ts' or '.tsx' file (e.g., '[WebPartName]WebPart.ts'). Write your JavaScript or TypeScript logic inside the web part class and its corresponding event-handling methods.
Locate the associated '.module.scss' file in your web part's folder. Write all your CSS/SCSS styles here. The '.module.scss' extension ensures that your class names are scoped locally to prevent styling conflicts across SharePoint.
In your 'render()' method or your associated React components, import the SCSS styles and update the HTML or JSX templates to apply your custom classes and trigger your functions.

Simplify Your Document Workflows with WPS Office
While SharePoint handles complex intranet developments, WPS Office provides a lightweight, seamless alternative for your everyday document, spreadsheet, and presentation needs. Enjoy full compatibility with Microsoft Office formats without the hefty subscription fees.
- 1. Download the Installer: Visit the official WPS Office website and download the free installation package for your operating system.
- 2. Install the Software: Run the downloaded installer and follow the simple on-screen instructions to set up WPS Office on your computer.
- 3. Open and Edit Documents: Launch WPS Office and instantly open your existing Microsoft Office files without losing any formatting or layout.

Frequently Asked Questions
Can I use plain JavaScript and CSS instead of TypeScript and SCSS in SPFx?
While it is technically possible to include plain JavaScript and CSS, the SharePoint Framework is heavily optimized for TypeScript and SCSS. Using the default .ts/.tsx and .module.scss files is highly recommended for better maintainability and scoped styling.
How do I reference my SCSS classes in a React-based SPFx web part?
Import your SCSS module at the top of your React component file (e.g., `import styles from './YourWebPart.module.scss';`), then apply the styles to your JSX elements using the syntax `className={styles.yourClassName}`.
Where should I put external JavaScript libraries in my SPFx project?
External libraries should generally be installed via npm and imported directly into your TypeScript files. If you are using a non-module script, you can declare it in the `config/config.json` file under the `externals` configuration section.




