combine bin and install script - node.js

Given the following excerpt from package.json:
{
"name": "foo",
"version": "0.0.1",
"description": "Foo",
"main": "bin/index.js",
"bin": "bin/index-cli.js",
"private": true,
"scripts": {
"install": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
...
}
npm install fails with:
npm ERR! enoent ENOENT: no such file or directory, chmod '/home/mk/work/my-project/node_modules/foo/bin/index-cli.js'
Which is, because the install script (tsc - typescript compiler), which generates bin/index-cli.js from src/index-cli.ts is not executed before npm tries to create the sym link.
Question: What is the right way to have a bin, which is generated by an install script?

Related

Getting missing script: start error in npm

I get this error when I run npm start.
npm ERR! Missing script: "start"
npm ERR! Did you mean one of these?
npm ERR! npm star # Mark your favorite packages
npm ERR! npm stars # View packages marked as favorites
npm ERR! To see a list of scripts, run:
npm ERR! npm run
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\USER\AppData\Local\npm-cache\_logs\2022-04-28T13_04_19_260Z-debug.log
package.json has various sections, scripts is one of them, which allows you to write npm scripts which we can run using npm run <script-name>. The error you're getting is because your start script is missing in that section.
For a node app, your package.json file should look similar to this.
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.20.0",
"cors": "^2.8.5",
"express": "^4.17.3"
}
}
In the above code, focus on the scripts section. The following line is missing in your package.json file.
"scripts": {
"start": "node app.js",
},
Add this line and you're good to go.
Option_1: Control inside file "package.json":
"scripts": {
"start": "what command is here?",
},
Option_2: Control Terminal (folder) where you write command OR open the responding terminal for your project: right-click
on the project name -> "Open in integrated Terminal"
You have to put what command you need npm to run when you give npm start.
You have to write node index.js in scripts.start in your package.json file

Why can't I successfully run "npm start" command on mac?

I'm trying to install npm and I keep getting errors. Initially the problem was I needed a package-lock.json file and package.json file and that was solved by typing "npm init" into the terminal.
Then when trying to run "npm start" it said I was missing the start script. So I opened the package.json file to add it as shown below:
{
"name": "arvinder",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Now I'm getting the following errors:
Arvinders-MacBook-Air:~ Arvinder$ npm start
npm ERR! file /Users/Arvinder/package.json
npm ERR! code EJSONPARSE
npm ERR! JSON.parse Failed to parse json
npm ERR! JSON.parse Unexpected string in JSON at position 137 while parsing '{
npm ERR! JSON.parse "name": "arvinder",
npm ERR! JSON.parse "version": "1.'
npm ERR! JSON.parse Failed to parse package.json data.
npm ERR! JSON.parse package.json must be actual JSON, not just JavaScript.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/Arvinder/.npm/_logs/2020-07-24T16_26_18_155Z-debug.log
I'm not sure where the error is in the json file or why it can't parse it so if anyone could help point out a solution that would be greatly appreciated.
Seems like you're missing a comma in line 7, after "start": "node index.js". Here's the correct one:
{
"name": "arvinder",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
I recommend you to install an extension for your IDE that can catch syntax errors on JSON files.
I suggest you use vscode.
It will throw an error when you missing a comma.
For example:
the example of missing a comma

npm start showing weird error 1

I am new to react js and I have installed the dependencies for the application. Below is my package file:
package.json
{
"name": "reactapp",
"version": "0.0.0",
"description": "learn and test",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --hot"
},
"author": "john doe",
"license": "BSD-2-Clause",
"dependencies": {
"webpack": "~1.13.3",
"webpack-dev-server": "~1.16.2",
"react": "~15.3.2",
"react-dom": "~15.3.2"
},
"devDependencies": {}
}
It throws the error:
npm ERR! weird error 1
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
npm ERR! not ok code 0
But, in the terminal while it doesn't show up anything when in scripts it is
"test": "echo \"Error: no test specified\" && exit 1"
You need to use the sudo command while installing nodejs-legacy.
Executing sudo apt-get install nodejs-legacy from your command line/shell will resolve the issue.

Unable to build nodejs app

So I'm trying out this go-react example but I am not able to build it by following the instructions. Running the following command:
~/react-go/jsapp$ npm run build
I get the following error:
> example-js-app#0.0.0 build /home/x/react-go/jsapp
> browserify -t reactify -r react -r ./src/App > ../webapp/static/bundle.js
/usr/bin/env: node: No such file or directory
npm ERR! weird error 127
npm WARN This failure might be due to the use of legacy binary "node"
npm WARN For further explanations, please read
/usr/share/doc/nodejs/README.Debian
npm ERR! not ok code 0
Here's the package.json content:
{
"name": "example-js-app",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "browserify -t reactify -r react -r ./src/App > ../webapp/static/bundle.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"envify": "^3.4.0",
"react": "^0.14.0",
"browserify": "^11.2.0",
"reactify": "^1.1.1"
}
}
I'm new to nodejs. What am I doing wrong?

pass npm config into npm scripts entry

I'm running my entire project from npm scripts, and i've namespaced a few methods underneath test for clarity. I'd like to use the silent level of verbosity for all test related commands instead of the normal, extremely verbose npm ERR! Exit status 1...
Package.json
{
"name": "test",
"version": "0.0.0",
"scripts": {
"test": "npm run test:unit && npm run test:integration",
"test:unit": "mocha test/unit",
"test:integration": "mocha test/integration"
}
}
I've tried including the --loglevel=silent in the npm scripts commands, but that does not work.
I do not want to enable this for all commands, just for testing related ones, so setting the flag on the environment or in .npmrc is not an option (unless there is additional criteria I can define in .nmprc).
The flag -s can do that :
Package.json
{
"name": "test",
"version": "0.0.0",
"scripts": {
"test": "npm run test:unit -s && npm run test:integration -s",
"test:unit": "mocha test/unit",
"test:integration": "mocha test/integration"
}
}
It just silences output from npm on those tasks.

Resources