How to login to AWS CodeArtifact before npm installs - npm-scripts

How can I trigger the login before the install script in node v18.12.1/npm 8.19.2?
I expect the npm preinstall script will run before the install script based on the documentation. When running node v14.21.1/npm 6.14.17 I was able to follow the instructions in this tutorial to trigger a CodeArtifact login with the preinstall script. All packages were installed from my CodeArtifact repository on running npm install as expected.
After updating to node v18.12.1/npm 8.19.2, npm install fails with error:
Unable to authenticate, your authentication token seems to be invalid
What I have tested (each in a fresh environment with no npm cache):
npm run preinstall successfully logs on
∴ AWS credentials are configured and the preinstall script works when executed
npm install fails authentication
npm run preinstall; npm install successfully logs on and installs from CodeArtifact
∴ the preinstall script is not running. I get no results from filtering the debug logs suggesting the co:login step is not run:
$ cat /root/.npm/_logs/2022-12-06T07_12_47_353Z-debug-0.log | grep co:login
$ echo $?
1
After reverting to node v14.21.1/npm 6.14.17, npm install behaves as expected, logging in and installing packages from CodeArtifact.
I have recreated the problem with a minimal package.json which can be tested in a docker container:
docker run --rm -it -v ~/.aws:/root/.aws:ro --name app node:18.12.1 /bin/bash
wget --quiet -O "awscliv2.zip" "https://awscli.amazonaws.com/awscli-exe-linux-$(uname -m).zip" && unzip -q awscliv2.zip
./aws/install
rm awscliv2.zip && rm -r ./aws
Copy files in from a separate terminal
docker cp package.json app:/package.json
docker cp package-lock.json app:/package-lock.json
And then install with npm install in the docker container.
package.json template would require you to substitute your own CodeArtifact repository, domain and owner
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"preinstall": "npm run co:login",
"co:login": "aws codeartifact login --tool npm --repository my-repository --domain my-domain --domain-owner 123456789 --region ap-southeast-2"
},
"author": "",
"license": "ISC",
"dependencies": {
"dayjs": "^1.11.6"
}
}
package-lock.json template would require you to substitute your own CodeArtifact url
{
"name": "app",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "app",
"version": "1.0.0",
"hasInstallScript": true,
"license": "ISC",
"dependencies": {
"dayjs": "^1.11.6"
}
},
"node_modules/dayjs": {
"version": "1.11.6",
"resolved": "https://mydomain-123456789.d.codeartifact.ap-southeast-2.amazonaws.com:443/npm/my-repository/dayjs/-/dayjs-1.11.6.tgz",
"integrity": "sha512-zZbY5giJAinCG+7AGaw0wIhNZ6J8AhWuSXKvuc1KAyMiRsvGQWqh4L+MomvhdAYjN+lqvVCMq1I41e3YHvXkyQ=="
}
},
"dependencies": {
"dayjs": {
"version": "1.11.6",
"resolved": "https://mydomain-123456789.d.codeartifact.ap-southeast-2.amazonaws.com:443/npm/my-repository/dayjs/-/dayjs-1.11.6.tgz",
"integrity": "sha512-zZbY5giJAinCG+7AGaw0wIhNZ6J8AhWuSXKvuc1KAyMiRsvGQWqh4L+MomvhdAYjN+lqvVCMq1I41e3YHvXkyQ=="
}
}
}

Related

How to replace the registry from package-lock.json to a new one?

Currently, the package registry in the package-lock.json is http://registry.npm.taobao.org/.
package-lock.json:
{
"name": "webpack",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"#types/clean-webpack-plugin": {
"version": "0.1.0",
"resolved": "http://registry.npm.taobao.org/#types/clean-webpack-plugin/download/#types/clean-webpack-plugin-0.1.0.tgz",
"integrity": "sha1-xJillaQXSwoxPOQt5+UldcEi9aQ=",
"dev": true,
"requires": {
"#types/webpack": "*"
}
},
"#types/node": {
"version": "9.4.0",
"resolved": "http://registry.npm.taobao.org/#types/node/download/#types/node-9.4.0.tgz",
"integrity": "sha1-uFoLzx4cyE60kBt+lpZq7cbweNE=",
"dev": true
},
"#types/strip-bom": {
"version": "3.0.0",
"resolved": "http://registry.npm.taobao.org/#types/strip-bom/download/#types/strip-bom-3.0.0.tgz",
"integrity": "sha1-FKjsOVbC6B7bdSB5CuzyHCkK69I=",
"dev": true
},
// ...others
}
I want to replace all registries with the npm official registry https://registry.npmjs.org/ or any specific registry. Keep using the same versions of packages.
How can I do that?
Found a solution from issue of yarn.
Before replacing the registries, let's check how many registries need to be replaced, I will check the number of taobao words to determine.
☁ v4 [master] ⚡ grep -o 'taobao' package-lock.json | wc -l
1180
Since some registries' pattern like this:
"resolved": "https://registry.npm.taobao.org/#hapi/joi/download/#hapi/joi-15.1.1.tgz?cache=0&sync_timestamp=1603524515155&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40hapi%2Fjoi%2Fdownload%2F%40hapi%2Fjoi-15.1.1.tgz",
The other_urls query string contains the url address of the taobao registry. The // symbol for the https protocol is escaped, so I will just replace registry.npm.taobao.org instead of https://registry.npm.taobao.org.
Besides, I removed the old node_modules using rm -rf node_modules, later, I will install node modules after replacing the registry with the new one, re-run npm install and run my application to check if it works.
Now, replace the registry using the below Linux command:
sed -i -e "s#registry.npm.taobao.org#registry.npmjs.org#g" package-lock.json
Check:
☁ v4 [master] ⚡ grep -o 'taobao' package-lock.json | wc -l
0
☁ v4 [master] ⚡ grep -o 'npmjs' package-lock.json | wc -l
1180
After re-install the node modules with the new registries, my project generated by create-react-app tool works fine.

NPM and Life Cycle Operation Order

I find this liste for "Life Cycle Operation Order":
preinstall, install, postinstall, prepublish, preprepare, prepare, postprepare
So I use preinstall with a commande line. My commande line is a aws cli commande. This command modify the .npmrc file (add authentification). When my script execute install phase, my new version of my .npmrc file is not taken into account.
I'm obligated to relaunch my commande line npm install
(<123456789> and <me> is modified for stackoverflow)
{
"name": "my-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"preinstall": "npm run co:login",
"co:login": "aws codeartifact login --tool npm --repository my-repo --domain my-domain --domain-owner <123456789> --profile <me> --namespace #tp-npm",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"uuid": "^3.3.2",
"#my-npm/my-common": "1.0.0"
}
}

How to fix "Could not locate bower" error while migrating from Vaadin 13 to Vaadin 14

I am trying to migrate my Vaadin application from 13th version to 14th. I am following the migration guide: https://vaadin.com/docs/v14/flow/v14-migration/v14-migration-guide.html step by step.
Everything is going well till I have to upgrade Polymer2 to Polymer3 following this guide: https://vaadin.com/docs/v14/flow/v14-migration/migration-tool.html
Executing the following command mvn vaadin:migrate-to-p3
returns the com.vaadin.flow.migration.MigrationToolsException: Could not locate bower. Install it manually on your system and re-run migration goal. but I have the bower installed globally and locally.
bower -v returns 1.8.8
This is how my package.json looks like:
{
"name": "no-name",
"license": "UNLICENSED",
"dependencies": {
"#polymer/polymer": "3.2.0",
"#webcomponents/webcomponentsjs": "^2.2.10",
"polymer-modulizer": "^0.4.3"
},
"devDependencies": {
"bower": "^1.8.8",
"copy-webpack-plugin": "5.0.3",
"raw-loader": "3.0.0",
"webpack": "4.30.0",
"webpack-babel-multi-target-plugin": "2.1.0",
"webpack-cli": "3.3.0",
"webpack-dev-server": "3.3.0",
"webpack-merge": "4.2.1"
},
"description": "This project can be used as a starting point to create your own Vaadin Flow application with Spring Boot.\r It contains all the necessary configuration and some placeholder files to get you started.",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "http://git.ws.com.pl:3000/ck/vaadin-mi.git"
},
"author": ""
}
Does anyone know how to fix it?
I had the same problem running the migration tool on Windows 10.
As a workaround, I used Docker to run it :
install Docker
run a Maven image, I use Maven 3 and openjdk 11 (mount your maven repo to prevent re-downloading all dependencies) :
docker run -it --rm -v c:/[path to your maven repo]:/root/.m2/repository -v c:/[path to your maven project]:/usr/src/myproject -w /usr/src/myproject --entrypoint "/bin/sh" maven:3-jdk-11
then, in the container, install node and npm :
curl -sL https://deb.nodesource.com/setup_12.x | bash -
apt-get install -y nodejs
install bower :
npm install -g bower
allow running bower with root
echo '{ "allow_root": true }' > /root/.bowerrc
run the migration tool
mvn vaadin:migrate-to-p3

Running npm install does not produce lock file

When running npm install, when will it produce a package-lock.json file and when will it not?
This is the version of npm that I am using:
$ npm --version
3.10.10
And this a simple package.josn that I am testing with:
$ cat package.json
{
"name": "invoices_svc",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.16.2"
},
"repository": {
"type": "git",
"url": "git#.../TotalInvoiceDemoApp.git"
},
"description": "..."
}
For some reason, I don't see a package-lock.json that is created after running npm install.
I also tried building a docker image with this, where I notice the warning:
npm notice created a lockfile as package-lock.json. You should commit this file.
...
Step 4/7 : RUN npm install
---> Running in f4c48bbcc52a
npm notice created a lockfile as package-lock.json. You should commit this file.
...
There may be some obvious configuration that I missed in my local dev environment? Why it won't produce the lock file locally?
lock-file was introduced in npm version 5.0.0, you need to update npm to generate lock files

npm pre commit not working

I am using npm precommit hook, but it is not stopping a file with issues to be committed, nor am I getting the message "Pre commit checks" when I try to commit a file.
Package Json:
{
"name": "myfolder",
"version": "1.0.0",
"description": "",
"main": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 0",
"precommit-msg": "echo 'Pre-commit checks...' && exit 0",
"lint": "csslint global/css"
},
"author": "SR",
"license": "ISC",
"dependencies": {
"csslint": "^1.0.4",
"jshint": "^2.9.4",
"pre-commit": "^1.2.2"
},
"pre-commit": [
"precommit-msg",
"lint"
],
"devDependencies": {
"pre-commit": "^1.2.2"
}
}
Please, make sure that your 'package.json' file is in the same folder, where '.git' folder is (where git repository was initialized). When you install 'pre-commit' package, 'pre-commit' file should appear under '.git/hooks/'.
Just FYI I had this issue because the pre-commit file was missing in the hooks folder.
Running npm i pre-commit --save-dev again created the file and solved it for me.
Have't managed to implement it with few "pre-commit" NPM modules (#fastify/pre-commit, monorepo-staged-precommit) so implemented it "manually" with adding tools/pre-commit.sh file into repo with content like:
#!/bin/sh
DIR='web'
echo "Pre-commit actions (NPM tests for $DIR)..."
cd $DIR && npm run test
and updating package.json with:
"scripts": {
"test",
"install-precommit": "cp ../tools/pre-commit.sh ../.git/hooks/pre-commit"
This solution has some limitations (like instead of automatic installation need to ask somewhere in "README" about npm run install-precommit and I'm not sure how it would work on Windows especially without Git Bash) but it worked for me.
Notes:
Other pre-commit NPM packages either didn't work as well or asked for NVM and other extra tools which I don't want devs to install for such small task.
pre-commit.sh may has any name and don't be executable - "install-precommit" task and git care about.