Unable to run babel via npm script "babel: command not found" - javascript

To get started I ran:
npm install --save-dev babel-cli
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-stage-0
Here is my package.json:
{
"scripts": {
"build": "babel src -d dist"
},
"devDependencies": {
"babel-cli": "^6.6.5",
"babel-core": "^6.7.2",
"babel-preset-es2015": "^6.6.0",
"babel-preset-stage-0": "^6.5.0"
}
}
Here is my .babelrc file:
{
"presets": ["es2015", "stage-0"]
}
My file structure is like this:
- Root
- src
- client
- server
- test
- dist
- package.json
I am calling npm run build from the root folder. I am expecting it to compile the source folder into the dist folder. It runs and then I get this error:
> babel src -d dist
sh: babel: command not found
npm ERR! Darwin 15.2.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "build"
npm ERR! node v5.8.0
npm ERR! npm v3.7.3
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! redacted#1.0.0 build: `babel src -d dist`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the redacted#1.0.0 build script 'babel src -d dist'.
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 redacted package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! babel src -d dist
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs redacted
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls redacted
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /Users/user/redacted/npm-debug.log
So as you can see, I've installed babel-cli, I've installed the presets, and I think everything is in order according to the babel docs.
Does anyone have ideas about why it wouldn't be working? Could I be missing a babel npm file? Is "babel src -d dist" incorrect?
Thanks for any help you can provide!
I made another folder and followed the same steps, it worked perfectly. For some reason it's not working in this directory.

I've come across the same issue lately. Removing the node_modules folder and running npm install again no longer fixes the issue.
The reason you are getting this error is because babel-cli needs to be installed globally, not as a project dependency.
Run npm install -g babel-cli to install it globally.
Babel-preset-es2015 can then be installed as a dev dependency for your projects npm install --save-dev babel-preset-es2015

You should never install babel-cli globally - in fact, they specifically have an entire paragraph telling you not to from their official docs.
Edit package.json >> add a script with the key called, say, build with the value ./node_modules/.bin/babel <commands>
If you called it build, just then type npm run build.

The error occurs because ./node_modules/.bin is not in $PATH. ./node_modules/.bin is where all the executable binaries can be found.
As recommended by the documentation, you can reference the babel cli inside of node_modules:
$ ./node_modules/.bin/babel src -d lib
You can modify your npm run build command to use this:
"scripts": {
"build": "./node_modules/.bin/babel src -d dist"
},

Did you run "npm install" to install the dev packages?

Many of the answers above are correct.
The error occurs because ./node_modules/.bin is not in $PATH. ./node_modules/.bin is where all the executable binaries can be found.
What I did was I built a simple dynamic alias function in my zshrc file.
# Babel
function bbl() {
./node_modules/.bin/babel "$#"
}
Now you can use bbl instead of babel
bbl --version
6.24.1 (babel-core 6.25.0)

Related

How can I create a node CLI app that needs babel to run?

I'm trying to creaet a global node CLI app, but It depends on Babel to run.
I've added this to my package.json
"bin": {
"kurly": "dist/index.js"
},
"scripts": {
"prepare": "npm run build",
"build": "babel src -d dist",
"start": "yarn build && node dist/index.js"
},
and I'm installing it from my git using
npm install -g alexanderprod/kurly
But when installing it I get the following error
npm ERR! path /usr/local/lib/node_modules/kurly/dist/index.js
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall chmod
npm ERR! enoent ENOENT: no such file or directory, chmod '/usr/local/lib/node_modules/kurly/dist/index.js'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
So it seems like npm didn't build it with babel before applying chmod.
And now I'm wonderging how to fix that, since I assume it makes no sense to upload dist/ to npm/git

How to run locally saved npm packages without setting them on package.json?

I am trying to execute a locally saved npm package, but it is not found.
npm run jsdoc
Outputs:
npm ERR! Linux 4.15.0-24-generic
npm ERR! argv "/usr/bin/node" "/usr/local/bin/npm" "run" "jsdoc"
npm ERR! node v8.10.0
npm ERR! npm v3.10.10
npm ERR! missing script: jsdoc
...
But I know the script is located in my node_modules/.bin, and can execute it directly:
jsdoc -> ../jsdoc/jsdoc.js*
And running it works as a pre-configured command on my package.json
"scripts": {
"doc": "jsdoc"
},
Is there a special environment configuration (npmrc or something similar) to make npm run look at both package.json scripts and in node_modules/.bin?
What am I missing?
The command npm run is an alias for npm run-script. As seen in the docs:
SYNOPSIS
npm run-script <command> [-- <args>...]
alias: npm run
It will only add node_modules/.bin to context inside scripts edited in package.json.
The npm-run answer can be misleading as one can think npm commands could be referenced as git sub-commands would (git-init, git-clone etc), which in this particular case is a separate package, not related to npm run-script.

Babel installation - src doesnt exist

quick question.
Found similar cases but they dont worked for me.
Following this guide https://babeljs.io/docs/setup/#installation I´m trying to install Babel Cli.
but running babel from a command in package.json configured like
"scripts": {
"build": "babel src -d lib"
},
throwing an error
> babel src -d lib
src doesn't exist
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! my-package#1.0.0 build: `babel src -d lib`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the my-package#1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely
additional logging output above.
npm ERR! A complete log of this run can be found in:
when running the command npm run build
I´ve already installed babel-preset-env and configured a .babelrc
npm version 6.1.0
Do you know where src doesn't exist comes from ?
Thanks!
src is the folder that contains the files you want to transform. Either create it and move your files there or update the command to use the folder that contains your files.
More info: https://babeljs.io/docs/usage/cli/#compile-directories

AWS Elastic Beanstalk - How to build bundle JS using npm and webpack

I have a node js app deployed at elasticbeanstalk. Installed and symlinked node, npm and webpack. But when running npm run build-prod which itself calls the script webpack --config /var/app/current/webpack.prod.config.js. Getting the following error with exit status -2. The same happens if I run the webpack command directly too. I'm looking out for solutions.
[2016-07-26T06:57:36.301Z] INFO [9731] - [Application update app-5c81-160726_122417#24/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_site_web/Command 06_npm_run_build_prod] : Activity execution failed, because:
> site-web#1.0.0 build-prod /tmp/deployment/application
> webpack --config /var/app/current/webpack.prod.config.js
npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/bin/npm" "run" "build-prod"
npm ERR! node v4.4.6
npm ERR! npm v2.15.5
npm ERR! file sh
npm ERR! path sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn sh
npm ERR! site-web#1.0.0 build-prod: `webpack --config /var/app/current/webpack.prod.config.js`
npm ERR! spawn sh ENOENT
npm ERR!
npm ERR! Failed at the site-web#1.0.0 build-prod script 'webpack --config /var/app/current/webpack.prod.config.js'.
npm ERR! This is most likely a problem with the site-web package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! webpack --config /var/app/current/webpack.prod.config.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs site-web
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR! npm owner ls site-web
npm ERR! There is likely additional logging output above.
npm ERR! Linux 4.4.14-24.50.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v4.4.6-linux-x64/bin/node" "/bin/npm" "run" "build-prod"
npm ERR! node v4.4.6
npm ERR! npm v2.15.5
npm ERR! code ELIFECYCLE
container config file:
container_commands:
01_node_symlink:
command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node"
02_npm_symlink:
command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/npm /bin/npm"
03_npm_install_global_packages:
command: "npm install webpack webpack-cli -g"
04_webpack_symlink:
command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/webpack /bin/webpack"
#05_webpack_run_build_prod:
#command: "webpack --config /var/app/current/webpack.prod.config.js --progress --colors"
06_npm_run_build_prod:
command: "npm run build-prod"
Scripts in package.json
"scripts": {
"build": "webpack --config webpack.local.config.js --progress --colors",
"build-local": "webpack --config webpack.prod.config.js --progress --colors",
"build-prod": "webpack --config /var/app/current/webpack.prod.config.js",
"server": "node app.js",
"dev-server": "node dev-app.js"
}
And when I uncomment 05 that is run the webpack command directly it ends with error Error: "/var/app/current/site-web/static/assets/app/index.js" is not in the SourceMap.
The build script is successful at local but blocked on all the ways at production. Couldn't figure out how to run webpack command to build the JS on AWS beanstalk environment. Isn't the ideal way of building the JS file?
node: 4.4.6
npm: 2.15.5
webpack: latest
I'm still new to elastic beanstalk and having similar issues myself but I noticed your tasks for symlinking node, npm, etc, are in the form of container_commands.
According to the official docs, "They run after the application and web server have been set up and the application version file has been extracted, but before the application version is deployed."
Maybe try using commands: instead of container_commands:. See link I shared.
Maybe the fact that these commands aren't running until after the application and web server have already been set up is why it may not be working for you?
Again, I'm still new to elastic beanstalk but maybe that can help.
I think the approach is faulty. Webpack and Webpack-cli are devDependencies of package.json.
You should set your env variable to NPM_USE_PRODUCTION=false in order for EB to install devDependencies as well.
see more here: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/nodejs-platform-packagejson.html
I had issues with global npm installs (in my case react-scripts). What I did as a workaround was to install the dependent package as declared in package.json and then symlink to it (located at /tmp/deployment/application/node_modules/.bin on EC2) in the .ebextensions config file:
03_react_scripts_symlink:
command: "ln -sf /tmp/deployment/application/node_modules/.bin/react-scripts /bin/react-scripts"
04_npm_run_build_prod:
command: "sudo npm run build"
Finally, I managed to run the build script at the staging dir. It's not a permanent solution but it works.
05_webpack_run_build_prod:
command: "cd /tmp/deployment/application && sudo webpack --config webpack.prod.config.js --progress --colors"

Error installing starting a project with grunt

I'm trying to start a new project using gruntjs.
I have this project structure:
grunt-test (dir)
|_ index.html
|_ package.json
|_ Gruntfile.js
|_ assets (dir)
|_ _js (dir)
|_ scripts.js
|_ _sass (dir)
|_ style.sass
I have nodejs and grunt installed.
So, when I go to project's folder and use this command line on terminal: npm install grunt --save-dev
It returns me all those errors:
npm ERR! Failed to parse json
npm ERR! Unexpected end of input
npm ERR! File: /Applications/XAMPP/xamppfiles/htdocs/grunt-test/package.json
npm ERR! Failed to parse package.json data.
npm ERR! package.json must be actual JSON, not just JavaScript.
npm ERR!
npm ERR! This is not a bug in npm.
npm ERR! Tell the package author to fix their package.json file. JSON.parse
npm ERR! System Darwin 11.4.2
npm ERR! command "node" "/usr/local/bin/npm" "install" "grunt" "--save-dev"
npm ERR! cwd /Applications/XAMPP/xamppfiles/htdocs/grunt-test
npm ERR! node -v v0.10.26
npm ERR! npm -v 1.4.3
npm ERR! file /Applications/XAMPP/xamppfiles/htdocs/grunt-test/package.json
npm ERR! code EJSONPARSE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /Applications/XAMPP/xamppfiles/htdocs/grunt-test/npm-debug.log
npm ERR! not ok code 0
I've been trying to install it for a while and I haven't had any success. I don't know what I'm doing wrong. I'm following all the instructions on gruntjs docs. Can someone help me?
Your package.json is not a valid JSON. Instead of typing in commands, you can add these as devDependencies in your package.json file and then use npm install which will install packages from the package.json file. You can use the below.
{
"name": "Project Name",
"version": "1.0.0",
"devDependencies": {
"grunt": "0.4.4",
"grunt-cli": "0.1.13"
}
}
Run npm install grunt-cli -g if grunt is not found message gets displayed.
In your package.json, strings are enclosed into single quotes ('), it makes it a non-valid JSON data. JSON expects strings to be enclosed into double quotes (").

Categories