Home Explore Blog CI



nushell

3rd chunk of `book/modules/using_modules.md`
bf818403e4b7cbd8c8844979c3f85ca3044a20995d5ff9810000000100000e1f
  use std/formats *
  ls | to jsonl
  ```

  Notice how the `to jsonl` command is placed directly in the current scope, rather than being a subcommand of `formats`.

- **Import one or more definitions from a module**

  Nushell can also selectively import a subset of the definitions of a module. For example:

  ```nu
  use std/math PI
  let circle = 2 * $PI * $radius
  ```

  Keep in mind that the definitions can be:

  - Commands
  - Aliases
  - Constants
  - Externs
  - Other modules (as submodules)
  - Environment variables (always imported)

  Less commonly, a list of imports can also be used:

  ```nu
  use std/formats [ 'from ndjson' 'to ndjson' ]
  ```

  ::: note Importing submodules
  While you can import a submodule by itself using `use <module> </submodule>` (e.g., `use std help`), the entire parent module and _all_ of its definitions (and thus submodules) will be _parsed_ when using this form. When possible, loading the submodule as a _module_ will result in faster code. For example:

  ```nu
  # Faster
  use std/help
  ```

  :::

## Importing Constants

As seen above with the `std/math` examples, some modules may export constant definitions. When importing the entire module, constants can be accessed through a record with the same name as the module:

```nu
# Importing entire module - Record access
use std/math
$math.PI
# => 3.141592653589793

$math
# => ╭───────┬──────╮
# => │ GAMMA │ 0.58 │
# => │ E     │ 2.72 │
# => │ PI    │ 3.14 │
# => │ TAU   │ 6.28 │
# => │ PHI   │ 1.62 │
# => ╰───────┴──────╯

# Or importing all of the module's members
use std/math *
$PI
# => 3.141592653589793
```

## Hiding

Any custom command or alias, whether imported from a module or not, can be "hidden" to restore the previous definition using
the [`hide`](/commands/docs/hide.md) command.

The `hide` command also accepts import patterns, similar to [`use`](/commands/docs/use.md), but interprets them slightly differently. These patterns can be one of the following:

- If the name is a custom command, the `hide` command hides it directly.
- If the name is a module name, it hides all of its exports prefixed with the module name

For example, using `std/assert`:

```nu
use std/assert
assert equal 1 2
# => Assertion failed
assert true
# => Assertion passes

hide assert
assert equal 1 1
# => Error:
# => help: A command with that name exists in module `assert`. Try importing it with `use`

assert true
# => Error:
# => help: A command with that name exists in module `assert`. Try importing it with `use`
```

Just as you can `use` a subset of the module's definitions, you can also `hide` them selectively as well:

```nu
use std/assert
hide assert main
assert equal 1 1
# => assertion passes

assert true
# => Error:
# => help: A command with that name exists in module `assert`. Try importing it with `use`
```

::: tip
`main` is covered in more detail in [Creating Modules](./creating_modules.md#main-exports), but for end-users, `main` simply means "the command named the same as the module." In this case the `assert` module exports a `main` command that "masquerades" as the `assert` command. Hiding `main` has the effect of hiding the `assert` command, but not its subcommands.
:::

## See Also

- To make a module always be available without having to `use` it in each Nushell session, simply add its import (`use`) to your startup configuration. See the [Configuration](../configuration.md) Chapter to learn how.

- Modules can also be used as part of an [Overlay](../overlays.md).

Title: Importing Constants, Hiding, and Additional Resources
Summary
This section explains how to import constants from modules in Nushell, access them through records, and selectively hide commands or aliases using the `hide` command. It also covers the use of import patterns with `hide` and suggests using startup configuration for modules to be always available. Finally, it references related topics such as overlays and module creation.