Building with Packages
Learning Outcome Guide
Section titled “Learning Outcome Guide”At the end of this class, you should be able to…
- Set up a basic front-end project using npm.
- Import and use npm packages in front-end applications.
- Create simple interactive web pages using JavaScript and npm packages.
- Explain and apply ES Module syntax (import and export) for structuring JavaScript and using packages.
Coding Demo
Section titled “Coding Demo”In-Class Demo Jan 2026 term
To run the coding demo, you need to have your Student Workbook open in Visual Studio Code.
-
Open the terminal window and paste in the following.
Run from the root of your repository pnpm dlx tiged --disable-cache --force DG-InClass/SDEV-1150-A04-Jan-2026/sk/lesson-14 ./src/lesson-14 -
Walk through the steps in the
ReadMe.mdof the new lesson.
Install dependencies and run the dev server
Section titled “Install dependencies and run the dev server”- Extract the starter zip and rename the folder to
lesson-14 - Move into the lesson-14/ directory:
cd lesson-14- Install the necessary dependencies:
npm installor
npm i- Run the dev server with the
devscript:
npm run dev- Open the provided development server URL in your browser
- You should see the default render for the vite project.
- Use this as the base for today’s examples.
Instructor Demo
Section titled “Instructor Demo”Using packages walkthrough
Section titled “Using packages walkthrough”- Install Day.js for date formatting as a normal dependency:
npm i dayjs- Import the dayjs library into
main.js:
import dayjs from 'dayjs';
const currentDate = dayjs().format('dddd, MMMM D, YYYY');document.querySelector('#today').textContent = `Today is ${currentDate}`;Run your project and see the results.
User-defined imports
Section titled “User-defined imports”-
Create a second JavaScript file in the
src/directory namedutils.js. NOTE: we won’t be adding a script tag to the index.html file, we will use ES Modules to connect our scripts -
Create a
greetUserfunction in theutils.jsfile:utils.js function greetUser(name) {return `Welcome to the app, ${name}!`;} -
Finally, to make the function available in other scripts, export it with the
exportkeyword:utils.js export function greetUser(name) {return `Welcome to the app, ${name}!`;}NOTE: We can also create named exports using the object notation
export { greetUser }; -
We can also define a default export usign the
defaultkeyword. Add a default export to theutils.jsfile:utils.js export default { defaultName: 'Jane Doe' }; -
Import the default from
utils.jsand use the defaultName in themain.jsscript:main.js import dayjs from 'dayjs';import { greetUser } from './utils.js';import utils from './utils.js'; // can be any name you likeconst user = prompt('Enter your name:');const message = greetUser(user || utils.defaultName); // use default if no name enteredconst currentDate = dayjs().format('dddd, MMMM D, YYYY');document.querySelector('#greeting').textContent = message;document.querySelector('#today').textContent = `Today is ${currentDate}`;
Student Exercise
Section titled “Student Exercise”Use what you have learned to use the AnimeJS library to animate the greeting element.
- Install the
animejslibrary as a normal dependency - Import the named
animateanimejs function intomain.js - Add the following to your
main.jsscript to animate the greeting:animate('#greeting', {translateY: [-20, 0],opacity: [0, 1],duration: 1000,easing: 'easeOutQuad',});
If all of the above have been done correctly, you should see the greeting heading fade slightly down and into view.
Push to your GitHub workbook repo
Section titled “Push to your GitHub workbook repo”Once you’re done making your own custom updates to the project, stage your files, commit your work, and push to the remote repository.
- Open a terminal in VS Code
- Stage all updated and created files:
git add .- Commit the changes:
git commit -m 'Lesson 14 Example'- Push your changes to the remote workbook repository:
git push origin main