Appropriate Way to Include Packages in JS

I have a question about the appropriate way to write code that includes packages for consumption on things like GitHub and these forums.

For one of the portfolio projects (Mixed Messaged), I made a Shakespearian Insult Generator.

To get it to work I installed an npm package called prompt-sync that works with node in the terminal.

However, when I copied my code to the feedback forum and used a Code Byte, the code byte popped back an error because it didn’t have the npm package installed.

Is there a standard way I can include a package in my code when I share it on things like Github?

const prompt = require('prompt-sync')({sigint: false}); const name = prompt('What is your name? '); let arr1 = ['bawdy','brazen','churlish','distempered']; let arr2 = ['bunch-backed','clay-brained','dog-hearted','empty-hearted']; let arr3 = ['younker','whipster','waterfly','snipe']; let word1 = arr1[Math.floor(Math.random() * 4)]; let word2 = arr2[Math.floor(Math.random() * 4)]; let word3 = arr3[Math.floor(Math.random() * 4)]; console.log(`${name}, thou art a ${word1} ${word2} ${word3} and I don't care who knows it!`);

Thank you for any help or insight you can provide.

I don’t know about Codebyte. But there are sandboxes (like Stackblitz) where you can install dependencies.
If I understand correctly, you don’t want to provide your own npm package but you just want to share your code and want it to run either in a sandbox online or on the computer of someone else?

If you developed the code in Node and installed a package, that package will be included as dependencies or devDependencies in the package.json in your project directory.
You can upload your whole project folder including your package.json on Github. Then anyone can download it, run npm install and all dependencies in the package.json will be installed.
Or you search for a sandbox that allows you to install packages.

1 Like