How can I execute a bin with yarn? - node.js

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"
}
}

Related

Call script from custom npm package

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!

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

Why is Heroku not detecting my start script?

I have a Node.js application on Heroku. To start it, I need index.jsto be executed. To do that, I added a package.json file with a start script.
As I read in Heroku Node.js Support:
First, Heroku looks for a Procfile specifying your process types.
If no Procfile is present in the root directory of your app during the build process, your web process will be started by running npm start, a script you can specify in package.json...
When I define the start script to be node index.js and I deploy my app to Heroku, I don't see any Dynos in the resources tab.
My code:
package.json
{
"name": "node.js app",
"version": "1.4.0",
"description": "A node.js app.",
"main": "index.js",
"repository": {
"type": "git",
"url": "Node.js repository"
},
"author": "Realex78",
"license": "MPL-2.0",
"dependencies": {
"npm package": "^1.0.0"
},
"devDependencies": {
"npm package": "^1.0.0"
},
"scripts": {
"start": "node index.js",
"poststart": "node scripts/poststart.js",
"restart": "node scripts/restart.js"
}
}
Make sure that you aren't using a Procfile, as Heroku gives it priority over package.json!
I.e. even though I had updated my startup script in package.json:
"scripts": {
"start": "node --harmony server.js"
},
I had forgotten that my Procfile was setup like this:
web: node server.js
Therefore my startup script in package.json was being ignored! The fix was to update my Procfile (to in this case include the --harmony param).
Note that I did not have to wait 24 hours for a successful deploy; it worked immediately.

Heroku: Bunch of npm errors on deployment

I have a bunch of npm errors in my heroku logs. How can I begin to debug this? thanks!
screenshot
package.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": "8.1.1",
"npm": "5.0.3"
},
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.0"
}
}
NPM ERR: missing script: start
It seems like you're missing the "start" script in your package.json file. That's where I would start looking. Heroku might assume you've set this script up beforehand. Here's a sample package.json file with the start script:
{
"name": "your-app",
"version": "1.0.0",
"main": "index.js",
"scripts": { // your npm scripts go here
"start": "nodemon index.js", // or whatever server package you're using
"lint:js": "node_modules/eslint/bin/eslint.js ./ ./**/*.js --fix; exit 0",
"lint:css": "node_modules/csslint/cli.js public/css/; exit 0",
"test": "NODE_ENV=test node_modules/mocha/bin/mocha"
},
"more": "settings below"
}
Here's a blurb from the Heroku website:
Specifying a start script
To determine how to start your app, Heroku first looks for a Procfile. If no Procfile exists for a Node.js app, we will attempt to start a default web process via the start script in your package.json.
The command in a web process type must bind to the port number specified in the PORT environment variable. If it does not, the dyno will not start.
For more information, see Best Practices for Node.js Development and Heroku Node.js Support.
https://devcenter.heroku.com/articles/deploying-nodejs

Is it possible to reference a property in package.json

Consider the following package.json:
{
"name": "expressapp",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"dev": "./node_modules/.bin/nodemon app.js"
},
"author": "me",
"license": "ISC",
"dependencies": {
"express": "^4.13.4",
"mongodb": "^2.1.7"
},
"devDependencies": {
"nodemon": "^1.9.1"
}
}
Now I want to rename my app.js to index.js. So I have to edit that name at least in two different places: main property and dev property of scripts. Is it possible to reference the value of main property inside package.json?
You can do it through environment variables
Under Linux
"scripts": {
"dev": "./node_modules/.bin/nodemon $npm_package_main"
},
Under Windows
"scripts": {
"dev": "./node_modules/.bin/nodemon %npm_package_main%"
},
JSON itself doesn't support variables.
It's up to the program consuming JSON that can decide whether to treat any particular pattern as a variable or to be replaced with some other text somehow.
While other answers have mentioned using the $ or %% notation for variables (that are OS-dependent), I think you can also solve your problem in the following way:
Instead of nodemon app.js you can just write nodemon .:
"main": "app.js",
"scripts": {
"dev": "nodemon ."
}
. will also automatically resolve to app.js
If you have a "main": "app.js" in package.json (in any folder, be it top level or sub folders) then any node process will identify the app.js file as the default one to load (either in require calls or executing via cli), just like it does index.js automatically.
Yes, you can reference to any field value from package.json when executing scripts.
But there is a difference, when you run script under windows, you should use %npm_package_field% and with unix based OS you should use $npm_package_field.
Where field is field name from package.json.
Under windows, you can use:
"dev": "./node_modules/.bin/nodemon %npm_package_main%"
Under unix:
"dev": "./node_modules/.bin/nodemon $npm_package_main"

Resources