Welcome to the Linux Foundation Forum!

The correct usage of integrate mode of create-fastify

krave
krave Posts: 58
edited February 2022 in LFW212 Class Forum

Hi,
I am reading Chapter 5 - Conventions section.
It mentions the integrate mode of lib create-fastify.
But the usage in the tutorial is different from the one listed on its repo

From tutorial:
npm init fastify -- --integrate

From README.md of create-fastify repo:
npm init fastify --integrate

What are the differences of these two usages? Or are they the same at all?

Comments

  • AlexDumitru
    AlexDumitru Posts: 3
    edited March 2022

    Hi @krave,

    I found your question quite interesting so I dug a little deeper.
    On the surface it looks quite straight forward. It's wrongly stated in the README.md repo. Simply because --integrate will be passed to npm instead of create-fastify.
    Also, the double-hyphen character is telling npm to stop parsing command line options and switches, so npm init fastify -- --integrate actually means:

    npm init fastify => npm exec create-fastify => fastify generate . (this is the fastify-cli npm module).

    The standalone -- is telling npm to stop parsing whatever it is after it so --integrate gets passed to the command above, which means it's like running:
    fastify generate . --integrate

    You can easily double check this by doing:

    mkdir new; cd new
    npm init fastify --integrate
    npm init fastify --integrate // second time you will get a warning, saying package.json already exists (but why?... i'm using --integrate)
    npm init fastify -- --integrate // works fine
    rm -rf *
    npm init fastify --integrator // bogus param, but no error ...what's up with that
    

    What puzzled me was why npm is ignoring unknown cli arguments, and while my google-fu didn't yield a recent topic on this, it looks like it has been since forever. Probably I should be ashamed I didn't knew this, but oh well, we live and learn. There are multiple closed git issues and RFCs around this topic in npm's repos, so feel free to dive into those if you want an explanation around this topic.

    I hope this sheds some light into what are the differences between the two. It most certainly helped me understand it better and learnt something new in the process.

  • krave
    krave Posts: 58

    Hi @AlexDumitru

    Thank you for your reply and the research.

    What puzzled me was why npm is ignoring unknown cli arguments

    It turns out that the standalone -- is a standard command usage across all UNIX commands not only for npm.

    If we run npm init fastify --integrator, from the perspective of npm program, there is a variable $1 assigned as 'integrator'. If the implemention of npm does not use $1 in its code, it will always ignore it no matter what you type in the command line.

    And I also found this isssue

    It looks like the implemention of npm v6 will check $1 and npm v7 will ignore it.

    So, I think the README.md is just obsolete rather than completely wrong.

  • dilipagheda
    dilipagheda Posts: 20

    if you already have an existing package.json and want to include fastify. these are the steps.

    • npm init fastify --integrate
    • npm install

    Done.

  • xdxmxc
    xdxmxc Posts: 157

    the problem is it depends on the version of npm, - this has been changed, changed back and changed back again based on npm changes between version - the content is now synced with the npm version that's installed alongside Node 16

Categories

Upcoming Training