Npm bin script and local file reading - node.js

I've got an npm package that has the bin section for running a cli interface utility. One of the dependent scripts of this utility reads a specific file from this package directory via fs module.
The definition of the cli util is:
"bin": { "my_cli_command": "lib/cli.js" }
When I use npm link and the cli command locally everything works file.
Like:
npm link
my_cli_command
But after installing the package in another node application run the cli command causes a problem with the script file reading because node rebuilds the bin script path:
npm i --save my_lib
npx my_cli_command
There will be an error because my_cli_command reads a local package file which is not available now.
Is there a way to allow the npm bin script to read a file from it's own package?

The problem resolved.
Just started using the path.resolve method and the __dirname variable to compute a file path:
let filePath = path.resolve(__dirname, "../files/somefile.txt");
So that approach will exclude dependency on cwd and an npm cli app will be able to work with it's package files without any problem locally or installed in another project.

Related

What's the difference between "npm install -g" (no args) and "npm link" to create CLI command?

To make a local node.js package work as a global commandline tool, I first add this to package.json
"bin": {
"myCommand": "./index.js"
}
According to the npm docs, I then have two options:
Within that folder, run npm install -g. According to the node docs:
npm install (in package directory, no arguments) ... In global mode
... installs the current package context (ie, the current working
directory) as a global package.
OR
Within that folder, run npm link. The node docs say
npm link in a package folder will create a symlink in the global
folder ... will also link any bins in the package to
{prefix}/bin/{name}
Both of these routes lets me run myCommand anywhere in the terminal while local edits are reflected in the tool without reinstalling/linking.
For local CLI tool purposes, these commands seem functionally identical. Is this true? Why would I want to use npm install -g (no args) when npm link seems functionally identical, plus affording extra capability?
Note: this question is purely about a local node project, nothing to do with anything in a remote package registry

Node.js Package.json file error while running Express.js

I am new to using Node.js
I have a project folder structure C:\A\B\C\
I have C:\A\B\C\html_pages\index.html in which I have React.js code having CDN
and C:\A\B\C\server.js which has Express.js code which I want to run.
I installed nvm then installed Node.js using command
nvm install 4.5
In windows PowerShell, I went to project directory
C:\A\B\C and used npm init (node_modules were not created in this step)
Did npm install express --save to use the express module (now node_modules folder was created but package.json was inside express folder)
Used npm start to start the web server but it gave following error.
ENOENT: no such file or directory, open 'C:\A\B\C\package.json'
I checked the posts on Stackoverflow for this error but was not able to solve it.
Should the package.json be generated directly under 'C:\A\B\C\ ?
I have installed Node.js in C:\Program Files\nodejs while project is not under Program Files, is this related to this?
P.S. : Used npm init --yes so that it installs node_modules inside my project folder and got the request processed but port 3000 gives error that it did not get GET request
The package.json file should be on the root of your directory.
Open command line from your root directory and type,
npm init
This will generate a package.json file on that directory.

Running node CLI module locally

I am having an node module which is a CLI script. The CLI uses the process.cwd() to get the current directory from which the cli script is invoked( that is important).
This works fine when I install the CLI module globally
(https://www.npmjs.com/package/reduxboilergen)
npm i -g <module>
But when I install it locally
npm i -S <module>
Then
1) The script is not invoked
2) I added a script in package.json and then if I run npm run "script_name", it is invoked but it always takes the directory from where the npm script is invoked as the process.cwd().
So if the folder structure is
root
- src
Then even if I run it inside src directory its picking up root as cwd() as the package.json is in root.
The node_module npm-run ( https://github.com/timoxley/npm-run)
solves this problem but is there any other way that you experts can think of so that I dont have to add this dependency?
The answer would be npx as of npm 5.2.0

Different behaviour between 'npm run' command and windows console

Here's a weird problem I'm suffering for days.
I need to create an asar packed file, done with electron "asar" command, like this:
c:/asar pack app app.asar
That packs my "app" folder into a packed file "app.asar". Thats running OK.
The goal
I need to include this instruction inside my package.json script file in order to generate a build process, chainning other actions.
The problem:
Well, when I run this command, by package.json script like this c:/npm run create-asar or either with a gulp-asar process, it creates the app.asar file, but seems to be corrupted.
It can't be unpacked, process throws an error and can't be accessed by the electron app
I can't figure out why.
I've tried to run the exact same command from console, that in package.json, exactly the same, and both with the results above.
what's the difference?
versions info
npm: v3.10.6
node: v4.5.0
asar: v0.13.0
electron: v1.4.3
Install asar locally as a project dependency, cd to your project directory and run:
$ npm install asar --save-dev
Change your npm-script to the following:
"scripts": {
"create-asar": "node_modules/.bin/asar pack app app.asar"
},
Note: asar is being executed via the local node_modules/.bin folder.
Then run $ npm run create-asar and subsequently check whether it unpacks successfully.

Install NodeJS package locally

When i try to install package on my local directory using npm install connect,but it just keep pop up some warning about
no such file or directory, open '/Users/felixfong/package.json'
But i don't not want to install package at my computer directory,i want to install at my local web app directory
Are you sure you are inside your local web app directory when you run the npm install connect command?
cd app-directory/
npm install connect
Also ensure that a package.json file is also present in the app-directory.
If it isn't present, you can use npm init command to create the package.json file interactively.
You have to go inside your project directory using
Then you can check package.json.
If package.json file is not there then initialize npm using the following command:
npm init
Then your can install the package using the following command:
npm install connect
'npm install connect' does not save the connect npm package in package.json file.
For saving the package into package.json file you have to givt --save option like:
npm install connect --save
Make sure that you are in web app's directory. Current path can be checked via command pwd in Linux and cd in windows. Navigate to your web app directory if you are somewhere else. Check existence of package.json by listing the content of the folder. ls and dir can be used for ubuntu and windows respectively for listing content. Commands for ubuntu are as below:
pwd
cd your-path/
ls
Now Initialize npm in your web app directory if package.json is not already existing there.
npm init
This will ask some information like:
name of the app,
its version,
description,
entry point,
test command,
git repo,
keywords,
author and
license (if any)
You can select default values by leaving the fields empty if you aren't sure or confused about any field. Combining this information as json, npm will create a file named package.json
Just run the desired command now after initialization of npm or if its already initialized:
npm install connect

Resources