Use variables in package.json - javascript

It's possible to access variables inside a package.json file with $npm_package_[key]. I need to make something like this, but had no success:
{
"name": "core"
"version": "1.0.0"
"scripts": {
"first": "echo $npm_package_myvariable"
"second": "echo $npm_package_myvariable"
}
"myvariable": "$npm_package_name:$npm_package_version"
}
I need to reuse the value of the key myvariable in multiple scripts, but unexpectedly, the printed value is $npm_package_name:$npm_package_version instead of the expected core:1.0.0.
I'm currently using:
nodejs version v10.16.3
macOS Catalina v10.15.3

I am highlighting 3 ways to do this. Each way is an increment to the prior way.
1. First approach
package.json:
{
"name": "core"
"version": "1.0.0"
"scripts": {
"start": "echo $var1"
}
}
Start npm:
var1=10 npm start
2. Second approach
First approach would fail if user fails to add var1 while running npm. Better approach is to use a default value
package.json:
{
"name": "core"
"version": "1.0.0"
"scripts": {
"start": "echo ${var1:10}"
}
}
Start npm:
var1=20 npm start : Using passed value
npm start : Uses defined default value
Let's now look at the final approach
3. Third approach
If you want to access variables in multipe scripts, you need npm-run-all dependency.
Install dependency:
npm i -D npm-run-all
package.json:
{
"name": "core"
"version": "1.0.0"
"scripts": {
"start": "npm-run-all multi:*",
"multi:first": "echo ${var1:-10}"
"multi:second": "echo ${var1:-10}"
}
}
Start npm:
var1=10 npm start

If you're using Linux
{
"name": "core",
"version": "1.0.0",
"scripts": {
"first": "echo $npm_package_name:$npm_package_version"
"second": "echo $npm_package_name:$npm_package_version"
}
}
If you're using Windows
{
"name": "core",
"version": "1.0.0",
"scripts": {
"first": "echo %npm_package_name%:%npm_package_version%"
"second": "echo %npm_package_name%:%npm_package_version%"
}
}

Related

npm pack removing file extensions from import

I'm trying to a create an internal library for work.
All seems to be fine until I attempt to use in another project.
The file extension in all of the import statements seems to have been removed during the npm pack phase.
In other words, the statement:
import * as Account from './modules/account.js'
becomes:
import * as Account from './modules/account'
This causes the import to fail.
I originally thought this may have been because I used the .js extension instead of .mjs, but switching to .mjs yields the same results.
main.js
import * as Account from './modules/account.js'
Account.secretSquirrel().then( data => console.log( 'inspector gadget', data ) );
node version
v16.15.0
package.json (sensitive info redacted)
{
"name": "#Nunya",
"version": "0.0.0",
"description": "Nunya",
"private": true,
"main": "./lib/main.js",
"scripts": {
"build": "npm run pack",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "Nunya.git"
},
"author": "Nunya",
"license": "ISC",
"type": "module",
"exports": {
".": {
"require": "./lib/main.js",
"default": "./lib/main.js"
},
"./Account": "./lib/modules/account.js"
}
}
As far as I can tell, this shouldn't be happening. Not sure how to resolve
Evidently, adding an import object defining the resolutions for the relative paths to package.json resolves this issue. Although, I'm certain there's a non-manual way to accomplish this, it is unbeknownst to me at this time.
If anyone knows, please let me know

NPM npm_package_main variable is always empty

Problem
NPM $npm_package_main variable is always empty.
When I set the package.json file with "main": "index.js"
Set the "start" property from scripts to "start": "node $npm_package_main"
Then run npm start
Problem: the CLI executes the Node REPL mode, ignoring the "main" variable from package.json.
Expected behavior: execute the command as node index.js.
Environment
Linux Ubuntu 20.04.1
npm -v = 7.3.0
node -v = v15.5.0
npm run env | grep npm_package_name = npm_package_name=app
npm run env | grep npm_package_main = EMPTY
How to reproduce
Create an "app" directory and enter the new directory
Create an "index.js" file with the following content
console.log('HELLO');
Run npm init and hit ENTER for all questions
Edit the package.json file and add the following line to the "scripts" property:
"start": "node $npm_package_main",
now your package.json must look like this
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"devDependencies": {},
"scripts": {
"start": "node $npm_package_main",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Run "npm start"
File "index.js" is not executed and Node enters the REPL mode.
Attempts
Set "start" and running "npm start" for:
"echo $npm_package_main" prints nothing
"echo $npm_package_name" prints "app"
"echo $npm_package_version" prints "1.0.0"
References
NPM package.json variables: https://docs.npmjs.com/cli/v7/using-npm/scripts
NPM Github issue: https://github.com/npm/cli/issues/2585
The official answer from NPM: use "node .". There's no official reason for this behavior, until this post.
According to the documentation, the "main" property contains the entrypoint for your app when is used as a module in other projects: https://docs.npmjs.com/cli/v7/configuring-npm/package-json#main
Solution
Use the "config" property instead:
{
"name": "app",
"version": "1.0.0",
"description": "",
"devDependencies": {},
"config": {
"main": "index.js"
},
"scripts": {
"start": "node $npm_package_config_main",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Run "npm start"
It works
References:
https://github.com/npm/cli/issues/2585
https://github.com/npm/cli/pull/2446

Lite-server not working

I had a project that was previously using lite-server. I can no longer get it to run using
npm run
I see the following from the console
PS C:\Users\XXX\Documents\Work\Repos\Homepage_Slider> npm run
Lifecycle scripts included in homepage_slider: test
echo "Error: no test specified" && exit 1 start
lite-server
I don't really understand why this isn't working anymore.
I've tried:
Updating NPM
Running lite-server globably
Make sure package.json is set up correctly
Here is my package.json file
{
"name": "homepage_slider",
"version": "1.0.0",
"description": "A simple slider for homepage",
"main": "index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "lite-server"
},
"repository": {
"type": "git",
"url": "XXX"
},
"keywords": [
"Slider"
],
"author": "XXX",
"license": "ISC",
"devDependencies": {
"lite-server": "^2.3.0"
},
"dependencies": {
"npm": "^6.0.1"
}
}
npm run is a reserved word for npm itself. Check the documentation
What you really need is npm start which is a shortcut for npm run start which is a shortcut for npm run-script start!

NPM environment variables not working in windows?

In my package.json file I'm trying to compile less code using versioning, something like this:
"scripts" {
...
"build:css": "lessc --source-map css/index.less build/$npm_package_name.$npm_package_version.css",
...
}
The problem is that the output files come back as
$npm_package_name.$npm_package_version.css
instead of
my-project.1.0.0.css
I've read that you can do
%npm_package_name%.%npm_package_version%.css
but this hasn't worked for me.
Anyone know why the variables aren't working? Do these variables work at all in Windows? If so, how do I make them work?
To make it cross-platform, use cross-var:
"scripts" {
...
"build:css": "cross-var lessc --source-map css/index.less build/$npm_package_name.$npm_package_version.css",
...
}
This package.json works for me on Windows 10:
{
"name": "x",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "Unlicense"
}
Note however that the environment variable substitution occurs after its been echoed by NPM, so the output looks like this:
C:\try\x>npm test
> x#1.0.0 test C:\try\x
> echo %npm_package_name%.%npm_package_version%.css
x.1.0.0.css

How to set npm run dev as starter for your app?

I am working with React and I am trying to figure out how to set npm run dev in order to compile and put my app to work. Right now, I need to do this in the terminal in order to see my app running
$ node server/index.js
here is the way I have my folders
and here is what I have in package.json
{
"name": "iOS.server2x.socket",
"version": "0.0.1",
"description": "iOS Live Socket.",
"author": "iOS Interactive",
"main": "server/index.js",
"port": 1101,
"scripts": {
"pretest": "eslint ./server",
"test": "echo \"Error: no test specified\" && exit 0",
"start": "rm -rf /home/git/.forever/chat.log; forever start --uid 'chat' ./server/index.js",
"stop": "forever stop chat"
},
"repository": {
"type": "xxxxxx",
"url": "http://url/url"
},
"dependencies": {
"async": "^1.4.2",
"babel": "5.8.3",
},
"devDependencies": {
"babel-eslint": "4.0.5",
}
}
And also, should I need webpack for this ?
I will answer my own question:
there is a way to set this actually, all I did is in the package.json in the scripts part
"scripts": {
"dev": "nodemon ./server/index.js",
"pretest": "eslint ./server",
"test": "echo \"Error: no test specified\" && exit 0",
"start": "rm -rf /home/git/.forever/chat.log; forever start --uid 'chat' ./server/index.js",
"stop": "forever stop chat"
}
so, npm run <here something in that json>, like
$ npm run dev in order to start the server
$ npm run pretest this is the task to run eslint
and so on . . .

Categories

Resources