Importing From Your Own Files
Dev’s Python tool keeps its parsing in readings.py, so Asha can reuse it for the sports hall’s
boiler. digest.py, the file next to it, starts with one line that imports three functions from
it:
from readings import is_blank, parse_line, summarise
The school’s new dashboard is written in JavaScript, so the tool is moving too. Does that one line still work?
In Python, readings.py shares every top-level name without saying so. A JavaScript file shares
only what it marks with export. So far, only parseLine() has moved across to readings.js,
and its declaration starts with that word (its body is folded here, as in the video):
readings.js
export function parseLine(line) {
…
}
The package.json beside the files holds { "type": "module" }, which tells Node to treat every
.js file in the folder as a module.
The port goes one step at a time, so both digest files start small: they import the parser and parse Thursday’s 07:00 reading, the week’s peak. Here’s the Python:
from readings import parse_line
reading = parse_line("Thu 07:00,78.0")
print(reading)
It prints the reading as a dictionary:
{'time': 'Thu 07:00', 'temp': 78.0}
The JavaScript import line swaps Python’s two words. import comes first, then the names you
take, in braces. These braces aren’t an object: they’re part of the import line. It’s
parseLine, in camelCase, because that’s the name readings.js exports. Then comes from, and
the source as a string in quotes, naming where the code lives.
Watch out for Python’s order here: if the line starts with from, it stops with a
SyntaxError. Names come first, and the source comes last. With the other two lines ported
line for line, here’s the port:
digest.js
import { parseLine } from "readings";
const reading = parseLine("Thu 07:00,78.0");
console.log(reading);
Running it with node digest.js stops with an error, and the reading never prints. Node prints
its own lines around the error, so read the line that starts with Error. Here it is, trimmed to
that line, with the folder part of the path shortened:
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'readings' imported from .../boiler-digest/digest.js
As with a failed Python import, no line of digest.js ran. Here’s why:
- In JavaScript, a bare name such as
"readings"means a package: installed code, like what pip installs for Python, that you name without a path. So Node never looks for areadingsfile next todigest.js: it looks only among installed packages. - For most modules, Python’s import searches the script’s folder first, then the standard
library and installed packages, and takes the first match. That’s how a
json.pyof your own can hide the realjsonmodule. - In JavaScript, the spelling picks the kind: a package is a bare name, and your own file is always a path. So a file of yours can never hide a package.
A path to a file next to this one starts with ./, meaning next to digest.js itself, wherever
you run it from. With ./ added, the error changes to Cannot find module '.../readings'. It
says module, not package, so ./ worked: Node looks for a file called readings, with no
extension, and there isn’t one. The next line suggests the fix:
Did you mean to import "./readings.js"?
Node doesn’t fill in an extension the way Python’s import finds readings.py from the name
alone. Like a path in Python’s open(), an import path names exactly one file, though it starts
from digest.js, not the folder you run in. If an import you copy leaves the extension off, add the one the error’s
Did you mean line suggests. With ./ and the whole file name, the import finds readings.js:
digest.js
import { parseLine } from "./readings.js";
const reading = parseLine("Thu 07:00,78.0");
console.log(reading);
Run it with node digest.js, and the reading logs:
{ time: 'Thu 07:00', temp: 78 }
JavaScript prints 78 where Python prints 78.0: it uses one number type for whole numbers and
decimals, so it’s the same value.
That’s the JavaScript way. A bare name is a package. Your own file is a path: ./ for “next to
this file”, and the full file name, .js included. So the spelling alone shows which imports
are packages and which are your own files, each a path you can follow.
Next up: Choosing What a Module Shares, a text lesson. You’ll export the rest of
readings.js, and import several names in one pair of braces.