i tring to create a personal global cli in nodejs. after read differents tutorial, i have tried to do it but not have console log printed in my terminal.
i have created a project npm with this package.json
{
"name": "npm-versioning",
"version": "1.0.0",
"description": "",
"main": "index.js",
"preferGlobal": true,
"bin": {
"vf-release": "./bin/cmd.js"
},
"scripts": {
"pretest": "node ./bin/cmd.js",
"test": "echo \"TEST ROOT\"",
"posttest": "vf-release",
},
}
this is the project tree
how can you see in my package.json i have created a bin command called "vf-release", this is connected al file js "bin/cmd.js"
cmd.js
#!/usr/bin/env node
console.log("AAA");
console.log("BBB");
the cli file is very easy. but now the problems,I premise that i work in window, but the same fail is in my "WSL Ubuntu machine" (so, is not a problem of windows!)
if i run "npm run test" on windows or unix i have always the same result
"C:\Program Files\nodejs\npm.cmd" test
npm-versioning#1.0.0 pretest C:\xampp\htdocs\personal\npm-versioning
node ./bin/cmd.js
AAA
BBB
npm-versioning#1.0.0 test C:\xampp\htdocs\personal\npm-versioning
echo "TEST ROOT"
"TEST ROOT"
npm-versioning#1.0.0 posttest C:\xampp\htdocs\personal\npm-versioning
vf-release
Process finished with exit code 0
pretest work (nodejs + js file)
test not matter
posttest use global cli but not write nothing!!
also if i launch the command (linkedin global npm directory) nothing happen.
which is my error?
SOLUTION FIRST STEP: install -g
npm install -g .
C:\Program Files\nodejs\vf-release -> C:\Program Files\nodejs\node_modules\npm-versioning\bin\cmd.js
npm-versioning#1.0.0
updated 1 package in 1.176s
now, if i use "vf-release" in my console i see:
vf-release
AAA
BBB
ok, but if i use a "npm run version" in another app nothing appen!, for example
app-another (package.json)
"scripts": {
"version": "vf-release"
},
the script write :
npm run version
test-app#1.0.0 version C:\xampp\htdocs\personal\npm-test-app
vf-release
why npm run somecommand work inside project, but not work in other run app?
Related
I'm struggling with multiple npm packages in a root git repository with custom dev scripts to handle launch, compile, build and so on. Now I came across npm workspaces and wanted to use this stunning new feature in my following project structure but I can't get it to work:
projectx (root)
- package.json
- apps
-- backend
-- src
-- package.json (name: #projectx/backend, scripts: "dev":"ts-node or whatever")
-- common
-- src
-- package.json (name: #projectx/common)
-- frontend
-- src
-- package.json (name: #projectx/frontend, scripts: "dev":"webpack")
My root package.json contains:
{
"name": "packagex",
"version": "1.0.0",
"description": "",
"main": "index.js",
"private": "true",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"back:dev": "npm workspace #projectx/backend dev",
"front:dev": "npm workspace #projectx/frontend dev",
"dev": "run-p back:dev front:dev"
},
"workspaces": [
"apps/*"
],
"repository": {
"type": "git",
"url": "git_url"
},
"author": "me",
"license": "ISC",
"devDependencies": {
"npm-run-all": "^4.1.5"
}
}
And now I want to start backend and frontend with npm-run-all and the command on root: npm run dev which results in:
And I also want to share the common package with backend and frontend, which should be possible in this case. Maybe anobody else is facing the same problem or has some ideas what I am doing wrong here.
npm#7.7.0 added a way to call scripts from child packages/workspaces, here are some examples based on your original:
Running a script named "dev" in all workspaces located under apps/backend:
npm run dev -w apps/backend
Running a script named "dev" in all workspaces:
npm run dev --ws
Running a script named "dev" in a package named #projectx/frontend:
npm run dev -w #projectx/frontend
More info:
Related CHANGELOG entry: https://github.com/npm/cli/releases/tag/v7.7.0
Docs: https://docs.npmjs.com/cli/v7/commands/npm-run-script#workspaces-support
Blog post: https://dev.to/ruyadorno/npm-workspaces-npm-run-and-exec-1lg0
Your "workspaces" property in package.json looks right. I'm using NPM Workspaces and it's working well, but it's still missing a lot of features so you need to wire things up yourself. I also don't think npm worksace is a command (but maybe for the future?), so here's a checklist to get it to work:
Make sure you're using Node 15+ and NPM 7+
Set all package.json to "private": true,
Delete all package-lock.json inside of your project, go to the root, then npm install. It should generate one root level package-lock.json that contains all dependencies for your workspaces
Since you're using npm-run-all, add this to your scripts:
"scripts": {
"back:dev": "cd apps/backend && npm run dev",
"front:dev": "cd apps/fontend && npm run dev",
"dev": "npm-run-all build --parallel back:dev front:dev"
}
Then start it with npm run dev.
Note, you may want to consider using start scripts instead of dev to shorten the command you need to type (e.g. npm start instead of npm run dev), but npm run dev will still be fine.
In root package.json you can also add short name for each package:
"scripts": {
"api": "npm --workspace=#app/api run",
}
#app/api is a name in package.json
And run scripts in ./packages/api folder from root like so:
npm run api lint
npm run api dev
I think you wish to:
keep scripts and dependencies separate (thus the 4 package.json files), for ease of maintenance
May I suggest a work-around without workspaces that might do what you're after:
{
...
"scripts": {
"//back:dev": "npm workspace #projectx/backend dev",
"back:dev": "npm --prefix apps/backend dev",
"//front:dev": "npm workspace #projectx/frontend dev",
"front:dev": "npm --prefix apps/frontend dev",
"dev": "run-p back:dev front:dev"
},
"//workspaces": [
"apps/*"
],
"devDependencies": {
"#local/back": "file:apps/backend",
"#local/front": "file:apps/frontend",
"npm-run-all": "^4.1.5"
}
}
The npm --prefix runs npm scripts in another folder than the current one.
The #local/back dependencies are not necessary for that, but I've found such useful if eg. a package depends on another. You might use that trick to reach for the common by:
"dependencies": {
"#local/common": "file:../common"
}
I wished a week ago that workspaces would offer a better solution, but didn't find any benefit over the above mechanisms.
I would also like workspaces to:
only expose those files in the files entry of the particular package.json (now, all are shown)
only allow import to paths in the exports of the particular package.json, if it has one
See
NPM Workspaces monorepo - share local package's distribution folder as root instead of the entire source files
(having node version 12 and npm version 6)
Backend: Node.js
Front: React.js
i cloned repository
cd into the directory
ran
npm install (installing dependencies etc...)
and when i ran
npm start
and i get
[Ben#Mac:~/Desktop/test]$ npm start
> answers-entry-level-exam#1.0.0 start /Users/Ben/Desktop/test
> lerna run start --parallel
lerna notice cli v3.22.1
lerna info Executing command in 1 package: "npm run start"
#ans-exam/server: > #ans-exam/server#1.0.0 start
/Users/Ben/Desktop/test/server
#ans-exam/server: > ts-node-dev index.ts
#ans-exam/server: Using ts-node version 8.5.2, typescript version 3.7.2
#ans-exam/server: server running 3232
which seems like the server is running okay
but localhost:3000 cant be reached
not opening anything
the first time it did open and there was a MacOS popup on the right side of the screen related to node ( i think that is the issue but cant figure out how to fix)
my package.json:
{
"name": "answers-entry-level-exam",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"b": "npm run bootstrap",
"bootstrap": "lerna bootstrap",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "lerna run start --parallel",
"postinstall": "npm run bootstrap"
},
"author": "",
"license": "ISC",
"devDependencies": {
"lerna": "^3.22.1"
}
}
Thanks!
I cloned again the repo and it fixed that
Cheers to all those who tried to help :)
Try localhost:3232. I see in your output server running 3232
Actually I'm trying to deploy my app to Heroku(it is not about a problem with Heroku). Previously I was running the script on my local machine by just running app.js (script name). but now I have to modify my packege.json by adding "start":"node src/app.js" script. But whenever I run the command (npm run start) it generates the error.
I have tried "start":"node app.js". I have checked the location of the script correctly it is in the src folder but still is not working.
{
"name": "src",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node src/app.js" // here I declare the start
},
This script has to start the server
You should just need to run npm start.
I have a basic App created using npm init -y. In package.json I have a main entry which points to server.js.
{
"name": "rest-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prestart": "SET NODE_ENV=dev"
}
I am trying to set the NODE_ENV variable in prestart and let npm to call main to invoke npm start. But environment variable set in the prestart is not carry forwarded and is undefined. When I run 'npm start', console outputs that both commands are executed in order.
PS D:\test\RestAPI> npm start
> rest-api#1.0.0 prestart D:\test\RestAPI
> set NODE_ENV=dev
> rest-api#1.0.0 start D:\test\RestAPI
> node server.js
undefined
[undefined] Listening on http://localhost:3000
but when I print the variable from the app, it is undefined. Is there anything that I am doing wrong here, or is this how it is supposed to behave? Is there a way to invoke and set env variable using 'SET NODE_ENV=dev' without chaining it to 'node server.js'
When I combine both in the 'start' as below, then the environment variable is set.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "set NODE_ENV=dev && node server.js"
}
I am testing this on Windows 10, npm version 3.10.10. Appreciate your help.
I know how this can be done in package.json using 'start'. This question is specific to how this can be achieved through 'prestart'.
Thanks.
The short answer is NOT possible.
1. Why :
this is not be possible because each script executed by different processes that npm spawns for this purpose which has its own environment variables.
To realise that, create test project and configure both scripts to be like
"start": "pause&&set VAR1",
"prestart" : "pause&&set VAR1=value&&set VAR1&&pause",
On windows open the task manager and pay close attention how many cmd process es are listed before running the script.
run the command "npm start" and at each request "press any key to continue..." just notice how processes created are created. I attached screenshots for this in order
2. Unless :
you change how npm executes different scripts to use one cmd for all the scripts which I think is complicated and probably will create bugs.
If you want to chain scripts and add env variables along the way then checkout the example given in the cross-env package:
{
"scripts": {
"parentScript": "cross-env GREET=\"Joe\" npm run childScript",
"childScript": "cross-env-shell \"echo Hello $GREET\""
}
}
If I install http-server locally (without -g flag) how can I run it with local directory as root directory?
In my root project directory I have a node_modules folder, If I want to execute http-server I need to execute $node ./node_modules/http-server/bin/http-server.
I wish to launch http-server with a command like $npm http-server.... If I use $npm start http-server, the server start without ./ as root.
You can call the executable that sits in the module's bin folder like so:
> ./node_modules/http-server/bin/http-server
If you want to do it with an npm command, you can put that in your package.json's scripts collection:
{
"name": "tmp",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start":"./node_modules/http-server/bin/http-server",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Then you can just run
> npm start