Esm: Unknown file extension error breaks scripts

Created on 7 Dec 2017  路  16Comments  路  Source: standard-things/esm

I ran into this because running nyc mocha with nyc requiring @std/esm caused @std/esm to throw on the ./node_modules/.bin/mocha script.

Following the instructions from the README about using @std/esm with nyc, running anything that installs as a node script without an extension seems like it's likely to run into this problem.

bug

All 16 comments

This is testing against master, I hope that's helpful and not noise.

This is helpful! I haven't shipped the UNKNOWN_EXTENSION bit yet. I still need to add tests.
It's more aligning with Node core stuff.

We'll need to update the doc tip for nyc and friends since the opt out is cjs.paths of true. Whenever CJS options are involved I just simplify for "esm":"cjs".

@tilgovi What @std/esm options are you using?

I get it with the "cjs" shorthand, with {"esm": "js", "cjs": true}, and with a more limited cjs configuration that includes "paths": true.

@tilgovi Could you create a simple repo to repro. I can take that and make it a scenario test.

Over here again: https://github.com/tilgovi/esm-babel-nyc-test

$ npm test

> [email protected] test /Users/randall/Desktop/esm-babel-nyc-test
> nyc mocha

/Users/randall/src/esm/index.js:4411
    throw new __WEBPACK_IMPORTED_MODULE_6__errors_js__["a" /* default */].Error("ERR_UNKNOWN_FILE_EXTENSION", foundPath);
    ^

Error [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension: /Users/randall/Desktop/esm-babel-nyc-test/node_modules/mocha/bin/mocha
   ...

Note that's after npm link @std/esm to a local, built copy of current master.

Works beautifully!

Thanks! I have some unit test debt to pay and I'd like to fix the other open issue before cutting a release. So I'm guessing another 2 days or so.

Mr esm noob again. Of course next was getting my tests running with esm.

FYI This got me by the no extension issue and using just release .18 and thus without those patches above. Hmmm.

"testw": "node -r @std/esm ./node_modules/.bin/_mocha ./test/*.mjs -r @std/esm  --reporter list --recursive --watch"

This assumes that one has changed your test files to esm imports and extensions to .mjs. PLUS "@std/esm": "cjs" in package.json

Note: mocha doesn't source .mjs files by default (file an issue with them?) so need to explicitly use a *.mjs.

Hope this helps someone. Maybe some mention in the tips. The double -r was tricky plus needing :cjs in package.json.

@dkebler Does it work with --compilers=mjs:@std/esm? IIRC that both requires @std/esm and adds *.mjs to mocha's glob.

will check on that but I did have to add --watch-extensions mjs. Maybe your suggestion will cover this as well. Will try and report back.

@dkebler The no extension issue shouldn't exist in released bits since the issue @tilgovi was mentioning was in our master branch (still unreleased).

You should just be able to use mocha -r @std/esm without the double require. If it's not working for you, then you should create a small repro repo and post a new issue so I can investigate.

ok that option worked like below without wildcard (watch works too)

"test": "node -r @std/esm ./node_modules/.bin/_mocha ./test --compilers=mjs:@std/esm --reporter list --recursive --watch",

BUT I noticed this.
(node:7787) DeprecationWarning: "--compilers" will be removed in a future version of Mocha; see https://git.io/vdcSr for more info

As to removing leading node -r @std/esm that doesn't work.
home/david/AllData/hacking/active-dev-repos/uci/uci-device/test/device.test.mjs:1 (function (exports, require, module, __filename, __dirname) { import chai from 'chai' ^^^^^^ SyntaxError: Unexpected token import

because I converted my test file to esm as well.

fyi using mocha 4+

BTW thanks for your efforts. This is the last piece of doing es6 without babel yea.

@dkebler While I appreciate your stream-of-dev comments it's much better if you just create a simple repro repo so I can more easily and quickly triage your issue.

https://github.com/uCOMmandIt/uci-device
npm run testw
for the last script above.

But as I said all is good for me for now even if I have to tell mocha (again) I've already asked for esm/.mjs

@dkebler If you use nyc, the instanbul cli (npm i --save-dev nyc), instead of istanbul directly you can make the following changes to your package.json.

  "scripts": {
    "test": "nyc mocha"
  },
  "nyc": {
    "extension": [".js", ".mjs"],
    "reporter": "lcovonly",
    "require": ["@std/esm"]
  }

I moved the mocha options to test/mocha.opts

-R spec
test/**/*{.js,.mjs}
Was this page helpful?
0 / 5 - 0 ratings