npx <package> runs on command line but not in package.json scripts section - node.js

I have a package installed in node using
npm i mypackage --save-dev
and it works fine on the command line with
npx mypackage --options
but when I want to have it in the package.json files scripts section it it fails. I tried
"scripts": {
"mystuff": "mypackage --options",
"mystuffnpm": "npm run mypackage --options",
"mystuffnpx": "npx mypackage --options"
}
but nothing works. What is the right way to get it in the scripts section running?

After integrating any package without command try "npx run".It will
install the missing packages in node modules.
Make sure you are running the command in the specific directory.
Try "npm init -y" to make new package.json file and then install the
packages.

Related

having some EPERM not permitted [duplicate]

I am new to Next.js I am using this command to create my app:
npx create-next-app
but it is giving an error which is:
Error: EPERM: operation not permitted, mkdir 'C:\Users\danyyal'
command not found: create-next-app
how to create the app? p.s I have nodejs installed.
First install create-next-app globally by doing
npm i -g create-next-app
After you can use create-next-app CLI to create next app.
If you want to create new next app in current directory do
create-next-app .
For a named next app just do
create-next-app your-app-name
Before create it manuely with
npm install react next react-router
Try to install the create-next-app package
npm i create-next-app
Then you can do again:
npx create-next-app my-awesome-app
You can always setup the project manually.
Step 1 :
Install next, react and react-dom in your project:
npm install next react react-dom
Step 2 :
Open package.json and add the following scripts:
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
Step 3: Populate ./pages/index.js with the following contents:
function HomePage() {
return <div>Welcome to Next.js!</div>
}
export default HomePage
To start developing your application run npm run dev or yarn dev. This starts the development server on http://localhost:3000.
UPDATE
Since you needed more help.
Create a new folder and name it as you like,then open your terminal & go into the project folder and run the step 1 commands. Now open package.json file and inside that write the step 2 code. Now create pages folder inside the current project directory and inside that create a index.js file and write the step 3 code. Now run the project by doing npm run dev.
So the project structure will be like this.
--Nextjs Project
---pages
|--- index.js
---packages.json
Try running npm i create-next-app first.
You can create it manually by creating a folder and run npm init to create a package.json file the run npm install next react react-dom --save, Open the package.json and add this
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
You must use node 10.13 or later to make it work !
NextJS Requirements
1. nvm use 10
2. npx create-next-app
Try installing it globally first, using the command
npm install -g create-react-app
And then, you can create your app using the command,
npx create-react-app
First, install npx globally (if not)
npm i -g npx
Then, run command create-next-app
npm i create-next-app
then you will be able to create-next-app by running
npx create-next-app appname
first installed npx globally using -g
npx i -g npx
then installed
npm i -g create-next-app

can i install a package locally and globally at same time in node.js?

I am working on a project. I have worked with nodemon that is installed globally for development.
I edited my package.json file to add a script to automatically run a nodemon script - as shown below:
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon -e js,pug"
}
Now when another developer runs:
npm start dev
they will surely get a error if they have not installed the nodemon module.
I know that the solution is to install nodemon locally as a development dependency.
Is it possible to work around this problem without installing it locally?
Can I install nodemon both locally and globally at the same time?
simply install it globally and you can use it in any of your project
command :
npm i -g nodemon
now you don't need to install it locally at all to make it work on your project.

Run npm scripts using local deps

Currently I run npm scripts using local deps this way:
package.json:
"scripts": {
"test": "node ./node_modules/karma/bin/karma start",
"node-test": "node ./node_modules/jasmine/bin/jasmine",
"build": "node ./node_modules/gulp/bin/gulp build"
},
I don't want to use global deps, since I can forgot to add deps to the package.json. This way when a local dep is missing, then I got an error message and I don't have problems because some deps are not installed globally, e.g. karma plugins.
Is there a better (shorter) way to define npm scripts using the local libs? Is this travis compatible?
edit:
If it wasn't obvious I have the same libs installed globally, but I want to use the local installs by these projects. That means when I start karma with karma start then the globally installed version will start the karma server, which means that if I don't have all of the karma plugins globally installed, then I got error.
Another problem that I have windows, so the solutions described here: How to use package installed locally in node_modules? do not work. Windows does not recognize the #!/bin/sh and the #!/usr/bin/env node head sections and there is no sh command as far as I can tell. At least not in webstorm terminal. Git bash has the sh command, but I want to run these npm scripts from webstorm terminal.
One possible solution could be to fix somehow webstorm so it could use sh from terminal. After that I could use $(npm bin) I assume. But that's just a guess. I am not sure whether this can be done.
npm automatically puts prepends the path ./node_modules/.bin to your PATH env before it executes commands run by using npm run (including the two "magic" shortcuts npm start and npm test)
npm scripts docs
You can just set this up with:
"scripts": {
"test": "karma start",
"node-test": "jasmine",
"build": "gulp build"
}
Assuming that you have karma, jasmine and gulp-cli listed in either your devDependencies or dependencies (so that they're install when doing npm install)
And yes, it is travis-compatible. Here is an example of a package that is tested on travis using tap which is installed locally as a module:
https://github.com/scriptoLLC/couchdown/

How does "npm" run "npm test"?

I always thought that npm test command just launches what I would write in package.json inside scripts: { test: ...} section. But I have this weird bug when it doesn't work.
So, I have this piece of config in package.json
"scripts": {
"start": "node index.js",
"test": "mocha tests/spec.js"
}
When I try to run tests I type npm test in terminal and had this error:
module.js:340
throw err;
^
Error: Cannot find module 'commander'
But everything is OK when I type just mocha tests/spec.js. Any ideas why is that?
UPDATE:
I've tried to install commander and I had an error Cannot find module 'glob'. After installing glob I have
Error: Cannot find module '../'**
But actually question is why do I have these errors and why is everything OK when running mocha tests/spec.js?
You may have two versions of mocha installed: one globally (npm install -g mocha) and one locally, which appears to be broken.
When you run a script through npm, either as npm run-script <name> or with a defined shortcut like npm test or npm start, your current package directory's bin directory is placed at the front of your path. For your package that's probably ./node_modules/.bin/, which contains a link to your package's mocha executable script.
You can probably fix this by removing the local mocha and reinstalling it with --save-dev:
rm -rf node_modules/mocha
npm install --save-dev mocha
That should get you a working local copy of mocha with all its dependencies (commander etc.) installed.

How can I invoke npm on heroku command line (to install bower components)?

Bower is for client side Javascript what npm is for the server side and reads a component.json file to recognize dependencies that should be fetched at deploy time so I'd be happy it heroku would run it at slug compilation time.
Unfortunately I can not invoke npm or bower from a heroku console or one-off command (heroku run "npm help") (heroku run bash -> npm help) as it's possible with ruby's rake. I've put npm and node (latest/x versions) in my package.json but in the engines section, not the dependencies.
I think this could be solved by customizing the node buildpack but I consider this a little too heavy task just for activating something so obvious.
You can also setup a postintall command, something like this in your package.json
"dependencies": {
"bower": "0.6.x"
},
"scripts": {
"postinstall": "./node_modules/bower/bin/bower install"
}
Then npm install will also install bower dependencies.
Pros : one command to rule them all.
Cons : you unnecessarily embed bower as a dependency.
You can use run like this:
heroku run npm install git://github.com/webjay/kaiseki
You should declare NPM dependencies in the package.json file
All you install from shell will be deleted on exit shell. You are in a cloned instance.
You can use bower directly like this
"dependencies": {
"bower": "^1.7.9"
},
"scripts": {
"postinstall": "sudo bower install --allow-root "
}

Resources