Call script from custom npm package - node.js

I want to write my own npm package to analyse the structure of a vue project (vueanalyser). So I created a new package with npm init --scope=#my-username and set the "main" property to index.js.
// package.json of the custom package
{
"name": "#my-username/vueanalyser",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"repository": {
"type": "git",
"url": "..."
},
"bugs": {
"url": "..."
},
"homepage": "...",
"description": ""
}
I published the package and added it to my vue project (.node_modules/#my-username/vueanalyser). Now I want to add a command like "analyse": "vueanalyser start" to the script property of the package.json of the vue project. If I do so I obviously get an error, that the command is unknown.
So I realized, that I can call the index.js with "analyse": "node node_modules/#my-username/vueanalyser/index.js, but I have seen packages where scripts can be called with a much shorter way e.g. "styleguide:build": "vue-styleguidist build". What do I have to change in order to call my script this way ("analyse": "vueanalyser start")?

the npm bin property
This specifies executables to copy into node_modules/.bin.
Add the executable header to your index.js
Add this to the first line: #!/usr/bin/env bash
Make the file executable
chmod +x index.js
Edit package.json
Add the bin property:
{
...
"bin": { "vueanalyser": "index.js" }
Republish package
Install package
Find node_modules/.bin/vueanalyser is a symlink to ../<package_name>/index.js!

Related

How can I execute a bin with yarn?

I have the following package.json and I'd like to run the bins "build" and "run":
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"bin": {
"build": "./bin/build.js",
"dev": "./bin/dev.js"
}
}
I've tried:
yarn run build
and I get
error Command "build" not found.
I've also tried:
yarn build
but the same thing happens:
error Command "build" not found.
It's propably not the right way to run bins. But then again, what is the right way to run bins with yarn?
Your package isn't installed.
When Yarn (and NPM) installs your package, it adds the commands under node_modules/.bin/, e.g. node_modules/.bin/build. Running yarn build would (if it doesn't find a matching script in the current package) look for a build in this .bin, then traverse upwards through the filesystem, looking for other node_modules/.bin/build's.
If your build script is only meant to be run while developing that specific package, add it as a script (see example here). It would more or less look like this:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": {
"build": "node ./bin/build.js",
"dev": "node ./bin/dev.js"
}
}
Do not need relative path added:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": {
"build": "build.js",
"dev": "dev.js"
}
}
The hashbang comment specifies the path to a specific JavaScript interpreter that you want to use to execute the script.
For example, helloWorld.js in ./node_modules/.bin:
#!/usr/bin/env node
console.log("Hello world");
You have a typo in your package.json. Where it says bin: it should say scripts:
{
"name": "simple-site",
"version": "0.0.5",
"license": "MIT",
"scripts": { // <-- here
"build": "./bin/build.js",
"dev": "./bin/dev.js"
}
}

Lerna publish adds gitHead key to package.jsons

I've got the following scripts in my root package.json in my lerna repo:
"scripts": {
"lerna:changed": "npx lerna changed",
"lerna:diff": "npx lerna diff",
"lerna:publish": "npx lerna publish"
}
After running npm run lerna:publish I see a gitHead key in my package.json file of all packages (not the root.json). This becomes a big problem when it comes to versioning because all packages will get bumped when this change is added. Why is this happening and how can I stop it?
I used to have a publish command in my root package.json file and I know this could have caused this problem originally but I've since removed it. Now, the package.json files for my sub packages look basically like this:
{
"name": "#myProject/appOne",
"version": "0.0.15-alpha.0",
"description": "",
"main": "index.js",
"directories": {
"lib": "lib"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
UPDATE: It seems like gitHead is added as a temporary key during the publish phase. My publish creates tags and commits them to GIT but my deployment to my registry is failing and that's probably why the cleanup isn't happening.

sh: 1: cucumber.js: not found

Trying to use this tutorial here:
https://github.com/lykmapipo/nodejs-cucumber-sample
The output to nvm current is: v10.12.0.
The output to npm --version is: 6.4.1>
I get the error below once I invoke npm test:
> nodejs-cucumber-sample#0.0.1 test /home/gnuc/code/nodejs-cucumber-sample
> cucumber.js
sh: 1: cucumber.js: not found
I am not sure as to why this is the case. The $PATH includes /home/gnuc/.nvm/versions/node/v10.12.0/bin. And I have already used npm install cucumber -g and npm install cucumber
Make sure that your package.json file includes this: "test": "cucumber-js"
So that it looks something like this:
{
"name": "hellocucumber",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": **"cucumber-js"**
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"cucumber": "^5.1.0"
}
}
So, you need to actually call the npm package/library. I have the following defined in my package.json:
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js"
},
you can also add some ---tags in this call.
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js --tags #RegressionTestSuite"
},
this will run any feature files that have #RegressionTestSuite at the top
Also, I have an output/results file created with a time stamp.
"scripts": {
"test": "node ./node_modules/.bin/cucumber-js --tags #RegressionTestSuite --format json:./results/log_new_`date +%Y-%m-%m__%H-%M`.json""
},
I hope this helped.
node ./node_modules/cucumber/bin/cucumber-js
This command is working fine.
And you got sh:1: cucumber.js: not found error means first things please look out the path of cucumber.js

When using npm install for local package, all files are included

I am setting up my local module to be installed via npm install --save ../path/to/my/project.
When running the npm install, the node_modules/my_project directory gets populated with all source files - not just the dist/ folder as configured in package.json. I'm not sure why.
Here's my setup:
Test Project (main project)
Files:
index.js
package.json
dist/
index.js
package.json
{
"name": "test",
"version": "1.0.0",
"description": "a test",
"main": "dist/index.js",
"files": [
"dist"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"test"
],
"author": "Me",
"license": "MIT"
}
-
Test Project2 (to import main project as a dependency)
package.json
{
"name": "test2",
"version": "1.0.0",
"description": "test2",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "MIT",
"dependencies": {
"test": "file:../test"
}
}
Once I run npm install, my node_modules/ directory looks like this:
test/
index.js
package.json
dist/
index.js
Is there something I'm overlooking? I'm under the assumption that the files: [] field is used to specify which files are included when the package is installed.
Additionally, I've tried running npm cache clean -f and npm cache verify, neither of which solve my issue.
It seems that this only occurs when attempting to install a local module. To test, I made a test repo on github and installed from there, only the dist/ file was included. Not sure why.

When npm installing GULP in local project - I get a ton of folders in node_modules - why?

I've been following this Gulp install by CSS tricks - https://css-tricks.com/gulp-for-beginners/ I noticed that when I get to their folder shot of what should be in the node_module folder... they only have bin and gulp. I have 40+ folders that get added. Is this suppose to happen? Does Gulp need these dependencies? If not why is it happening? Here is my package.json with certain info emitted and a screenshot of 1/2 the folders made on install. I start the project clean (no folders or crazy installs uninstalls were done).
{
"name": "---",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+-----.git"
},
"keywords": [
"---",
"---'",
"---",
"---"
],
"author": "-----",
"license": "ISC",
"homepage": "------",
"devDependencies": {
"gulp": "^3.9.0"
}
}
This is due to changes in the new npm 3, which I assume you're using here (you can check by doing npm -v if you want to make sure). All modules and their dependencies are now in /node_modules instead of being nested in sub-folders of the module that depended on them (like it used to be).
Your dependencies will now be installed maximally flat. Insofar as is possible, all of your dependencies, and their dependencies, and THEIR dependencies will be installed in your project's node_modules folder with no nesting.
Source: http://dailyjs.com/2015/06/26/npm-3/

Resources