When I run npm init, the package name is always scoped with my organization name by default. How to change this? - npm

When I run "npm init" the package name always starts from #hackstrap/..., by default. Instead when I run the command package name should be "webpack-demo". How can I make this happen?

npm config delete scope --global
Running this command helped me solve the issue

Related

What is the meaning of package name before #git in package-lock.json

When I try to run npm install, I got an error:
Could not install from "node_modules/eth-sig-util/ethereumjs-abi#git+https:/github.com/ethereumjs/ethereumjs-abi.git" as it does not contain a package.json file.
So I went to check the diff for package-lock.json, and noticed npm somehow modified
"ethereumjs-abi": "git+https://github.com/ethereumjs/ethereumjs-abi.git",
into
"ethereumjs-abi": "ethereumjs-abi#git+https://github.com/ethereumjs/ethereumjs-abi.git",
which breaks the npm install, so I'm wondering what is the meaning of placing package name before #git and why it breaks the install process.
It works after I delete the ethereumjs-abi# prefix, but it shows up after I run npm install and breaks again...
Thanks in advance!
A #git use to install the package from the git repo.
like: you fork package git repo into your Git account and you change some part of the package now you want to that install that changed package in project you can use #git+'git repo URL of your changed repo'.
here ethereumjs-abi package install from the https://github.com/ethereumjs/ethereumjs-abi.git" git repo:
"ethereumjs-abi": "git+https://github.com/ethereumjs/ethereumjs-abi.git",

How to solve 'vue-cli-service' is not recognized as an internal or external command?

I am getting an error when trying to run npm run serve. At first I installed node.js then vue as well as vue/cli.
But when I am trying to run server as -> npm run serve at that time I'm getting error like 'vue-cli-service' is not recognized as an internal or external command.
I used below codes for installation:
npm install -g vue
npm install -g #vue/cli
can someone guide me what to do to solve this issue ?
I think you are using cmd in windows.
Try deleting the node_modules folder and after that run npm i from the cmd.
Then try running npm run serve again and see if it works this time
Install vue/cli-service globally
npm install #vue/cli-service -g
This will install global npm package.
#vue/cli-service is usully installed as global, because you do not usually copy these types of packages to every project.
If the global npm package gets corrupted, it is not stored in node_modules folder, but rather in other depending on the os. Therefore removing node_modules does not help. Locations for global node_modules folders are
%USERPROFILE%\AppData\Roaming\npm\node_modules (Win10) or
/usr/local/lib/node_modules (Linux),
check this stack overflow post on how to locate global packages.
it will depend on the package manager you are using
delete node_modules
if you are using yarn run yarn or yarn install and then yarn serve
if you are using npm run npm install and then npm run serve
In my case, the package #vue/cli-service is installed in my local node_modules environment, but not my global environment, so it cannot be used as a command. I type .\node_modules\.bin\vue-cli-service serve and it works.
As it is mentioned in terminal that node_modules is missing from your project, so you can't directly use npm run serve, first you have to do npm install then do npm run serve. It will work fine
In my case I ran below commands in GitBash and it worked fine
npm install
npm run serve
If you are using cmd in windows.
deleting the node_modules folder and after that run npm istall from
the cmd.
run npm run serve and see if it works this time
In my case, I have checked the folder of node_modules was missing. I am using Windows. So I run this in cmd.
npm install
npm run serve
Then I check it in localhost like usual.
This issue mostly happens when either #vue/cli is not installed or in most cases,
#vue/cli is already installed and you are currently working on a project and when running
yarn serve or npm run serve.
Most at times, this issue is been caused by broken dependencies.
to fix this issue, simple run
yarn install or npm install
depending on your package manager.
well after trying all the solutions above and it still haven't worked for you then you probably have a stupid space in the full directory of your Vue project like in my case. so remove that that space and it will work from then on.
Remember to set the NODE_ENV=development and run npm install again
Try changing the project path to one without spaces, it worked on windows 10
I had faced the same problem in windows. Then
first I deleted the node_module. then I run npm install.
For Windows you should modify package.json to:
"scripts": {
"serve": "vue-cli-service.cmd serve",
"build": "vue-cli-service.cmd build",
"lint": "vue-cli-service.cmd lint"
}
,
I had the same issue using windows + WSL2 (Ubuntu 20.04). Looking at the logs generated after trying to run npm i I noticed that my WSL2 environment did not have python2 installed. So to solve I ran the following commands:
sudo apt-get install python2
rm -rf node_modules
npm i
npm run serve
I faced the same in Windows. Had to run npm install again. Then it worked perfectly.
Wait, what's the difference between #vue/cli and #vue/cli-service? When you install both, they show different number of packages installed. The latter solved my issue actually but everyone keeps saying install #vue/cli.
try running npm i or npm install and then proceed to run npm i vue after previous installation done. works for me
you need use "npm install" at Command Line
Before running "npm install", try running this command first:
npm set strict-ssl false
Like you, I got the error below when I ran npm run serve from the CMD command line,
'vue-cli-service' is not recognized as an internal or external
command, operable program or batch file.
I got past this familiar error by using the following command to add the npm folder to the PATH that CMD searches for executables:
path=%path%;C:\Users\<USERNAME>\AppData\Roaming\npm
where <USERNAME> is your Windows user profile directory name. Only then was I able to run the following commands successfully:
npm install
npm run serve
What solved the issue for me was renaming the directory. I had used the symbol "&" on the folder name and it seems to break things, so changing it to "and" fixed the issue.
This will probably be an incredibly niche thing, but if I help even 1 person it's fine by me.
I have a project, I can run it well on Linux, but i have the same issue on windows, I solve it this way (I hope in your case it works too):
Delete the node_modules
Install it again with npm i

NPM Init and Start

I'm using npm for a local project and I want to know if I have to use npm init every time I start a session? I think the answer is yes.
If I restart my machine for example, do I have to do npm init? Do I have to do npm install and npm start each time?
Thanks
No, you only have to do npm init when you're first creating a project. It essentially just creates the package.json file (https://docs.npmjs.com/cli/init.html).
And you should only have to run npm install when you first set up a project for local development, or when changes are made to the project's dependencies. So, usually just once, unless you've made changes. (https://docs.npmjs.com/cli/install.html)
npm start is a script that should be defined in your package.json, and you will likely need to run that every time you begin local development on your project.
When you are creating a node project, you need to have package.json. npm init is a convenient way of scaffolding your package.json; you may need to run it everytime you are starting a new project.
npm install, however, installs your dependencies in node_modules folder. You may need to run this everytime you manually add a dependency to your package.json file.
If you need extra information, check here: https://nodesource.com/blog/an-absolute-beginners-guide-to-using-npm/
npm init is to make new modules you dont ever need to run npm init to start a session at all as far as I can tell

react-native: switch from yarn to npm

Is there a step-by-step process to change a react-native project from using yarn as the package manager to using npm? All I can find after several days of searching are instructions to go from npm to yarn and a package called deyarn which doesn't seem to fully work for me. Does anyone have a good resource on this?
Try this :
Remove yarn.lock (don't need this file).
Remove folder node_modules
In package.json, change script use yarn to the same command with npm
Remove all global package of yarn (don't need to remove if you want to use npm for one project)
Remove yarn if you don't want to use it again.
Install npm (if you installed, ignore this step)
Install global and local package you need
Can you upload some error, you said that not fully work.
Edit:
If you want to change npm to yarn, it same:
Remove package-lock.json (don't need this file).
Remove folder node_modules
In package.json, change script uses npm to the same command with yarn
Remove all global package of npm (don't need to remove if you want to use yarn for one project)
Remove npm if you don't want to use it again.
Install yarn (if you installed, ignore this step)
Install global and local package you need
You can see CLI commands comparison for 3rd step
You can try taking the following steps:
Remove node_modules
Run npm install
This should work because npm and yarn use the same package.json.
The deyarn package worked brilliantly for me.
Note that it will only flag (not auto-update) any package-lock.json scripts that you may need to update.
Depending on your environment needs, you may also want to strip out the engines: yarn: '..' entry it adds to your package-lock.json.
You don't need to do anything just run npm start cmd then follow the same step as suggest.
I've covert my yarn project To npm see the image.
enter image description here
enter image description here
hope is work for you.
thanks happy coding.

What is the difference between yarn run and npm start?

Is yarn run intended to be the equivalent of npm start?
It seems yarn run start is the equivalent of npm start, which runs the script inside the start field of the script field in package.json
Few things to understand:
npm: run command is mandatory to execute user defined scripts.
yarn: run command is not mandatory to execute user defined scripts.
start command is not a user defined script name, so you may not need to specify run command to execute it.
So, all the below commands work similar!
npm start
npm run start
yarn start
yarn run start
If you have a user defined script named 'app':
npm app (Does not work!)
npm run app (Works!)
yarn app (Works!)
yarn run app (Works!)
Note: By default start runs node server.js in case not explicitly defined.
npm start is a shortcut for npm run start
Now in terms of running scripts from package.json, all these are equivalent:
npm run start
npm start
yarn run start
yarn start
npm run myscript
npm myscript this is an error
yarn run myscript
yarn myscript
This is because run is not mandatory command for yarn, but it is for npm.
Bonus
npr start - OK
npr myscript - OK
Put this file somewhere in PATH, eg. %localappdata%\Programs\Git\cmd
npr.cmd
npm run %*
yarn run is similar to npm run, they can be used to run the scripts in package.json.
For npm, you can leave out run when run the npm lifecycle scripts (test, start, restart, and stop), but these scripts maybe have extra effect. For example, npm start will run node server.js if the "scripts" object does not define a "start" property in package.json. see the doc npm run. You can't run script with other name leaving out run.
For yarn, you can leave out run for all the scripts in package.json, but if your script name is same as yarn built-in cli commands, the built-in cli commands will have preference over your scripts. doc yarn run.
So the best way to run scripts in package.json: never leave out run.