In this lesson you will
- Export and import functions and values between your own files
- Create package.json and set the project to ES modules
- Install a package with npm and know what package-lock.json is for
- Run tasks with npm scripts and read common module errors
Your own modules and package.json
Every Node.js project starts with a package.json, a small JSON file that names the project and lists its scripts and packages. npm init -y writes one with default answers. Then put related code in its own file and mark what other files may use with export. Here vat.js exports a table of Gulf VAT rates and an addVat function, and index.js imports both by name. Anything a module does not export stays private to that file, which keeps helpers from leaking into the rest of the program.
The first run worked but printed a warning. Node.js has two module systems: the older CommonJS with require and module.exports, and standard ES modules with import and export, the same syntax as in the browser. A .js file is CommonJS unless package.json says otherwise. Current versions notice the import syntax, parse the file again as an ES module and warn that this costs time. The warning itself names the fix: add "type": "module". We ran npm pkg set type=module, and the second run was clean.
export const VAT_RATES = { AE: 5, SA: 15, OM: 5, BH: 10 };
export function addVat(amount, country) {
const rate = VAT_RATES[country];
if (rate === undefined) throw new Error(`No VAT rate for ${country}`);
return Math.round(amount * (100 + rate)) / 100;
}import { addVat, VAT_RATES } from "./vat.js";
console.log(Object.keys(VAT_RATES));
console.log("200 AED with UAE VAT:", addVat(200, "AE"));
console.log("200 SAR with Saudi VAT:", addVat(200, "SA"));Wrote to /home/you/souq-tools/package.json:
{
"name": "souq-tools",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}[ 'AE', 'SA', 'OM', 'BH' ]
200 AED with UAE VAT: 210
200 SAR with Saudi VAT: 230
(node:335) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/you/souq-tools/index.js is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to /home/you/souq-tools/package.json.
(Use `node --trace-warnings ...` to show where the warning was created)[ 'AE', 'SA', 'OM', 'BH' ]
200 AED with UAE VAT: 210
200 SAR with Saudi VAT: 230We replaced our folder path with /home/you. The VAT rates are the standard rates in force in November 2026; check the tax authority before using them in real invoices.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Should I use require or import in Node.js?
Use import for new code. ES modules are the JavaScript standard, work in browsers too and support top level await. Learn to read require, because many existing projects and older answers online still use CommonJS.
What is the difference between npm install and npm ci?
npm install installs and may update package-lock.json. npm ci deletes node_modules and installs exactly what the lock file says, failing if it does not match package.json, which is what servers and CI should use.
What does the caret ^ mean in a package version?
^1.6.9 accepts 1.6.9 and any later version with the same major number, such as 1.7.0, but not 2.0.0. A tilde, ~1.6.9, accepts only patch updates such as 1.6.10.
What is npx used for?
npx runs a package command without installing it globally, such as npx create-next-app@latest. It uses the copy in your project if there is one and otherwise downloads it to a cache, so check the package name carefully before you press Enter.
Are npm packages safe to install?
Most are, but a package runs with your permissions, and attackers publish look alike names. Prefer well known packages, read the name twice, keep dependencies few, and run npm audit to see known vulnerabilities.
Sources
Facts in this lesson were checked against these official pages on 27 November 2026.
- Node.js docs: ECMAScript modules
- Node.js docs: Modules, packages and "type"
- npm docs: package.json
- npm docs: npm install
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.