Cli: [BUG] Experimental modules within "bin" script

Created on 10 Aug 2020  路  4Comments  路  Source: npm/cli

Current Behavior:

When writting this "bin" script where local package is "module":

#!/usr/bin/env node
// code

I get this stacktrace:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for C:/myproject/bin/script
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
    at Loader.getFormat (internal/modules/esm/loader.js:113:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:244:31)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Loader.import (internal/modules/esm/loader.js:178:17) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Expected Behavior:

The code after // code to run.

Steps To Reproduce:

  • npm init -y
  • Let type = "module" in package.json.
  • Create a bin/script file in the project and add it to package.json.
  • Environment:

  • OS: Windows 10
  • Node: 12.18.1
  • NPM: 6.14.5
  • Bug Needs Triage Release 6.x

    All 4 comments

    I already tried these hacks sometime ago. Tried again and these don't work.

    https://stackoverflow.com/questions/48179714/how-can-an-es6-module-be-run-as-a-script-in-node

    I believe extensionless files do not currently have a way to be native ESM code.

    if you want to kick off an ESM file you are going to want to use dynamic import not require. require is undefined in an ESM file and you cannot require an ESM file (only import).

    Oh, actually { bin: "bin/cli.mjs" } is working. Because of tsc from TypeScript I used to thought you always had to use an extensionless file.

    Was this page helpful?
    0 / 5 - 0 ratings