Is it possible to reference a property in package.json - node.js

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"

Related

IntelliJ and WebStorm treating npm linked local module as an indirect dependency, and not using it in it's suggestions

I have a local node module I am trying to reuse in my other modules.
I use npm link to add the module as a dependency to other modules.
Everything works fine, however IntelliJ Treats this as an indirect dependency even if I add the dependency to the pom. Because of this none of the autocompletions for the dependency work.
Pom1:
{
"name": "test-module",
"version": "1.0.0",
"description": "\"Module functionality testing project\"",
"main": "index.js",
"scripts": {
"test": "\"No Tests done\""
},
"author": "",
"license": "ISC"
}
pom2:
{
"name": "test-module-2",
"version": "1.0.0",
"description": "\"Module functionality testing project\"",
"main": "index.js",
"scripts": {
"dev": "node index.js",
"test": "\"No Tests done\""
},
"author": "",
"license": "ISC",
"dependencies": {
"test-module": "^1.0.0"
}
}
Anyone know why this is and how to fix it?
The issue is tracked at WEB-49242, please follow it for updates.
As a workaround, please try including node_modules\test-module folder in index by selecting Mark directory as/Cancel exclusion from its right-click menu

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

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!

Running a custom built npm package not working

I am building an npm library and published it to npm.
After publishing I am trying to install the library globally.
But it's not working, looks like it's trying to open the file directly
My package.json looks like as follows:
{
"name": "lssomename",
"version": "1.0.4",
"description": "Test",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"run": "node index.js"
},
"author": "Test",
"license": "ISC",
"dependencies": {
"command-line-args": "^5.0.2",
"fs": "0.0.1-security",
"ncp": "^2.0.0",
"rimraf": "^2.6.2",
"zip-folder": "^1.0.0"
}
}
As soon as I try to run it throws the following error
/home/thabung/.nvm/versions/node/v8.5.0/bin/lssomename: line 1: $'\r': command not found
/home/thabung/.nvm/versions/node/v8.5.0/bin/lssomename: line 2: syntax error near unexpected token `('
'home/thabung/.nvm/versions/node/v8.5.0/bin/lssomename: line 2: `const log = require("./logger");
The same error comes if try to run directly the index.js file, meaning
when I try
./index.js
instead of
node index.js
Ok the issue was, I need to add an indicator in the begining of the file,
also known as shebang for Unix like systems like as follows
#!/usr/bin/env node
in the beginning of my index.js file & it started working after that.

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

Resources