Skip to content

Node.js Modules and npm: import, export, package.json, Installing Packages and Scripts

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

One file is fine for a ten line script. Real programs split code into modules: a file for VAT rules, one for prices, one for the server. Each file exports what others may use and imports what it needs. On top of your own modules sit packages written by other people, which you install with npm, the package manager that comes with Node.js. This lesson builds a small souq-tools project step by step, shows the warning Node.js printed before we told it the project uses ES modules, the error a missing file extension causes, and what npm install changed on disk. Recorded with Node.js 22.23.4 and npm 10.9.

Quick answer

In Node.js, export values from a file with export and load them with import { name } from "./file.js", always including the .js extension for your own files. Run npm init -y to create package.json, and add "type": "module" so .js files are treated as ES modules. Install packages with npm install name, which adds them to dependencies, node_modules and package-lock.json. Built in modules use the node: prefix, such as node:path.

35 min read and practice · Content checked on 27 November 2026

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.

Example: Two modules and a package.json
vat.js
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;
}
index.js
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"));
Real output: terminal, npm init -y (in a new folder souq-tools)
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"
}
Real output: terminal, node index.js (before "type": "module")
[ '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)
Real output: terminal, npm pkg set type=module, then node index.js
[ 'AE', 'SA', 'OM', 'BH' ]
200 AED with UAE VAT: 210
200 SAR with Saudi VAT: 230

We 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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.