相关文章推荐
酒量小的椰子  ·  Attributes - C# ...·  3 月前    · 
无聊的槟榔  ·  Velocity教程 - ...·  7 月前    · 
闷骚的大熊猫  ·  JavaScript ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

With the following node.js code:

import { fileURLToPath } from 'node:url';
import path from 'node:path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));

I am seeing the following lint error relating to the import.meta.url reference:

This snippet is to replicate __filename and __dirname in ESM as per node.js guidance. The same error is given when using import.meta.url as follows... which is also in the official guidance:

import { readFileSync } from 'fs';
const buffer = readFileSync(new URL('./data.proto', import.meta.url));

I have looked at this but it doesn't solve my problem. This is specifically within the Cloud9 IDE... not the current AWS Cloud9 but a self-hosted Cloud9 based on this repo (last updated 4 years ago). The only guidance from (AWS) Cloud9 is on this page:

I can get certain basic rules to work using this .eslintrc file, e.g.

rules: { semi: ["error", "never"]

So I know that the config file is taking effect in the IDE. But can't see the appropriate rule to disable the "unexpected token import" error.

EDIT: the following seem relevant but I cannot determine if it has ever really reached a conclusion:

https://github.com/eslint/eslint/issues/12518

https://github.com/eslint/eslint/pull/13196

https://github.com/eslint/eslint/issues/13133

This is actually an educated guess, since I'm not using the Cloud9 IDE. If your .eslintrc file is being recognized, what you need to add there are the proper parser options, e.g.:

"parserOptions": { "ecmaVersion": 2020, "sourceType": "module" "rules": { "semi": ["error", "never"]

The reason is that the ESLint parser treats by default all JavaScript sources as ES5 scripts, while the import.meta syntax is only allowed in modules since ECMAScript 2020.

Update

The setting "ecmaVersion": 2020 is supported in ESLint 6 onwards. Another option to have import.meta recognized is using a custom parser like Babel. The packages to install for the Babel parser are @babel/core and @babel/eslint-parser. And the relevant settings in .eslintrc are here:

"parser": "@babel/eslint-parser", "parserOptions": { "ecmaVersion": 2020, "requireConfigFile": false

Note: I couldn't find out which versions of ESLint are supported by the Babel parser in the documentation in their repo (link above). I can only see in the code that ESLint 7 and 8 are supported. If the current version of Babel does not work with your version of ESLint, you may have to try installing older releases and see if they work. And in that case, an additional plugin like syntax-import-meta may be required.

Sadly this doesn't work. In fact, adding the parser options stops the semi rule from working. I notice that after hitting newline in the editor, an error indication pops up briefly in the margin and then disappears... if I hover over it before it vanishes I see that it says: Parsing error: Invalid ecmaVersion. Perhaps the ESLint parser baked into this version of Cloud9 (frozen for 4 years) just won't support new parser versions? – drmrbrewer Nov 18, 2021 at 7:56 If I understand the code correctly, the IDE uses ESLint 4 (current is 8). If you cannot update ESLint in the repo you could try the Babel parser, but I'm not sure if it still works with ESLint 4. – GOTO 0 Nov 18, 2021 at 8:19 I'll see if I can update the version of the eslint dependency. Before posting this question I did investigate using the Babel parser but couldn't figure it out because it seemed to require installing it as a dev dependency for a particular project, and as such it wouldn't be available as a parser option in this global .eslintrc file. – drmrbrewer Nov 18, 2021 at 8:30 OMG, that's creepy. That Cloud9 repo is archived but it has a lot of forks. If I had to work on it, I would start looking there in case someone already did the job of upgrading the dependencies, sometimes miracles happen. I'm sorry I could not help. Good look! – GOTO 0 Nov 18, 2021 at 18:22 That tool works well enough to narrow down the search, and it seems that there are no useful forks :-( Perhaps time to investigate other self-hosted options, e.g. vscode looks good: code.visualstudio.com/docs/nodejs/working-with-javascript – drmrbrewer Nov 18, 2021 at 19:24

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.