I have webpack use babel to transpile es6 files into es5. I want to compress these using uglifyjs. The method I used which worked till today was to have this script file be invoked with npm:
scripts/uglifyjs:
#!/usr/bin/env zsh
for file in ./public/assets/js/*.js
do
echo "uglifying `basename $file`"
uglifyjs --verbose --compress --source-map content=${file:2}.map,url=`basename $file`.map,filename=${file:2}.map,includeSources=true --output $file $file
done
And in my package.json:
"scripts": {
...
"uglifyjs": "scripts/uglifyjs",
Take the first file that gets invoked, links.js. If I manually type the command is works:
$ uglifyjs --verbose --compress --source-map content=public/assets/js/links.js.map,url=links.js.map,filename=public/assets/js/links.js.map,includeSources=true --output ./public/assets/js/links.js ./public/assets/js/links.js
INFO: Using input source map: public/assets/js/links.js.map
When running npm run uglifyjs I get the following error:
$ npm run uglifyjs
> jbuk-frontend#0.0.1 uglifyjs /home/jonny/git/jonnybarnes.uk
> scripts/uglifyjs
uglifying links.js
fs.js:651
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open 'content=public/assets/js/links.js.map,url=links.js.map,filename=public/assets/js/links.js.map,includeSources=true'
at Object.fs.openSync (fs.js:651:18)
at Object.fs.writeFileSync (fs.js:1300:33)
at done (/home/jonny/git/jonnybarnes.uk/node_modules/uglify-js/bin/uglifyjs:516:20)
at cb (/home/jonny/git/jonnybarnes.uk/node_modules/uglify-js/bin/uglifyjs:324:39)
at /home/jonny/git/jonnybarnes.uk/node_modules/uglify-js/bin/uglifyjs:391:9
at tryToString (fs.js:512:3)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:500:12)
I don’t know why I can run the command manually, but not via npm, it worked before. Is this a known change with npm v5?
Related
So I'm trying to make conditions in my .gitlab-ci.yml if there is no package then, npm publish to pack the library in the GitLab registry
I gave my pipeline the permission to the registry and the npm access token, but I still get the unauthorized error
this is the part of the .gitlab-ci.yml where I create the .npmrc file and set the configurations.
script:
- |
if [[ ! -f .npmrc ]]; then
echo 'No .nmprc found! Creating one now.'
echo "#${CI_PROJECT_ROOT_NAMESPACE}:registry=${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/">.npmrc
echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">>.npmrc
echo "//registry.npmjs.org/:_authToken=${NPM_ACCESS_TOKEN}">>.npmrc
echo "Created the following .npmrc:"; cat .npmrc
fi
The pipeline get me this when I try to find if there is a package with the name of : $NPM_PACKAGE_NAME
#scope:registry = "https://gitlab.example.com/api/v4/projects/project_id/packages/npm/"
//gitlab.example.com/api/v4/projects/projet_id/packages/npm/:_authToken = (protected)
//registry.npmjs.org/:_authToken = (protected)
; "cli" config from command line options
long = true
$ npm config set always-auth true
$ echo $(npm view "${NPM_PACKAGE_NAME}" )
npm ERR! code E401
npm ERR! 401 Unauthorized - GET https://gitlab.example.com/api/v4/projects/project_id/packages/npm/#scope%2fmy-package
Where :
- NPM_PACKAGE_NAME=$(node -p "require('./my-package/package.json').name")
Instead of echo to the .npmrc, you could try the npm commands directly
npm config set -- '//gitlab.example.com/api/v4/projects/<your_project_id>/packages/npm/:_authToken' "${NPM_TOKEN}"
npm config set -- '//gitlab.example.com/api/v4/packages/npm/:_authToken' "${NPM_TOKEN}"
That way, you are sure the .npmrc is properly updated.
Could you try to create the .npmrc in your home folder instead of locally?
We do this in our pipeline and it works without any problem:
publish:
stage: publish
script:
- echo "#<scope>:registry=https://${CI_SERVER_HOST}/api/v4/projects/${REGISTRY_PROJECT_ID}/packages/npm/" > ~/.npmrc
- echo "//${CI_SERVER_HOST}/api/v4/projects/${REGISTRY_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}" >> ~/.npmrc
- npm version --no-git-tag-version "$(<.next-version)" --allow-same-version
- npm publish --tag ${NPM_TAG_NAME}
As you see, other than the npm version and npm publish commands, the only difference is the .npmrc file location.
I'm using commander.js command like this ./index.js --project mono --type item --title newInvoice --comments 'Creates an invoice' --write, Now I'm using the command through npm run item newInvoice by setting some options in package.json file like this
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"snapshot": "node --max-old-space-size=10240 ./scripts/snapshot.js",
"item": "./index.js --project mono --type item --title",
"config": "./index.js --project mono --type config --title"
}
But whenever I'm trying to get the --write option with npm using npm run item newInvoice --write it's showing undefined for --write
Source Code:
#!/usr/bin/env node
const fs = require('fs');
const program = require('commander');
require('colors');
program
.version('0.1.0')
.option('--project [project]', 'Specifies the project name', 'mono')
.option('--type [type]', 'Type of code to generate, either "item" or "config"', /^(config|item)$/, 'config')
.option('--title [title]', 'Title of the item or config', 'untitled')
.option('--comments [comments]', 'Configs: describe the config', '#todo description/comments')
.option('--write', 'Write the source code to a new file in the expected path')
.option('--read', 'To see what would be written the source code to a new file in the expected path')
.parse(process.argv);
console.log(program.write, program.read); //=> undefined undefined
Can anyone help me how to use commander js command with npm?
When you run your npm run command you need to utilize the special -- option to demarcate the end of any option(s) that may belong to the npm run command itself (e.g. --silent), and the beginning of the argument(s) that are to be passed to the end of the npm script.
Run the following command instead:
npm run item -- newInvoice --write
Given the aforementioned command and the command currently defined your npm script named item they essentially form the following compound command prior to execution:
./index.js --project mono --type item --title newInvoice --write
^ ^
The npm run documentation states the following:
As of npm#2.0.0, you can use custom arguments when executing scripts. The special option -- is used by getopt to delimit the end of the options. npm will pass all the arguments after the -- directly to your script.
and it's usage syntax is defined in the Synopsis section as:
npm run-script <command> [--silent] [-- <args>...]
^^
Note: It's not possible to add the -- option to the npm script itself.
I’m trying to make an easy-to-use NPM package out of my nextjs-generic-landing-page and the NPM is now at https://www.npmjs.com/package/nextjs-generic-landing-page
For some reason, the components using styled-jsx do not work, they render weirdly and produce a warning:
index.js:1 Warning: Received `true` for a non-boolean attribute `jsx`.
If you want to write it to the DOM, pass a string instead: jsx="true" or jsx={value.toString()}.
in style
in div
in SocialSharing (at pages/index.js:32)
…
Here’s the code I use to build the NPM package using Babel:
"publish:npm": "rm -rf dist && for folder in {components,lib,config,public}; do mkdir -p dist/${folder} && babel ${folder} -d dist/${folder} --copy-files --presets=#babel/preset-env,next; done && cp package.json dist/"
Any tips?
I was missing the styled-jsx/babel plugin:
"publish:npm": "rm -rf dist && for folder in {components,lib,config,public}; do mkdir -p dist/${folder} && babel ${folder} -d dist/${folder} --copy-files --presets=#babel/preset-env,next --plugins=styled-jsx/babel; done && cp package.json dist/"
I'm using npm to run a build and I'm trying to override my .npmrc config options using cmd line flags. However npm insists on passing these flags directly to my script rather than reading them as config options as described in the docs. What am I doing wrong?
From the cmd line I try to build a Tizen pacakge like so:
npm run package --tizen_profile myprofile
inside my package.json I have:
"package": "tizen package -t wgt --sign $npm_package_config_tizen_profile -- .buildResult/wgt -o .buildResult/wgt"
The result from running the command is:
package: `tizen package -t wgt --sign $npm_package_config_tizen_profile -- .buildResult/wgt -o .buildResult/wgt "myprofile"`
Where it should be:
package: `tizen package -t wgt --sign "myprofile"_tizen_profile -- .buildResult/wgt -o .buildResult/wgt`
It's like npm is merely appending the cmd line argument to the script command instead of plugging it in like a variable as described in the docs: https://docs.npmjs.com/misc/config
Has there been a recent update to npm which deprecates and removes this ability? I just updated to npm 6.x but it was working this way on 5.x as well.
you can try to rewrite your script within package.json without --sign flag like:
"package": "tizen package -t wgt -- .buildResult/wgt -o .buildResult/wgt"
and then pass it when you run npm command:
npm run package -- --sign myprofile
I assume that you can change the order of arguments, because --sign myprofile now will be at the very end of your command
UPDATE
here is another way to pass variables and place them in any place in your CLI command (without using npm config set). In my package.json:
"aaa": "ls $myoptionalflag && $mycmd"
this way I can pass any flag to la command (or not to pass at all) and I can pass any CLI command as mycmd variable. So, now I can run:
myoptionalflag=-la mycmd=pwd npm run aaa
which will execute
ls -la && pwd
or
mycmd=pwd npm run aaa
which will execute
ls && pwd
I FIGURED IT OUT!
The documentation is flawed as it doesn't tell you the correct syntax for passing npm config properties. I had to use:
npm run package --mypackagename:tizen_profile="myprofile"
where mypackagename is the name property used in package.json. Also note the key value syntax is --key=value and not --key value as described in the docs. Again, --key would be packagename:key using the name specified at the top level of your package.json.
Hey all got a issue with npm and tslint I was hoping you could help me with.
Ok here comes my situation and code:
package.json
"scripts": {
"lint": "tslint -c tslint.json 'src/app/**/*.ts'",
"pretest": "npm run lint ",
"test": "echo 'No test are made'",
...
},
When I run command npm test this is the output:
input terminal
$ npm test
output terminal
> keoom#1.0.0 pretest c:\Users\Hjorth\Documents\github\keoom-angular
> npm run lint
> keoom#1.0.0 lint c:\Users\Hjorth\Documents\github\keoom-angular
> tslint -c tslint.json 'src/app/**/*.ts'
> keoom#1.0.0 test c:\Users\Hjorth\Documents\github\keoom-angular
> echo 'No test are made'
'No test are made'
If I only run command tslint -c tslint.json 'src/app/**/*.ts' I on the other hand see the linting error.
input terminal
$ tslint -c tslint.json 'src/app/**/*.ts'
output terminal
src/app/app.component.ts[1, 27]: " should be '
So as you can see there is a linting error in my code, but if I am not running the script directly It will not show. What I am expecting is this:
When I run npm test the script pretest will run, and that script will run the script lint, it will then exit with exit 0 before test script is run as it will find the linting error.
Anyone that can be of assistance.
It's the quotes around the file spec in the tslint command that are the problem:
"lint": "tslint -c tslint.json 'src/app/**/*.ts'"
^ ^
If you remove those, you should see the expected error reported by tslint.