question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

adding `"sourceType": "module"` flags dynamic imports.

See original GitHub issue

Tell us about your environment

  • ESLint Version: 4.14
  • Node Version: 9.3
  • npm Version: 5.6

What parser (default, Babel-ESLint, etc.) are you using? default

Please show your full configuration:

module.exports = {
  "env": {
    "es6": true,
    "node": true,
    "mocha": true
  },
  "parserOptions": {
    "ecmaVersion": 2017,
    "sourceType": "module"
  },
  "extends": "eslint:recommended",
  "rules": {
    "indent": [
      "error",
      2
    ],
    "no-console": 0,
    "semi": ["error", "never"],
    "linebreak-style": [
      "error",
      "unix"
    ],
    "quotes": [
      "error",
      "single"
    ]
  }
}

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

added "sourceType": "module" to stop flagging import.
That worked fine but…

What did you expect to happen? not flag dynamic import line await import

What actually happened? Please include the actual, raw output from ESLint. image

Parsing error: Unexpected token import (Fatal)

Note: if I remove the sourceType: module then this stops flagging but of course then the standard import lines do get flagged.

run with atom linter-eslint 8.4 plugin and @std/esm by @jdalton which enables esm support including dynamic imports.

I understand this is all “new stuff” but come node 10 esm will be native (without using @std/esm) and I assume will include dynamic imports.

import path from 'path';
import fs from 'fs';
// import { promisfy as pify } from 'util';
import deepset from 'lodash.set';
import walk from 'walk-filtered';

// const pStat = pify(fs.lstat);

const EXTENSIONS = ['.mjs'];

// rename to file name or remove default in a module
function reDefault(obj, fpath) {
  const newObj = {};
  Object.keys(obj).forEach((key) => {
    if (key !== 'default') newObj[key] = obj[key];
    else if (arguments.length === 2) newObj[path.parse(fpath).name] = obj.default;
  });
  return newObj;
}

async function importFile(directory, relativePath, options, results) {
  let module = await import(path.join(directory, relativePath));

  if (options.default === 'none') module = reDefault(module)
  else module = reDefault(module, relativePath)

  // merge result
  if (options.collect === 'mergeAll') Object.assign(results, module);
  else {
    const dirs = path.parse(relativePath).dir;
    if (dirs.length) deepset(results, dirs.replace(/\//g, '.'), module);
    else Object.assign(results, module);
  }
} // end importFile

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jdaltoncommented, Jan 10, 2018

I mean it’s ESLint’s policy too (there’s acorn plugins). It’s a real bummer it’s shrugs all the way down.

0reactions
ljharbcommented, Jan 10, 2018

In other words, import() is not a thing that exists in the language yet (at stage 4, according to acorn’s unfortunate official policy), so it can’t ignore it because it doesn’t understand it in the first place.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic Imports for Code Splitting cause: ESLint Parsing ...
I've followed the steps provided in Webpack's Dynamic import section, as well as Anthony ... Adding `parser: 'babel-eslint', solved my issue.
Read more >
JavaScript modules - MDN Web Docs
This guide gives you all you need to get started with JavaScript module syntax.
Read more >
Dynamic imports - The Modern JavaScript Tutorial
First, we can't dynamically generate any parameters of import . The module path must be a primitive string, can't be a function call....
Read more >
TSConfig Reference - Docs on every TSConfig option
Specifies an allowlist of files to include in the program. ... Without this flag, using an export from a UMD module requires an...
Read more >
dynamic import can only be supported when transforming es ...
Dynamic imports are only supported when the 'module' flag is set to ... ... The module option sets the module system for the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found