Skip to content

Incorrect module import #36

@esauter5

Description

@esauter5

I believe the module docs are incorrect. Based on this lib/math.js

export function sum(x, y) {
  return x + y;
}
export var pi = 3.141593;

The docs say we can import like this

import math from 'lib/math';
console.log('2π = ' + math.sum(math.pi, math.pi));

But I think this is incorrect. In this case math is undefined because there is no default export. The correct import would be:

import * as math from 'lib/math';

or if you wanted to just destructure:

import { pi, sum } from Math;

I made this mistake today when dealing with code that was mixed with requires and import. These two statements below look equivalent because they similar structure which I think leads to the confusion:

var math = require('lib/math');
import math from 'lib/math';

There's a great reference here: http://www.2ality.com/2014/09/es6-modules-final.html for anyone who happens to come across this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions