Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

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) {
  …
}

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 Dev’s Python tool keeps its parsing in readings.py, so Asha 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:40 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.

01:00 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:35 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:57 It stops with an error, and the reading never prints.

02:02 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:13 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:28 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:51 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:17 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.