NPM rimraf - continue executing when folders are missing (stop exit code 1) - javascript

I have an npm script set up to a few synchronous commands. The starting command is npm run clean:install".
Here is the sequence:
"install:all": "npm install && bower install",
"clean": "npm run rimraf -- node_modules doc typings coverage wwwroot bower_components"
"preclean:install": "npm run clean",
"clean:install": "npm set progress=false && npm run install:all"
Works fine if all directories exist. The problem is if any of the directories are already deleted, the script exists with code 1 and prevents all additional syncronous scripts from running.
So that means it failes to run the original command of clean:install which is " "clean:install": "npm set progress=false && npm run install:all"
Error from npm:
npm ERR! angular2-webpack-starter#5.0.4 clean: `npm cache clean && npm run
rimraf -- node_modules doc typings coverage wwwroot bower_components`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the angular2-webpack-starter#5.0.4 clean script 'npm cache clean && npm run rimraf -- node_modules doc typings coverage wwwroot bower_components'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the angular2-webpack-starter package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! npm cache clean && npm run rimraf -- node_modules doc typings coverage wwwroot bower_components
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs angular2-webpack-starter
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls angular2-webpack-starter
npm ERR! There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
The last line gives it away. It wants node_modules but can't find it so it fails.
How can I make my script continue executing to ignore these failures (missing directories)?
EDIT: The error occurs because rimraf is removing node_modules and wiping itself out. Once the folder it cleared, rimraf is gone and exit code 1. Can I exclude the rimraf folder within node_modules so it exists correctly?

I am (probably) looking at this boilerplate project you're using. Did you read the instructions? They are asking you to run npm install typings webpack-dev-server rimraf webpack -g, which includes installing rimraf globally, before even using the project. If you've failed to do this, using the npm run clean:install, or anything which calls this script, will fail because the rimraf module can't be found in your local node_modules (if already removed) or it will be deleting itself.

The solution was a bit tricky. The problem was because rimraf was removing itself since it was only installed locally in node_modules. So as soon as it removed the node_modules dir, the program died.
I realized it was necessary to add a task to have it install rimraf globally. The first task that executes is install:all. Then rimraf is in the global PATH. However, rimraf was a dependency for many other node_modules, so it kept getting installed locally and therefore ignored the global PATH. It kept dying because it was still referencing the local node_modules folder.
The trick was to uninstall rimraf locally right before executing any rimraf command. Now it's forced to use the global PATH because it doesn't exist locally.
"preinstall:all": "npm install typings webpack-dev-server rimraf webpack -g",
"install:all": "npm install && bower install",
"prerimraf": "npm uninstall rimraf",
"rimraf": "rimraf",
"clean": "npm run rimraf -- node_modules doc typings coverage wwwroot bower_components"
"preclean:install": "npm run clean",
"clean:install": "npm set progress=false && npm run install:all"
After install:all has been executed on project load (in Visual Studio 2015, Task Runner Explorer has a binding called Project Open), there are times I want to clean everything and start fresh. Now I can simply execute clean:install and uninstall node_modules and reinstall immediately with no problem! Exit code 0. Sweet!

Related

Babel CLI --out-dir runs into permissions errors on second compile

I'm running into a strange error that seems to be giving me permissions issues on Windows 10. I'm using the Babel CLI to compile some ES6 code, which works great using --out-file, which will overwrite files just fine, by --out-dir doesn't. Here's the relevant parts of my package.json:
{
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-minify": "^0.2.0",
"babel-preset-env": "^1.6.0"
},
"scripts": {
"build-components": "babel Scripts/Components --out-file Scripts/Compiled/components.js",
"build-pages": "babel Scripts/Pages --out-dir Scripts/Compiled/Pages",
"watch-components": "babel Scripts/Components -w --out-file Scripts/Compiled/components.js",
"watch-pages": "babel Scripts/Pages -w --out-dir Scripts/Compiled/Pages",
"build-js": "npm run build-components && npm run build-pages"
}
}
I'm building this within Visual Studio and using the Task Runner Explorer package to run my commands (though it still fails from PowerShell, or running it as a build command within the project). The plugin runs 'npm run build-js' on build and 'npm run watch-components' and 'npm run watch-pages' afterwards to watch for any changes. 'npm run watch-pages' fails every time, as does 'npm run build-pages' if the files it outputs to already exist. The only solution I've found so far is to delete the 'Scripts/Compiled/Pages' folder every time I make a change to a file within 'Scripts/Pages', which defeats the purpose of the watch command and is generally just annoying to have to do every time, especially when I'm doing a lot of file changes. Both build commands for the components works every time and will overwrite the old file without issue.
> babel Scripts/Pages --out-dir Scripts/Compiled/Pages
Error: EPERM: operation not permitted, open 'D:\Projects\PasswordReset\Trunk\STS.PasswordReset.Web\Scripts\Compiled\Pages\Index.Vue.js'
at Error (native)
at Object.fs.openSync (fs.js:584:18)
at fs.writeFileSync (fs.js:1224:33)
at outputFileSync (D:\Projects\PasswordReset\Trunk\STS.PasswordReset.Web\node_modules\output-file-sync\index.js:45:3)
at write (D:\Projects\PasswordReset\Trunk\STS.PasswordReset.Web\node_modules\babel-cli\lib\babel\dir.js:33:5)
at handleFile (D:\Projects\PasswordReset\Trunk\STS.PasswordReset.Web\node_modules\babel-cli\lib\babel\dir.js:43:7)
at D:\Projects\PasswordReset\Trunk\STS.PasswordReset.Web\node_modules\babel-cli\lib\babel\dir.js:61:9
at Array.forEach (native)
at handle (D:\Projects\PasswordReset\Trunk\STS.PasswordReset.Web\node_modules\babel-cli\lib\babel\dir.js:59:29)
at Array.forEach (native)
npm ERR! Windows_NT 10.0.15063
npm ERR! argv "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\Web\\External\\Node.exe" "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\Web\\External\\node_modules\\npm\\bin\\npm-cli.js" "run" "build-pages" "--color=always"
npm ERR! node v5.4.1
npm ERR! npm v3.3.4
npm ERR! code ELIFECYCLE
npm ERR! STS-VueApp#1.0.0 build-pages: `babel Scripts/Pages --out-dir Scripts/Compiled/Pages`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the STS-VueApp#1.0.0 build-pages script 'babel Scripts/Pages --out-dir Scripts/Compiled/Pages'.
npm ERR! This is most likely a problem with the STS-VueApp package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! babel Scripts/Pages --out-dir Scripts/Compiled/Pages
npm ERR! You can get their info via:
npm ERR! npm owner ls STS-VueApp
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! D:\Projects\PasswordReset\Trunk\STS.PasswordReset.Web\npm-debug.log
Process terminated with code 1.
I thought maybe it was something having to do with the files not being included in my project, because I get a similar error from the component builds if my 'Scripts/Compiled' folder isn't included in the project, but including 'Scripts/Compiled/Pages' still gives me the same error and including the actual output file, ie: 'Scripts/Compiled/Pages/Index.js' doesn't fix it either.
Any ideas?? :\
It's only my thoughts. But, can you please double check:
do you run VS as Administrator?
do you run Task Runner Explorer as Administrator?
Make sure that both works under Administrator.
I'm running into something similar and the problem looks to be that babel is moving over the read only attrib on files that are under source control from VSS, i.e you pull a file from VSS it comes down with read only. The watch on it transpiles it to your target folder with read only on it. Subsequent transpiles fail as the target file won't get overwritten when it's set to read only.
I'm still working on the right solution to this but at the moment I have hacked up temp fix via a task runner command that removes read only on the entire target folder.
"babelJsxTranspileInit": "attrib -r [targetFolder] /s",

'NODE_ENV' is not recognized as an internal or external command

I have an app which is working perfectly on linux but when I execute it on windows I get this error.
'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.
'NODE_ENV' is not recognized as an internal or external command,operable program or batch file.
npm
ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! aaa#1.0.0 webpack-watch: `NODE_ENV='debug' webpack --progress -d -colors --watch`
npm ERR! Exit status 1
npm ERR!
I have tried this in cmd.
SET NODE_ENV=debug
which I found here “NODE_ENV” is not recognized as an internal or external command
And here is snippet of code package.json where I guess is my mistake.
"webpack-watch": "NODE_ENV='debug' webpack --progress -d --colors --watch",
"server-watch": "NODE_ENV='debug' nodemon backend/server.js"
Windows doesn't understand the syntax var=value cmd1 arg1. You need to adapt the NPM run tasks to use Windows' syntax:
"webpack-watch-win": "set NODE_ENV=debug & webpack ..."
"server-watch-win": "set NODE_ENV=debug & nodemon ..."
....but there are probably more errors you will hit downstream. This should at least get you past the environment variable declaration, though. Node is strongly rooted in *nix shells, not cmd.exe.
Setup your NODE_ENV with export. So in your package.json add in your scripts block
"scripts":{
"webpack-watch": "export NODE_ENV='debug' && webpack --progress -d --colors --watch",
"server-watch": "export NODE_ENV='debug' && nodemon backend/server.js"
}

babel-cli issues with npm

I am using babel to build a part of my library and I'm running into issues when i run babel commands through npm.
I have an npm script called build that calls:
{
"prebuild": "rm -rf && mkdir dist",
"build": "babel src/index.js -o dist/index.js"
}
I have run the actual babel command itself in my command line and it works.
However when I do npm run build from my command line it says
The CLI has been moved into the package 'babel-cli'
npm also says that it is that specific line that is failing.
I have already tried the following:
Restart my terminal.
Install babel v5 because I read that v6 split a lot of its functionality.
npm rebuild.
Delete my node_modules and npm install
Any other ideas? as to why npm fails at running this command?
Install babel#5 globally (npm install babel#5 --global) as well as locally: npm install babel#5 --save-dev

npm package.json scripts not being called

I have the following scripts section in my projects package.json:
"scripts": {
"seed": "node bin/seed",
"test": "echo \"Error: no test specified\" && exit 1"
},
If i run $ npm test I get this:
>npm test
> node-mongo-seeds#0.0.1 test C:\Users\m089269\WebstormProjects\node-mongo-seeds
> echo "Error: no test specified" && exit 1
"Error: no test specified"
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0
If i run $ npm seed, I get this:
npm seed
Usage: npm <command>
where <command> is one of:
add-user, adduser, apihelp, author, bin, bugs, c, cache,
completion, config, ddp, dedupe, deprecate, docs, edit,
explore, faq, find, find-dupes, get, help, help-search,
home, i, info, init, install, isntall, issues, la, link,
list, ll, ln, login, ls, outdated, owner, pack, prefix,
prune, publish, r, rb, rebuild, remove, repo, restart, rm,
root, run-script, s, se, search, set, show, shrinkwrap,
star, stars, start, stop, submodule, tag, test, tst, un,
uninstall, unlink, unpublish, unstar, up, update, v,
version, view, whoami
npm <cmd> -h quick help on <cmd>
npm -l display full usage info
npm faq commonly asked questions
npm help <term> search for help on <term>
npm help npm involved overview
Specify configs in the ini-formatted file:
C:\Users\m089269\.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config
npm#1.4.3 C:\Program Files\nodejs\node_modules\npm
Why does it recognize my test script but not my seed script?
EDIT
When I try npm run-script seed I get this error which is expected because I don't pass in a -d param:
$ npm run-script seed
> node-mongo-seeds#0.0.1 seed C:\Users\m089269\WebstormProjects\node-mongo-seeds
> node bin/seed
Populate mongo from a set of .json files.
Usage: $ node seed
Options:
-d The path to your mongo db [required]
Missing required arguments: d
npm ERR! node-mongo-seeds#0.0.1 seed: `node bin/seed`
npm ERR! Exit status 1
...
When I try npm run-script seed -d "localhost/ease" I get this error.
npm run-script seed -d localhost/ease-dev
npm info it worked if it ends with ok
npm info using npm#1.4.3
npm info using node#v0.10.26
npm ERR! Error: ENOENT, open 'C:\Users\m089269\WebstormProjects\node-mongo-seeds\node_modules\seed\package.json'
...
Why is it looking for a package.json in node_modules\seed? Seed is not even a dependency.
From the documentation:
npm supports the "scripts" member of the package.json script, for the following scripts:
prepublish: Run BEFORE the package is published. (Also run on local npm install without any arguments.)
prepare: Run both BEFORE the package is packed and published, on local npm install without any arguments, and when installing git dependencies (See below). This is run AFTER prepublish, but BEFORE prepublishOnly.
prepublishOnly: Run BEFORE the package is prepared and packed, ONLY on npm publish.
prepack: run BEFORE a tarball is packed (on npm pack, npm publish, and when installing git dependencies).
postpack: Run AFTER the tarball has been generated and moved to its final destination.
publish, postpublish: Run AFTER the package is published.
preinstall: Run BEFORE the package is installed
install, postinstall: Run AFTER the package is installed.
preuninstall, uninstall: Run BEFORE the package is uninstalled.
postuninstall: Run AFTER the package is uninstalled.
preupdate: Run BEFORE the package is updated with the update command.
update, postupdate: Run AFTER the package is updated with the update command.
pretest, test, posttest: Run by the npm test command.
prestop, stop, poststop: Run by the npm stop command.
prestart, start, poststart: Run by the npm start command.
prerestart, restart, postrestart: Run by the npm restart command. Note: npm restart will run the stop and start scripts if no restart script is provided.
Additionally, arbitrary scripts can be run by doing npm run-script <stage> <pkg>.
You can see the reason why your npm test script works is because npm test is a built-in command. You must use npm run-script if you want to execute a script that is not executed by a built-in npm command.
Custom scripts declared in the package.json can be ran with the npm run <your-script> form in your shell.
Try npm run seed or npm run test
To execute the custom scripts in package.json using below
npm run-script seed
or
npm run-script < custom script >
or you can use
npm run < custom script >

npm install error from the terminal

I am trying to install node in my mac..
i am getting the following error...
i downloaded the node from node site and ran that package...
can you guys tell me why i am facing that errror..when i do npm install
MacBook-Pro:~ Raj$ npm install
npm ERR! install Couldn't read dependencies
npm ERR! package.json ENOENT, open '/Users/Raj/package.json'
npm ERR! package.json This is most likely not a problem with npm itself.
npm ERR! package.json npm can't find a package.json file in your current directory.
npm ERR! System Darwin 13.0.0
npm ERR! command "node" "/usr/local/bin/npm" "install"
npm ERR! cwd /Users/Raj
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! path /Users/Raj/package.json
npm ERR! code ENOPACKAGEJSON
npm ERR! errno 34
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Users/Raj/npm-debug.log
npm ERR! not ok code 0
Running just "npm install" will look for dependencies listed in your package.json. The error you're getting says that you don't have a package.json file set up (or you're in the wrong directory).
If you're trying to install a specific package, you should use 'npm install {package name}'. See here for more info about the command.
Otherwise, you'll need to create a package.json file for your dependencies or go to the right directory and then run 'npm install'.
I had this problem when trying to run 'npm install' in a Terminal window which had been opened before installing Node.js.
Opening a new Terminal window (i.e. bash session) worked. (Presumably this provided the correct environment variables for npm to run correctly.)
In my case it was due to a bad URL (http:// instead of git://, no .git at the end) for one of the dependencies.
You're likely not in the node directory. Try switching to the directory that you unpacked node to and try running the command there.
In case it helps anyone else - my issue was a rookie error, I had a space in the name line of my package.json and it caused the dependencies to be unreadable.
I came across this, and my issue was using an older version of node (3.X), when a newer version was required.
The error message actually suggested this as well:
...
Make sure you have the latest version of node.js and npm installed
...
So the solution may be as simple as upgrading node/npm. You can easily do this using nvm, the "Node Version Manager"
After you've installed nvm, you can install and use the latest version of node by simply running this command:
nvm install node
For example:
$ nvm install node
Downloading https://nodejs.org/dist/v8.2.1/node-v8.2.1-darwin-x64.tar.xz...
######################################################################## 100.0%
Now using node v8.2.1 (npm v5.3.0)
$ node --version
v8.2.1
In mac you might have downloaded and installed Node js in
/Users/yourusername/Downloads/nodejs-todo-master , so go here and run npm install command, no need of sudo as well., you should get output like this...
underscore#1.4.4 node_modules/underscore
ejs#0.8.8 node_modules/ejs
redis#0.8.6 node_modules/redis
jasmine-node#1.0.28 node_modules/jasmine-node
├── walkdir#0.0.7
├── coffee-script#1.8.0 (mkdirp#0.3.5)
├── requirejs#2.1.15
└── jasmine-reporters#1.0.1 (mkdirp#0.3.5)
express#3.0.6 node_modules/express
├── methods#0.0.1
├── fresh#0.1.0
├── range-parser#0.0.4
├── cookie-signature#0.0.1
├── buffer-crc32#0.1.1
├── cookie#0.0.5
├── commander#0.6.1
├── mkdirp#0.3.3
├── debug#2.1.0 (ms#0.6.2)
├── send#0.1.0 (mime#1.2.6)
└── connect#2.7.2 (pause#0.0.1, bytes#0.1.0, formidable#1.0.11, qs#0.5.1)
First download json package file from https://github.com/npm/read-package-json
and then run npm install from terminal.
This is all because you are not in the desired directory. You need to first get into the desired directory. Mine was angular-phonecat directory. So I typed in cd angular-phonecat and then npm install.
If someone is in my situation facing this error and have tried all the above solutions, like:
you are in the right directory
you have a package.json file,
the JSON is valid,
you have tried to run %temp%
you have tried " npm install -d --save"
etc.
Mine worked by doing "npm install --force"
Note: This was also recommended in the error itself, which I didn't pay attention to earlier.
Even " Yarn install" worked.
npm install -d --save worked for me. -d flag command force npm to install your dependencies and --save will save the all updated dependencies in your package.json
For me I'm on windows 10 X64...
My code npm install on cmd failed
So instead of npm i used Yarn
Just type yarn install instead of npm install
This fixed my problem.Tried for 2 days finally found the best
solution
To install yarn , on cmd enter the following code
npm install --global yarn
To check if it has installed correctly enter the following code
yarn --version
Hey if you found error and it stuck while installing then try this
Open run and type %Temp% and delete all file
Then type prefetch on run app an delete all files then try it
These Will do the Job
npm install -g yarn
yarn install
or
npm install --force
Hey if you found error and stcuk while installing packages
,getting only three files like json file ,lock file and module file using yarn then try this using yarn.
Open run and type %Temp% and delete all file
Then type prefetch on run app an delete all files
Then type on CMD npx create -react-app it will give you all packages

Categories

Resources