Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

379
Visualizações
How to run node.js cli with experimental-specifier-resolution=node?

Our team has built a small CLI used for maintenance. The package.json specifies a path for with the bin property, and everything works great; "bin": { "eddy": "./dist/src/cli/entry.js"}

Autocompletion is achived by using yargs@17.0.1. However we recently converted the project to use es6 modules, because of a migration to Sveltekit, i.e. the package.json now contains type: module. Because of this, the CLI now only works if we run with:

what works

node --experimental-specifier-resolution=node ./dist/src/cli/entry.js help

However, if we run this without the flag, we get an error "module not found":

Error [ERR_MODULE_NOT_FOUND]: Cannot find module...

So the question is

Can we somehow "always" add the experimental-specifier-resolution=node to the CLI - so we can continue to use the shorthand eddy, and utilize auto completion?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

There are two probable solutions here.

Solution 1

Your entry.js file should start with a shebang like #!/usr/bin/env node. You cannot specify the flag directly here, however, if you could provide the absolute path to node directly in the shebang, you can specify the flag.

Assuming you have node installed in /usr/bin/node, you can write the shebang in entry.js like:

#!/usr/bin/node --experimental-specifier-resolution=node

(Use which node to find the absolute path)

However, this is not a very portable solution. You cannot always assume everyone has node installed in the same path. Also some may use nvm to manage versions and can have multiple version in different path. This is the reason why we use /usr/bin/env to find the required node installation in the first place. This leads to the second solution.

Solution 2

You can create a shell script that would intern call the cli entry point with the required flags. This shell script can be specified in the package.json bin section.

The shell script (entry.sh) should look like:

#!/usr/bin/env bash
/usr/bin/env node --experimental-specifier-resolution=node ./entry.js "$@"

Then, in your package.json, replace bin with:

"bin": { "eddy": "./dist/src/cli/entry.sh"}

So when you run eddy, it will run the entry.js using node with the required flag. The "$@" in the command will be replaced by any arguments that you pass to eddy.

So eddy help will translate to /usr/bin/env node --experimental-specifier-resolution=node ./entry.js help

over 4 years ago · Santiago Trujillo Relatório

0

Just add a script to your package.json:
Assuming index.js is your entry point and package.json is in the same directory

{
  "scripts": {
    "start": "node --experimental-specifier-resolution=node index.js"
  }
}

Then you can just run on your console:

npm start
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda