Create and publish an NPM package (using NX)

Create and publish an NPM package (using NX)

Using "NX" we are going to set up a basic project with a small REACT library that you can deploy to npm.

Nx is a smart, fast, and extensible build system with first-class monorepo support and powerful integrations. (from NX docs)

(note: nx version 15.3, react 18.2.0)

The first step is to run the basic mono repo configuration of an NX project, running the following command:

  • npx create-nx-workspace@latest

Selecting the following options:

No alt text provided for this image

After the installation of the packages, you can enter the folder of the new project (I'm going to use vs code) :

  • cd ./my-library
  • code .

No alt text provided for this image

You can run the basic project with:

  • npm run start / yarn start

No alt text provided for this image

Then in your browser, you will see something like this

No alt text provided for this image

We are ready to create our library, NX It makes it easy for us to create with a simple command (or its extension for vs code)

No alt text provided for this image
No alt text provided for this image

  • npx nx generate @nrwl/react:library awesomeLibrary --buildable --bundler=rollup --importPath=@awesomeOrg/awesomeLibrary --publishable --tags='test lib' --unitTestRunner=vitest --no-interactive?

Why do this flags?

  • buildable: generates a buildable library (currently deprecated)
  • bundle: the bundler to use for generating the library (in this case "rollup" for a publishable library)
  • importPath: the name for importing the library in the react project
  • publishable: setup basic config for the publishing of the library
  • tags: npm tags for reference
  • unitTestRunner: Test runner to use for unit tests. (vitest in this case, you can choose jest)
  • no-interactive: run the command in non-interactive mode

You can find the generated library in this path "my-library/libs/awesome-library"

No alt text provided for this image

(the main code is inside the src/lib/awesome-library.tsx file)

For testing purposes, we are going to import this library into our react app?

No alt text provided for this image

(the import is the same that we use in the importPath flag)

Run the start command you will see the new library working as a normal react component with the text "Welcome to AwesomeLibrary!"

No alt text provided for this image

We are ready to build and publish this library to npm

(you will need to have an npm account and follow the npm documentation to init session, in this case, I'm using a free account that has unlimited?Public?Packages)

Run the command and follow the instructions:

  • npm login

No alt text provided for this image

add and organization

No alt text provided for this image

After that add this extra configuration in the library, is needed for CJS support:

in the libs\awesome-library\project.json file, add the following options to the build:

-"skipTypeField": true,

-"format": ["esm", "cjs"],

No alt text provided for this image

(note this configuration is optional)

Run the command:

  • ?yarn nx build awesome-library

It will build the library in the dist folder ready to be published

(if you need to modify the organization and version change this before building the library in the package.json file)

No alt text provided for this image

Run the command:

  • npm publish --new-version

(make sure that you are in the dist\libs\awesome-library folder, if you have a problem with the command, set the configuration of npm to public with "npm config set access public")

No alt text provided for this image

We can see the library in the npm registry (as a public package)

No alt text provided for this image


That's it, NX makes it really easy to create libraries! you can install it in any react project.

要查看或添加评论,请登录

Yesid Gonzalez的更多文章

  • What problems do UX designers solve?

    What problems do UX designers solve?

    User experience is a broad discipline because you must have skills in many different fields or at least know a little…

  • WEB Postmortem

    WEB Postmortem

    A postmortem is a process intended to help you learn from past incidents. It typically involves analysis or discussion…

  • Pow recursion explained

    Pow recursion explained

  • What Happens When You Type in a URL in your browser

    What Happens When You Type in a URL in your browser

    Let's say that you are sitting at your computer, surfing the Web, and you get a call from a friend who says, "check…

  • Do you need the Internet Of Things?

    Do you need the Internet Of Things?

    Maybe you hear about the terms "internet of things" and "artificial intelligence" and you think that is something magic…

  • Basics of Machine Learning

    Basics of Machine Learning

    In these days where the world is interconnected, maybe you hear some terms like Artificial Intelligence Machine…

  • Everything is an object!

    Everything is an object!

    Object-oriented programming refers to a type of computer programming software design in which programmers define not…

  • How object and class attributes work.

    How object and class attributes work.

    What’s a class attribute A class attribute is a Python variable that belongs to a class rather than a particular…

  • Dynamic Libraries C

    Dynamic Libraries C

    Library functions in C language are inbuilt functions which are grouped together and placed in a common place called…

  • How things are stored in computer memory?

    How things are stored in computer memory?

    Computer data storage is a complex subject, but it can be broken down into three basic processes. First, data is…

社区洞察

其他会员也浏览了