Why does `npm init` Assume NPM Package Build? - npm

This is definitely a theoretical question, but why does running npm init ask a bunch of questions for setting up the fields below?
"name": "my-project-that's-definitely not going to npm",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
A very large percentage of us are using npm just for the package management aspect of it. It seems to me like there should be an option to not set it up as anything BUT a package manager, so just:
"dependencies": {
"#whatever/somepackage": ">=4.0.0-beta <5.0.0",
},
The only justification I can think of is that a lot of people also use npm as a build tool, so this provides an entry point for running scripts. Is that correct? Are there other reasons?
P.S. I know I can use -y flag to default the fields, but that still creates them.

You are correct that npm foremost purpose is a package manager.
And being a package manager, it manages various aspect of a package.
Being a package, it means your code should be able to distribute and reuse by others.
Thus that's why basic information such as name, version, and license are needed.
And npm init is the best place and best time to declare those.
As you mention, you can use npm init -y to use default values so that you don't have to answer them.

Related

Playwright giving "npm ERR! could not determine executable to run" failure

Just started playing with Playwright. Installed and decided to run the tests that come with it. Can't figure out why it is failing.
$ npx playright tests
[11:09:04]
npm ERR! could not determine executable to run
FAIL
I've double check Typescript installed
$ tsc -v
Version 4.7.4
and nom
$ npm -v
8.1.0
I looked up solution on this site, and it mentions removing the .git/hooks file - I don't have that.
package.json is straight forward:
{
"name": "project01",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {},
"author": "",
"license": "ISC",
"devDependencies": {
"#playwright/test": "^1.25.2"
}
}
What on earth am I missing? Did a lot of Googling and still stuck.
Any help would be appreciated.
Base on my experience, npx command was not recognized as executable command.
I tried changing to npm playwright install and it worked.
enter image description here
So, I'm not sure what happened, but when I tried a new project and followed the same steps, everything worked fine.
I guess the takeaway is, if this happens, try re-installing ¯_(ツ)_/¯

Does a published artifact affect npm install?

I have a private, unpublished NPM package. Let's say it's named foo-test:
{
"name": "foo-test",
"version": "0.0.0",
"license": "MIT",
"private": true,
"scripts": {...},
"dependencies": {...}
}
It's not in the NPM registry, and I've marked it private.
I found out via a security audit that this is "vulnerable to dependency confusion attacks", but I don't know how.
If someone later comes along and publishes a real public package called foo-test to the NPM registry, will that affect my local development against my private package?
That is, let's say there's a real package foo-test#1.0.0 available on npmjs. If I run npm install locally against my own unrelated version of foo-test, will there be any side effects arising from the fact that the local package that I'm building has the same name as a public package on the registry?

How to open two seperate nw.js apps

I am building a server and a client nwjs app, but I can't open both at the same time. I wonder if there is any way to do this. I run npm run dev on both of my opened VS Code but when I run this command on the second app it just won't open at all (doesn't matter which one is the second app I would like to run). I tried to build the client app and run it and after it run the server app but it's the same, the second app won't start.
This is my package.json file in both app, I don't know if this helps at all. Only the name is different in the apps (nwjs_client and nwjs_server)
{
"name": "nwjs_server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nw src/",
"prod": "nwbuild --platforms win32,win64,osx64,linux32,linux64 --buildDir dist/ src/"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"nw": "^0.49.1"
}
}
I'm willing to accept any answers, I don't know if it is even possible to run 2 different nwjs apps.
I'm confused by what you are trying to do.
If you are trying to run an NW.js that, instead of loading directly from a index.html, it loads displays a page from a local webserver:
Create an server.js that spins up a local web server on a specific port (like 4263 or something not super common).
If you need any node_modules for this (like express) make sure it is a dependency and not a devDependency.
Set your "main" in the package.json to "http://localhost:4263 using the same port as the server
set your "node-main" to "server.js", this will run in the node context before your window is displayed when starting the app.
Set your "node-remote" to "http://localhost:4263" using the same port also. This will allow Node commands to run on that URL when loaded in NW.js.
If you are wanting to run two commands at the same time you can:
npm install --save-dev concurrently wait-on. This will install two devDeps
Set your npm script to "start": "concurrently \"npm run serve\" \"wait-on http://localhost:4263 && nw .\""
This will run your npm run serve command, which presumably spins up a local webserver for development, if using something like webpack, this could take a minute. Then it waits until localhost:4263 actually returns a response. Then it launches NW.js
concurrently will also let you run any two (or more) commands at the same time.

How to do npm scripts work vs direct commands?

I keep seeing this behavior, so I want to understand it. I install gulp and the gulp-cli (not globally). Typing the gulp command gives me -bash: gulp: command not found
I take the gulp command and drop it into an npm script and boom, it works. Can someone explain what's going on here?
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"gulp": "gulp", // This works!!
"gulp-build": "gulp build" // As does this!!
},
Ahh, that makes sense. I was looking for the relative path to gulp in the project couldn't find the right path. Indeed ./node_modules/.bin/gulp does work. And thank you RobC for quoting that telling line in the docs. Totally makes sense now!

How to install certain dependencies in package.json

I am trying to optimize my CI build step. I want to install certain dependencies before installing others. How can I achieve that?
For example, suppose my package.json file is:
{
"name": "my-project",
"version": "1.0.0",
"author": "krismath",
"dependencies": {
"lib-a": "^1.5.13",
"lib-b": "^0.7.2",
"lib-c": "^15.0.61"
}
I want to install lib-b first in order to use it in other build steps. If I do
npm install lib-b
this doesn't guarantee that the version of lib-b will be semantically compatible with the one in package.json.