Running babel-cli from npm script not working - node.js

I followed the directions here to install babel-cli. I added "build": "babel src -d lib" to my package.json in the directory I want to run it in. However, on running, I get this error:
🐕 npm run build
> ipfs-readme-standard#1.0.0 build /Users/richard/src/ipfs-readme-standard
> babel src -d lib
src doesn't exist
npm ERR! Darwin 14.5.0
npm ERR! argv "/Users/richard/.nvm/versions/node/v5.0.0/bin/node" "/Users/richard/.nvm/versions/node/v5.0.0/bin/npm" "run" "build"
npm ERR! node v5.0.0
npm ERR! npm v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! ipfs-readme-standard#1.0.0 build: `babel src -d lib`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the ipfs-readme-standard#1.0.0 build script 'babel src -d lib'.
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 ipfs-readme-standard package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! babel src -d lib
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs ipfs-readme-standard
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls ipfs-readme-standard
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/richard/src/ipfs-readme-standard/npm-debug.log
I'm at a loss. Shouldn't src be generated? There's no extra step on babeljs.io that I am missing.

Shouldn't src be generated?
This is the folder that contains the script that you want to be transpiled. If it doesn't exist then babel will throw the error you posted.
Also, take note of what it says at the bottom of page you linked to:
Pre-6.x, Babel enabled certain transformations by default. However, Babel 6.x does not ship with any transformations enabled. You need to explicitly tell it what transformations to run. The simplest way to do this is by using a preset, such as the ES2015 Preset.
This means that even if you create a src directory and place a file containing ES6 code in it, Babel will happily run, but the output will be (almost) identical to the input.
This is a quick example of how to get up and running with the babel-cli.
Create a project, then install the babel-cli package and ES2015 preset:
mkdir babeltest && cd babeltest
touch package.json
npm install babel-cli babel-preset-es2015 --save-dev
Next edit package.json:
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"build": "babel src -d lib"
},
"scripts": {
"build": "babel --presets es2015 src -d lib"
},
"devDependencies": {
"babel-cli": "^6.0.0"
}
}
Notice that the command in the npm scripts is slightly different to that on the babel homepage, in so far as we're telling it to use the installed presets.
Next make a file in the src directory:
mkdir src && cd src
touch main.js
In main.js add:
[1,2,3].map(x => x * x)
Then run babel via npm:
npm run build
And inspect the output in lib/main.js
"use strict";
[1, 2, 3].map(function (x) {
return x * x;
});

You also get this error when your node modules are not installed, If you download the code from the internet and immediately try running the code, it throws the above error, just run
npm install
and then
npm run build // or other commands should work

In case someone is still looking for solution, check if .babelrc is missing
If so simply create a new .babelrc file and paste the above snippet in it.
{
"presets": ["es2015", "stage-0"]
}

Related

Posting to cyclic and it's asking for package.json to be in the root?

package.json not found at root level of repository.
Cyclic runs scripts defined "scripts" section to build, test and run your app.
But my project is set up like:
-final
--backend
---package.json(including npm start)
--frontend
---package.json(including npm start)
-package.json
^ includes:
"scripts": {
"frontend": "cd frontend; npm start",
"backend": "cd backend; npm start"
How do I merge these packages together and put them into the root directory so I can make this react application live?
I was thinking about something like this:
"scripts": {
"frontend": "cd frontend; npm start",
"backend": "cd backend; npm start"
but when I go to the root directory (final)
npm ERR! Missing script: "start"
npm ERR!
npm ERR! Did you mean one of these?
npm ERR! npm star # Mark your favorite packages
npm ERR! npm stars # View packages marked as favorites
npm ERR!
npm ERR! To see a list of scripts, run:
npm ERR! npm run

npm install failing on npm v6 but passing on v8 (latest)

I am trying to install a package which I created (let's say abc) and getting the below error
Stacktrace:
$ npm i -g abc
/<home>/.nvm/versions/node/v12.22.10/bin/abc -> /<home>/.nvm/versions/node/v12.22.10/lib/node_modules/abc/src/bin/runner.js
npm ERR! code ENOENT
npm ERR! syscall chmod
npm ERR! path /<home>/.nvm/versions/node/v12.22.10/lib/node_modules/abc/bin.js
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, chmod '/<home>/.nvm/versions/node/v12.22.10/lib/node_modules/abc/bin.js'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /<home>/.npm/_logs/2022-08-05T07_58_00_703Z-debug.log
The issue is happening on npm v6 (I am using 6.14.17) while it is able to install the package with v8 (latest)
The changes I made in the version that I published is have a very basic script and put it in bin.
// package.json snippet
"license": "xyz",
"bin": {
"my-package": "./src/bin/runner.js",
"setup": "./bin.js"
},
"dependencies": {}
// package.json snippet (older version which is getting installed correctly with v6 as well
"license": "xyz",
"bin": {
"my-package": "./src/bin/runner.js"
},
"dependencies": {}
The folder structure is as follows:
-- root-folder
-- src
-- bin
-- runner.js
-- bin.js
-- package.json
Things I tried:
Different versions of node - failing for all with npm v6
Working for same node versions (v12 and above) for npm v8 (latest)
The same code was working on npm v6 with npm link (before publishing while testing)
Tried older versions published of the package and they were installed as expected so something in this release is causing the issue
npm cache clean --force
npm install using --force
npm install with and without -g
Try this command npm i --froce
This should bypass some problems in your dependencies!

When using npm, commands fail due to cache access restriction/lock (errno EBUSY -4082)

Problem
I've been running into what appears to be a directory access issue in Windows when attempting to use npm. Having tried numerous suggestions from various other SO questions and answers, I thought I'd see if anyone might have suggestions or solutions based on log the output.
I'm continuously running into the error anytime I attempt an npm operation, including install (global or local), uninstall, update, or executing tools such as electron-packager as shown in the package.json scripts section, below the error itself.
Information
Operating system: Windows 10 (Build 19043)
node version: v18.3.0
npm version: 8.12.1
The error output itself is typically
:
npm ERR! code EBUSY
npm ERR! syscall rename
npm ERR! path C:\Users\<username>\AppData\Local\npm-cache\_cacache\tmp\a870f111
npm ERR! dest C:\Users\<username>\AppData\Local\npm-cache\_cacache\content-v2\sha512\d3\23\a585f82ed54ccfe948a39da16e212d9b112d87f43f74100e4e56c7f688e670ed506b8e7b57284c0a7c268503abcc5569023fcaa73f00b1c4f7670d5c2537
npm ERR! errno -4082
npm ERR! EBUSY: resource busy or locked, rename 'C:\Users\<username>\AppData\Local\npm-cache\_cacache\tmp\a870f111' -> 'C:\Users\<username>\AppData\Local\npm-cache\_cacache\content-v2\sha512\d3\23\a585f82ed54ccfe948a39da16e212d9b112d87f43f74100e4e56c7f688e670ed506b8e7b57284c0a7c268503abcc5569023fcaa73f00b1c4f7670d5c2537'
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\<username>\AppData\Local\npm-cache\_logs\2022-06-04T18_34_40_792Z-debug-0.log
Full log output: PasteBin
package.json
{
"name": "MyElectronApp",
"productName": "MyElectronApp",
"desktopName": "MyElectronApp",
"version": "1.0.0",
"description": "Foobar",
"main": "app.js",
"repository": "https://github.com/",
"keywords": [],
"author": "cognophile",
"license": "GNU General Public License v3.0",
"files": [
"app.js",
"assets/styles.css",
"resources/icon.png",
"resources/icon.ico",
"resources/icon.icns"
],
"scripts": {
"start": "npx electron .",
"build": "npm run build:macos && npm run build:linux && npm run build:windows",
"build:macos": "npx electron-packager . --overwrite --asar --out=dist --ignore='^media$' --prune --platform=darwin --arch=x64 --icon=resources/icon.icns --app-bundle-id=com.cognophile.myelectronapp --app-version=$npm_package_version && cd dist/MyElectronApp-darwin-x64 && zip -ryXq9 ../MyElectronApp-macos-${npm_package_version}.zip MyElectronApp.app",
"build:linux": "npx electron-packager . --overwrite --out=dist --ignore='^media$' --prune --platform=linux --arch=x64 --icon=resources/icon.png --app-bundle-id=com.cognophile.myelectronapp --app-version=$npm_package_version && cd dist/MyElectronApp-linux-x64/ && zip -ryq9 ../MyElectronApp-linux-${npm_package_version}.zip *",
"build:windows": "npx electron-packager . --overwrite --asar --out=dist --ignore='^media$' --prune --platform=win32 --arch=ia32 --icon=resources/icon.ico --version-string.ProductName=$npm_package_productName --app-version=$npm_package_version && cd dist/MyElectronApp-win32-ia32 && zip -ryq9 ../MyElectronApp-windows-${npm_package_version}.zip *"
},
"devDependencies": {
"electron": "^13.6.6",
"electron-builder": "^22.4.1",
"electron-packager": "^14.2.1"
},
"dependencies": {
"about-window": "^1.15.2"
}
}
Trying to run the scripts results in the same error, with differing details. For example, trying to execute the npm start script. The same is true for npm run build:windows, but the unfetchable package URL in the error is https://registry.npmjs.org/electron-packager
❯ npm start
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
> MyElectronApp#1.0.0 start
> npx electron .
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
npm ERR! code EBUSY
npm ERR! syscall rename
npm ERR! path C:\Users\<username>\AppData\Local\npm-cache\_cacache\tmp\01286790
npm ERR! dest C:\Users\<username>\AppData\Local\npm-cache\_cacache\content-v2\sha512\1b\71\c52f6c1e74e5c3c22e0f872bae840fa8e460c356b5c1f807c437a1f8cad2b7bfeb884bb387cbd8012a08d5deaa4b1395f5d4d1cfbc00cfd1b4f5c5e201e3
npm ERR! errno EBUSY
npm ERR! Invalid response body while trying to fetch https://registry.npmjs.org/electron: EBUSY: resource busy or locked, rename 'C:\Users\<username>\AppData\Local\npm-cache\_cacache\tmp\01286790' -> 'C:\Users\<username>\AppData\Local\npm-cache\_cacache\content-v2\sha512\1b\71\c52f6c1e74e5c3c22e0f872bae840fa8e460c356b5c1f807c437a1f8cad2b7bfeb884bb387cbd8012a08d5deaa4b1395f5d4d1cfbc00cfd1b4f5c5e201e3'
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\<username>\AppData\Local\npm-cache\_logs\2022-06-04T19_08_01_775Z-debug-0.log
Attempted solutions
I've tried a number of suggestions from a variety of sources, including (npm ERR! code ENOENT npm ERR! syscall rename [StackOverflow]):
Clearing the cache with npm cache clean --force then reinstalling (npm install)
Verifying the cache with npm cache verify then reinstralling (npm install)
Removing node_modules and package-lock.json, clearing the cache, and reinstalling
Removing read-only checkbox from project folder and cache folder (right-click > properties) which seems to immediately get reset
I've tried these in dedicated PowerShell terminals (non-admin and admin), and the integrated terminal in Visual Studio Code, all having the same result. I've also tried:
Repairing the nodejs installation via the installer
Uninstalling nodejs and removing node/npm related files and directories, restarting the PC, and reinstalling nodejs and attempting npm install in a fresh PowerShell terminal
Rebooting to ensure no other processes are using these directories (such as vscode or another terminal instance)
Closing anti-virus program (not attempted a full uninstall) and trying the above solutions
I suspect this relates to permissions on the cache directories, judging by the error message, but since it is npm which creates those directories after the npm cache clean, I'm not sure I understand what more is needed, and am naturally hesitant to change directory permissions (e.g. NodeJS change file and folder permissions [StackOverflow]) until having received advice.
Thanks in advance!

Unable to install NPM inside Docker

I have a project that builds locally, and I try to create a Docker image.
In my project, I install NPM packages, and my package file is located at:
\src\Core.Blazor\package.json
But, when I try to build my Docker image, it failed with the following error:
npm ERR! code ENOLOCAL
npm ERR! Could not install "src/Core.Blazor/package.json" as it is not a directory and is not a file with a name ending in .tgz, .tar.gz or .tar
I join the Dockerfile too:
### >>> GLOBALS
ARG ENVIRONMENT="Production"
ARG PROJECT="PyProd.IdentityServer.Host"
### <<<
# debian buster - AMD64
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
### >>> IMPORTS
ARG ENVIRONMENT
ARG PROJECT
### <<<
ARG NUGET_CACHE=https://api.nuget.org/v3/index.json
ARG NUGET_FEED=https://api.nuget.org/v3/index.json
# Copy sources
COPY src/ /app/src
ADD common.props /app
WORKDIR /app
# Installs NodeJS to build typescripts
RUN apt-get update
RUN apt-get install curl
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install -y nodejs
RUN npm install /app/src/Core.Blazor/package.json
After trying to fix with the following, I also get an error:
RUN npm install /app/src/SmartPixel.Core.Blazor/
Here is the error I got:
> aspnet-parcel-exp#0.1.0 build /app/src/SmartPixel.Core.Blazor
> parcel build wwwroot/assets/blazorcore.js --out-dir wwwroot/dist/
sh: 1: parcel: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! aspnet-parcel-exp#0.1.0 build: `parcel build wwwroot/assets/blazorcore.js --out-dir wwwroot/dist/`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the aspnet-parcel-exp#0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
I also join the package.json file:
{
"name": "aspnet-parcel-exp",
"private": true,
"version": "0.1.0",
"devDependencies": {
"parcel": "1.12.3"
},
"includePaths": [
"./wwwroot/assets/js",
"./wwwroot/assets/css"
],
"scripts": {
"build": "parcel build wwwroot/assets/blazorcore.js --out-dir wwwroot/dist/",
"watch": "parcel watch wwwroot/assets/blazorcore.js --out-dir wwwroot/dist/"
}
}
I don't know NodeJS, but as your error says:
npm ERR! Could not install "src/Core.Blazor/package.json" as it is not a directory and is not a file with a name ending in .tgz, .tar.gz or .tar
I believe you should change your Dockerfile to this:
RUN npm install /app/src/Core.Blazor/
Or even this:
RUN npm install /app/src/Core.Blazor
As the second one doesn't have trailing slash.

How to fix 'npm ERR! Exit status 64 ' error in npm

I'm setting up a local server, and I am running my server with npm run json:server but I am getting the following error:
npm ERR! Failed at the jsonserver#1.0.0 json:server script 'json-server --watch db.json'.
Can't figure out how to fix this?
I tried to update npm update -g and it doesn't help.
Also tried npm i -g npm
and install the json server locally using npm i --save-dev json-server
Doesn't help
Lenovo-ideapad-990-95IKB:~/Desktop/jsonserver$ **npm run json:server**
> jsonserver#1.0.0 json:server /home/zack/Desktop/jsonserver
> **json-server --watch db.json**
Could not find an option or flag "-c".
Usage: pub <command> [arguments]
Global options:
-h, --help Print this usage information.
--version Print pub version.
--[no-]trace Print debugging information when an error occurs.
--verbosity Control output verbosity.
[all] Show all output including internal tracing messages.
[error] Show only errors.
[io] Also show IO operations.
[normal] Show errors, warnings, and user messages.
[solver] Show steps during version resolution.
[warning] Show only errors and warnings.
-v, --verbose Shortcut for "--verbosity=all".
Available commands:
cache Work with the system cache.
deps Print package dependencies.
downgrade Downgrade the current package's dependencies to oldest versions.
get Get the current package's dependencies.
global Work with global packages.
help Display help information for pub.
publish Publish the current package to pub.dartlang.org.
run Run an executable from a package.
upgrade Upgrade the current package's dependencies to latest versions.
uploader Manage uploaders for a package on pub.dartlang.org.
version Print pub version.
Run "pub help <command>" for more information about a command.
See http://dartlang.org/tools/pub for detailed documentation.
npm ERR! Linux 4.19.5-041905-generic
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "run" "json:server"
npm ERR! node v8.10.0
npm ERR! npm v3.5.2
npm ERR! code ELIFECYCLE
npm ERR! jsonserver#1.0.0 json:server: `json-server --watch db.json`
npm ERR! **Exit status 64**
npm ERR!
npm ERR! **Failed at the jsonserver#1.0.0 json:server script 'json-server --watch db.json'.**
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 jsonserver package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! json-server --watch db.json
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs jsonserver
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls jsonserver
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /home/zack/Desktop/jsonserver/npm-debug.log```
Here is package.json:
{
"name": "jsonserver",
"version": "1.0.0",
"description": "REST API Tracker",
"main": "index.js",
"scripts": {
"json:server": "json-server --watch db.json"
},
"author": "",
"license": "ISC",
"dependencies": {
"json-server": "^0.14.2"
}
}
Expected to start server on localhost:3000
Thank you for help!
I think the issue is you're merging the idea of an npm run script with starting a json-server and haven't fully implemented either aspect. Here are the steps I would try, assuming you wish to have a run script to start your json-server:
Rename db.json to package.json since this is actually your package.json file for your node project. You probably created this file when you ran npm init. This file is not the json data that you're mocking for the json-server when you start the server with your custom script command.
Create a new file called db.json and give it the mocked json you wish to use for your json-server. For example, from the documentation:
db.json:
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
Now change your custom run script command to not include special characters. For example, within your package.json change the following:
"scripts": {
"json:server": "json-server --watch db.json"
},
to:
"scripts": {
"start": "json-server --watch db.json"
},
Now start your server with your custom script command: npm run start. At this point there shouldn't be any errors for starting the server.
Now if you go to http://localhost:3000/posts/1, you should get the following json response:
{ "id": 1, "title": "json-server", "author": "typicode" }

Resources