Loading video player…

Importing From Your Own Files

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:

Language: Python
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):

Language: JavaScript Filename: 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:

Language: Python
from readings import parse_line

reading = parse_line("Thu 07:00,78.0")
print(reading)

It prints the reading as a dictionary:

Language: Program Output
{'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:

Language: JavaScript Filename: 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:

Language: Program Output
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 a readings file next to digest.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.py of your own can hide the real json module.
  • 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:

Language: JavaScript Filename: 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:

Language: Program Output
{ 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.

00:00 Dev’s Python tool keeps its parsing in readings.py, so Asha, who looks after the sports hall boiler, can reuse it. digest.py, next to it, imports three functions from it in one line.

00:14 The school’s new dashboard is JavaScript, so the tool is moving too. Does that one line still work? In this lesson, you’ll import from your own JavaScript file.

00:27 readings.py shares every top-level name without saying so. A JavaScript file shares only what it exports, and so far only parseLine() has moved across.

00:39 package.json sets the type to module, so Node treats these files as modules.

00:47 One step at a time: both digest files import the parser and parse Thursday morning’s reading, the week’s peak. Run it in Python, and it prints that reading as a dictionary.

00:59 In JavaScript, the import line swaps Python’s two words. Import comes first, then the name in braces. They’re not an object: they hold what you take. It’s parseLine, in camel case, as readings.js exports it.

01:17 Then from, and the source in quotes: a string naming where the code lives. Watch out for Python’s order here: if the line starts with the word from, it stops with a syntax error.

01:30 Names first, source last.

01:34 The other two lines port line for line, with console.log() in place of print() and const for the reading. Python found its readings file from the bare name alone.

01:45 What happens when you run the JavaScript version? Your turn: pause the video and predict.

01:56 It stops with an error, and the reading never prints.

02:01 Node can’t find a package called readings. Node prints its own lines around it, so read the one that starts with the word error: that’s all the card keeps.

02:12 As with a failed Python import, nothing ran. In JavaScript, a bare name like readings means a package: installed code, like what pip installs for Python, named without a path.

02:27 So Node never looks for a readings file next to digest.js: it looks only among installed packages.

02:36 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.py of your own can hide the real json module.

02:50 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.

03:04 Add ./: a path to a file next to this one starts with it. That’s next to digest.js itself, wherever you run it from. Run it again, and the error changes.

03:18 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, with the extension added.

03:33 Add .js, and the path names the whole file. Node doesn’t fill in an extension the way Python’s import finds readings.py from the name alone.

03:45 Like a path in Python’s open, an import path names exactly one file, though it starts from this file. If an import you copy leaves the extension off, add the one the hint suggests.

03:59 Run it once more, and the reading logs: Thursday morning, 78 degrees. JavaScript prints 78 where Python prints 78.0: one number type covers whole numbers and decimals, so it’s the same value.

04:16 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. So the spelling alone shows which imports are packages and which are your own files, each a path you can follow.

04:36 Next up is 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.

Become a Member to join the conversation.