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
Related
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%"
}
}
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!
so I have my package.json file like this:
{
"name": "chat_app",
"version": "0.2.0",
"description": "The second iteration of the chat app",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "index.js"
},
"author": "Tino",
"license": "MIT",
"dependencies": {
"express": "^4.14.0",
"jade": "^1.11.0"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-jade": "^1.1.0"
}
}
Here is my index.js file:
var app = require('express')(),
express = require('express'),
http = require('http').Server(app),
jade = require('gulp-jade');
app.use(express.static(path.join(__dirname, '/public')));
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
})
http.listen(3000, function() {
console.log('listening on localhost:3000');
})
When I type: node start it does not work. Why is this? Much help is appreciated.
Thanks
The scripts in your package.json should read:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
To run these scripts use the commands
npm test
or
npm start
Using npm scripts gives you the flexibility to chain commands and use build tools.
You can read more about this here.
Example
Install nodemon, a tool that automatically restarts your node application when you make changes.
Just use nodemon instead of node to run your code, and now your process will automatically restart when your code changes.
...from your terminal run: npm install -g nodemon
Now in your package.json add the following script:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev": "nodemon index.js"
},
And from your command line run this command:
npm run dev
This should give you a basic understanding of how npm scripts work.
The docs for nodemon are here if you are interested.
the scripts simply run the command you write in them in the shell/terminal. so if you want to run npm start and have node running index.js you need to write
"scripts": {
"start": "node index.js"
}
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 . . .
I'm trying to create my first package with nwjs, so I created a simple app with only nwjs module installed locally with npm. My app main file has only a console.log("Hello World!"). I zipped the app files - keeping them in the root - and renamed to app.nw. I basically followed this doc. The package tree is the following:
app.nw
- node_modules
- nw (and all its files inside)
- package.json
- app.js
And my package.json is:
{
"name": "app3",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"window": {
// Some properties here
}
}
Then I created an empty folder and copied app.nw to it. From the module folder /nw/nwjs I copied the files: nw.exe, nw.dll, nw_elf.dll and ffmpeg.dll to it, and dragged app.nw over nw.exe. The debug.log file shows me the following error:
[0524/110408:ERROR:icu_util.cc(157)] Invalid file descriptor to ICU data received.
[0524/110408:FATAL:icu_util.cc(260)] Check failed: result.
I can't find anything related to this error and this tool over the web. I'm running it in a Win7 x64. I also tryied downloading nwjs manually from github and tryied with x64 and x86 binaries, but they all give me the same error.
Below, the backtrace if needed:
v8::OutputStream::WriteHeapStatsChunk [0x000007FEDA1B7AB1+225089]
std::vector<v8::CpuProfileDeoptFrame,std::allocator<v8::CpuProfileDeoptFrame> >::operator= [0x000007FEDA1640BC+259612]
v8::OutputStream::WriteHeapStatsChunk [0x000007FEDA1F2E58+467688]
v8::Extension::auto_enable [0x000007FEDA1203D1+368897]
v8::Extension::auto_enable [0x000007FEDA11ED19+363081]
ChromeMain [0x000007FED9CE0EB5+133]
GetUploadedReportsImpl [0x000000013F201251+3297]
GetUploadedReportsImpl [0x000000013F20097F+1039]
IsSandboxedProcess [0x000000013F264628+231640]
BaseThreadInitThunk [0x0000000076C359BD+13]
RtlUserThreadStart [0x0000000076E6A2E1+33]
What am I missing here?
I got it to work with the following package.json:
{
"name": "app4",
"version": "1.0.0",
"description": "",
"main": "index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"single-instance": true,
"package": ".json",
"window": {
"title": "app4",
"toolbar": false
}
}
I'm actually not sure what specific property which I removed/added to made it to work. Then I zipped it to package.nw and put in the same folder as nw.exe to run it.