0

I may have messed something up with my installation, I don't know.

I reinstalled node with N package manager (script? package?) some time ago to solve sudo problems for my global packages, and today I encountered a problem -- I couldn't require globally installed packages in the node REPL (require('lodash') for example).

I am pretty sure I have something messed up with my node setup, but the more I skim through the web the more confused I am -- what are $NODE_MODULES, $PREFIX, .../lib/node, .../lib/node_modules, $N_PREFIX, which and why are obsolete, what are the differences etc.

My current setup looks like that:

# fragment of ~/.bashrc
# setup NODE
export N_PREFIX="$HOME/.n"
export PATH=$N_PREFIX/bin:$PATH

directory structure:

# output of `tree $N_PREFIX -L 2`. Arrows are symlinks, bin/node is executable
/home/tooster/.n
├── bin
│   ├── check -> ../lib/node_modules/checker/cli.js
│   ├── eslint -> ../lib/node_modules/eslint-cli/bin/eslint.js
│   ├── eslint-cli -> ../lib/node_modules/eslint-cli/bin/eslint.js
│   ├── ffmpeg-bar -> ../lib/node_modules/ffmpeg-progressbar-cli/lib/main.js
│   ├── js-yaml -> ../lib/node_modules/js-yaml/bin/js-yaml.js
│   ├── node
│   ├── npm -> ../lib/node_modules/npm/bin/npm-cli.js
│   ├── npx -> ../lib/node_modules/npm/bin/npx-cli.js
│   ├── tsc -> ../lib/node_modules/typescript/bin/tsc
│   └── tsserver -> ../lib/node_modules/typescript/bin/tsserver
├── include
│   └── node
├── lib
│   └── node_modules
├── n
│   └── versions
└── share
    ├── doc
    ├── man
    └── systemtap

And the return of 'module' from Node REPL:

Module {
  id: '<repl>',
  path: '.',
  exports: {},
  filename: null,
  loaded: false,
  children: [],
  paths: [                              <-- None of these actually exist
    '/home/tooster/repl/node_modules',
    '/home/tooster/node_modules',
    '/home/node_modules',
    '/node_modules',
    '/home/tooster/.node_modules',
    '/home/tooster/.node_libraries,
    '/home/tooster/.n/lib/node'
  ]
}

I cannot find any info about difference between the .../lib/node and .../lib/node_modules. Symlinking latter to the former seems to work, but I don't know what are consequences of doing that, so I'd rather not do that blindly.

Also npm config get prefix returns /home/tooster/.n.

How should a proper installation of npm with N look like? I want to have my globally installed packages available in REPL, because I often write quick scripts in Node.

Tooster
  • 322
  • 3
  • 14
  • I do not think your installation is wrong, but by default you can not require modules which you have installed globally. Related: https://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package, https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders, https://stackoverflow.com/questions/42445679/node-not-finding-global-module – shadowspawn Oct 03 '21 at 04:22
  • Then it defeats the purpose of using REPL as a quick hacking tool if I have to install packages locally for my 4-liner that I want to run in arbitrary directory. I don't understand why would the creators it's a good idea to forbid using global packages in REPL. – Tooster Oct 03 '21 at 12:50

0 Answers0