Giter Club home page Giter Club logo

ni's Introduction

ni

npm i in a yarn project, again? F**k!

ni - use the right package manager


npm i -g @antfu/ni

npm · yarn · pnpm · bun


ni - install

ni

# npm install
# yarn install
# pnpm install
# bun install
ni vite

# npm i vite
# yarn add vite
# pnpm add vite
# bun add vite
ni @types/node -D

# npm i @types/node -D
# yarn add @types/node -D
# pnpm add -D @types/node
# bun add -d @types/node
ni --frozen

# npm ci
# yarn install --frozen-lockfile (Yarn 1)
# yarn install --immutable (Yarn Berry)
# pnpm install --frozen-lockfile
# bun install --no-save
ni -g eslint

# npm i -g eslint
# yarn global add eslint (Yarn 1)
# pnpm add -g eslint
# bun add -g eslint

# this uses default agent, regardless your current working directory

nr - run

nr dev --port=3000

# npm run dev -- --port=3000
# yarn run dev --port=3000
# pnpm run dev --port=3000
# bun run dev --port=3000
nr

# interactively select the script to run
# supports https://www.npmjs.com/package/npm-scripts-info convention
nr -

# rerun the last command

nlx - download & execute

nlx vitest

# npx vitest
# yarn dlx vitest
# pnpm dlx vitest
# bunx vitest

nu - upgrade

nu

# npm upgrade
# yarn upgrade (Yarn 1)
# yarn up (Yarn Berry)
# pnpm update
# bun update
nu -i

# (not available for npm & bun)
# yarn upgrade-interactive (Yarn 1)
# yarn up -i (Yarn Berry)
# pnpm update -i

nun - uninstall

nun webpack

# npm uninstall webpack
# yarn remove webpack
# pnpm remove webpack
# bun remove webpack
nun -g silent

# npm uninstall -g silent
# yarn global remove silent
# pnpm remove -g silent
# bun remove -g silent

nci - clean install

nci

# npm ci
# yarn install --frozen-lockfile
# pnpm install --frozen-lockfile
# bun install --no-save

if the corresponding node manager is not present, this command will install it globally along the way.


na - agent alias

na

# npm
# yarn
# pnpm
# bun
na run foo

# npm run foo
# yarn run foo
# pnpm run foo
# bun run foo

Global Flags

# ?               | Print the command execution depends on the agent
ni vite ?

# -C              | Change directory before running the command
ni -C packages/foo vite
nr -C playground dev

# -v, --version   | Show version number
ni -v

# -h, --help      | Show help
ni -h

Config

; ~/.nirc

; fallback when no lock found
defaultAgent=npm # default "prompt"

; for global installs
globalAgent=npm
# ~/.bashrc

# custom configuration file path
export NI_CONFIG_FILE="$HOME/.config/ni/nirc"
# for Windows

# custom configuration file path in PowerShell accessible within the `$profile` path
$Env:NI_CONFIG_FILE = 'C:\to\your\config\location'

Integrations

asdf

You can also install ni via the 3rd-party asdf-plugin maintained by CanRau

# first add the plugin
asdf plugin add ni https://github.com/CanRau/asdf-ni.git

# then install the latest version
asdf install ni latest

# and make it globally available
asdf global ni latest

How?

ni assumes that you work with lockfiles (and you should)

Before it runs, it will detect your yarn.lock / pnpm-lock.yaml / package-lock.json / bun.lockb to know current package manager (or packageManager field in your packages.json if specified), and runs the corresponding commands.

Trouble shooting

Conflicts with PowerShell

PowerShell comes with a built-in alias ni for the New-Item cmdlet. To remove the alias in your current PowerShell session in favor of this package, use the following command:

'Remove-Item Alias:ni -Force -ErrorAction Ignore'

If you want to persist the changes, you can add them to your PowerShell profile. The profile path is accessible within the $profile variable. The ps1 profile file can normally be found at

  • PowerShell 5 (Windows PowerShell): C:\Users\USERNAME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
  • PowerShell 7: C:\Users\USERNAME\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
  • VSCode: C:\Users\USERNAME\Documents\PowerShell\Microsoft.VSCode_profile.ps1

You can use the following script to remove the alias at shell start by adding the above command to your profile:

if (-not (Test-Path $profile)) {
  New-Item -ItemType File -Path (Split-Path $profile) -Force -Name (Split-Path $profile -Leaf)
}

$profileEntry = 'Remove-Item Alias:ni -Force -ErrorAction Ignore'
$profileContent = Get-Content $profile
if ($profileContent -notcontains $profileEntry) {
  ("`n" + $profileEntry) | Out-File $profile -Append -Force -Encoding UTF8
}

nx and nix is no longer available

We renamed nx/nix to nlx to avoid conflicts with the other existing tools - nx and nix. You can always alias them back on your shell configuration file (.zshrc, .bashrc, etc).

alias nx="nlx"
# or
alias nix="nlx"

ni's People

Contributors

adaex avatar amir20 avatar antfu avatar antongolub avatar canrau avatar cawa-93 avatar colinhacks avatar dimava avatar dunqing avatar eggsy avatar flower-f avatar frankfang avatar fritx avatar ghengeveld avatar hannoeru avatar hawtinzeng avatar itpropro avatar jatindotdev avatar joaosamouco avatar jordienr avatar juckz avatar kirklin avatar liby avatar morrislaptop avatar ouduidui avatar posva avatar privatenumber avatar smallnine9 avatar sxzz avatar zeko369 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ni's Issues

[Feature] add outdated packages list output

As with nu -i, add an option for listing outdated packages.
This would be an alias for npm outdated, pnpm outdated and yarn outdated.

Example: nu -o

Thank you for this very helpful tool! ❤️

Cant install due to conflict bin `nx` with NX CLI (nrwl)

Cant install ni on a machine with global nx cli installed (https://nx.dev/latest/angular/getting-started/nx-cli).

 EEXIST

npm ERR! path C:\Users\###\AppData\Roaming\npm\node_modules\@antfu\ni\bin\nx.js

npm ERR! dest C:\Users\###\AppData\Roaming\npm\nx.cmd

npm ERR! EEXIST: file already exists, cmd shim 'C:\Users\###\AppData\Roaming\npm\node_modules\@antfu\ni\bin\nx.js' -> 'C:\Users\###\AppData\Roaming\npm\nx.cmd'

npm ERR! File exists: C:\Users\###\AppData\Roaming\npm\nx.cmd
npm ERR! Remove the existing file and try again, or run npm
npm ERR! with --force to overwrite files recklessly.
npm ERR! A complete log of this run can be found in:

`nx` behaviors different with `npx`

npx will try to load local package bins from ./node_modules/.bin, if not match, then download (to tmp dir?) and execute.

nx seems skipped the search on local package bins, it will always download (to tmp dir?) and execute. For some packages, the results is different as well.

# install
ni -D jest ts-jest

# run
nx ts-jest config:init

I am using pnpm, the echo is like this:

Progress: resolved 11, reused 7, downloaded 2, added 0
......

But if run npx ts-jest config:init, it just run, without an extra installation.

permission denied

image

Permission denied while running nr i, after installing @antfu/ni globally.

Support for common package commands

Do you think the package could be extended to abstract common commands for frameworks like vue:

e.g.

nc bulid

  • react - nr build
  • vue - nr build
  • nestjs - nr build

nc start

  • react - nr start # no prod version
  • vue - nr serve
  • nestjs - nr start

nc dev

  • react - nr start
  • vue - nr dev
  • nestjs - nr start:dev

Windows support

Looks like it does not work on Windows.

pnpm -v # => 5.13.4
ni

Result

'command' is not recognized as an internal or external command,
operable program or batch file.
Detected pnpm but it doesn't seem to be installed.

add uninstall comand

I love this tool so much. It makes my life easier but It would be great if there was a command to uninstall a package

[Idea] Include `taze` with `ni`

I use https://github.com/antfu/taze quite frequently and just thought it would quite fall under the scope of this project, that is, managing dependencies easily with one tool and unified syntax.

One could argue that ni's scope should stay limited. I won't open the debate but I wanted to submit the idea. A nt command would be pretty cool.

[Feature Request] default configuration upward recursive lookup

有这么一个场景,假设有一个文件目录为这样:

| ~
├── react-project
│   ├── .nirc (content: yarn) #不支持
│   ├── bar
│   └── foo
└── vue-project
    ├── .nirc (content: pnpm) #不支持
    └── vite-project

我希望在 react-project 目录下使用 yarn, 在 vue-project 目录中使用 pnpm

这个时候我有以下几种方案:

  1. 在目录下的项目的 package.json 文件中添加 "packageManager": "[email protected]"
  2. 借助 NI_CONFIG_FILE 环境变量,指定 .nirc 文件路径;
  3. 本次请求的功能:使用 findUp 递归向上查找配置文件,如上树形图所示;

上述方案的一些顾虑:

方案1: 如果是三方开源项目,需要手动去添加记录,产生不必要的 git 变更。
方案2: 可能需写一些 shell 脚本来支持目录变更后修改环境变量或者覆盖 .nirc 文件内容,如借助 .zshrc 的 hooks
方案3: 如果实现则会存在一个问题是配置文件混乱,使用者不知道执行命令后使用的是那个包管理器,当其实可以丰富一下帮助文档,比如 ni "?" 目前只是一个 debugger 功能,但可以在这个上面输出更多的内容, config 信息等等等

本次功能请求:

如上树形目录图,在没有明确包管理器时,递归向父目录查找配置文件。

alternative solution for alias conflicts with PowerShell on Windows

I tried out the solution described at the bottom of the README. It didn't work.
By running following command in PowerShell 7, I could get rid of the system alias:
del alias:ni -Force
The -Force flag is needed because it is a system alias that is protected.
Edit: the solution above is only valid for the current session.

The following solution worked:

  1. create powershell profile file:
New-Item $profile -force -itemtype file
  1. open file:
notepad $profile
  1. add following line:
remove-item alias:ni -Force
  1. safe file and open new PowerShell

有没有 --no-frozen-lockfile 的选项?

感谢大佬的工具,对我们非常有帮助!

在我们的 CI 环境中使用 ni 提示
ERR_PNPM_OUTDATED_LOCKFILE Cannot install with "frozen-loackfile" beacause pnpm-lock.yaml is not up-to-date with package.json

Note that in CI enviroments this setting is true by default. If you still need to run install in such cases, use "pnpm install --no-frozen-lockfile"

但是我看了下,好像并不支持 --no-frozen-lockfile
求大佬帮助 🙏

nu -i --latest doesn't work with pnpm

I noticed when I do pnpm -i --latest I get the following output:

❯ pnpm up -i --latest
? Choose which packages to update (Press <space> to select, <a> to toggle all, <i> to invert selection) …
❯ ○ @vueuse/core       6.9.2 ❯ 7.0.0
  ○ lint-staged (dev) 11.2.6 ❯ 12.1.1

Enter to start updating. Ctrl-c to cancel.

But with nu -i --latest I see:

❯ nu -i --latest
All of your dependencies are already up-to-date inside the specified ranges. Use the --latest option to update the ranges in package.json

Seem like --latest is not respected.

When I get a chance I'll try to debug and maybe send a PR.

Some thoughts on `nrm` and other naming conflicts

First of all, thank you very much for this project, it has been very convenient for me, but of course I also encountered the problem of nrm naming conflicts, whenever I want to uninstall a package, I still need to determine the current package manager, considering how often npm uninstall is used, I think I need to discuss this issue.

When I read #33, my first thought was to customize the alias in .nirc, but that didn't seem to work when I tried to dynamically generate the bin file.
idea: read the configuration before install and generate the bin file, add it to directories.bin.

Reference:

After I researched and tried the above options with no results, I had to post the question to the community for further discussion.

If there is no other solution, is it possible to change nrm to nun or some other name ...😂...

Yarn 3 upgrade

nu won't work with yarn v3 because upgrade doesn't exist anymore and has been replaced by up:

[ni --frozen] The behavior of several package managers is inconsistent

Just view https://stackoverflow.com/questions/58482655/what-is-the-closest-to-npm-ci-in-yarn

Unfortunately, because of the way yarn module resolution works, just doing yarn install --frozen-lockfile is sometimes not enough. You can still be left with transitive deps that are invalid.

To truly get the same behavior as npm ci you must do:

rm -rf node_modules && yarn install --frozen-lockfile


Does ni --frozen need to support rimraf? Or there are other considerations

Single binary

Would you be open to the idea of having a single ni binary to avoid things like #20 ?

Maybe something like:

ni
ni add
ni remove
ni run
ni dlx
ni update
...etc

Naming conflicts

After install ni globally, I found nrm instruction is substituted by ni feature.
Usually, I use nrm as managing npm registries tools.

So, can we rename nrm to other name? From the download size of nrm, it was commonly used.

Error when using concurrently

"scripts": {
    "renderer": "ts-node .tauri-vite/runner.js",
    "main": "tauri dev",
    "dev": "concurrently \"nr renderer\" \"nr main\""
  },

when use nr dev main process is exit

"scripts": {
    "renderer": "ts-node .tauri-vite/runner.js",
    "main": "tauri dev",
    "dev": "concurrently \"yarn renderer\" \"yarn main\""
  },

nr dev is ok

npm uninstall

I hope can use nui xxx to uninstall a module, insteading of use npm uninstall or yarn remove, Thanks~~

Windows platform: Conflicts with PowerShell New-Item alias.

View current PowerShell version: run get-host | select version


Version: 5.x

  1. Open folder: C:\Windows\System32\WindowsPowerShell\v1.0

  2. Edit file: Microsoft.PowerShell_profile.ps1 - If the file does not exist, create a new one.

  3. Add the following line to the end of the file: Del alias:ni -Force

  4. Restart the terminal.


Version: 7.x

  1. Open folder: C:\Program Files\PowerShell\7

  2. Edit file: Microsoft.PowerShell_profile.ps1 - If the file does not exist, create a new one.

  3. Add the following line to the end of the file: Remove-Alias -Name ni -Force

  4. Restart the terminal.

`.nirc` `globalAgent` flag doesn't seem to work.

I have a .nirc file in the same directory as my package.json, but the globalAgent is ignored when installing global packages, and is using npm instead of pnpm.

Here is the contents of my .nirc file:

defaultAgent=pnpm
globalAgent=pnpm

Problems identified @ by webstorm package manager when selecting Ni

I use the package manager Ni in webstorm. When there is @ in the directory folder, I can't recognize it, as shown in the following figure
image
My current solution is to copy all the files under @ antfu / Ni to the Ni folder so that they can be used
image

But this is only a temporary solution. It is not convenient for me to manage it (such as future upgrade and other operations)

image

I can't determine whether this is a webstorm problem or a Ni problem, but what I want is if the released package doesn't bring @. That is, the released package is not private. Can this problem be solved?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.