npm reinstalls package.json and package-lock.json - javascript

my npm was working perfectly fine, today I wanted to install a package and I got a lot of npm ERR! messages.
Then I saw that npm was creating package.json, package-lock.json, and a node_modules folder in the directory I was installing the packages in.
Shouldn't these files and folders be in /bin/ somewhere?
Then after I reinstalled some packages npm ls works but after some time it outputs:
├── #babel/code-frame#7.18.6 extraneous
├── #babel/helper-validator-identifier#7.19.1 extraneous
├── #babel/highlight#7.18.6 extraneous
├── #jridgewell/gen-mapping#0.3.2 extraneous
├── #jridgewell/resolve-uri#3.1.0 extraneous
├── #jridgewell/set-array#1.1.2 extraneous
But not just these, there are hundreds.
I just uninstalled Node and npm and re-downloaded it again.
But now when I tried to install a package it created a node_modules folder, package.json and package-lock.json in my home directory.
These files and the folder is not supposed be there, right?
Thank you!

Related

Updating version of dependency in package-lock.json

How to update version of a dependency which is required by other dependencies.
├─┬ vue-svgicon#3.2.9
│ └─┬ yargs#12.0.5
│ └── yargs-parser#11.1.1
In my case, yargs-parser needs to be updated to 13.1.1. Making any changes in the package-lock.json just overwrites it when you run npm install.
npm audit fix doesnt update it either
Tried doing with npm-shrinkwrap.json as an override for package-lock.json but want to find out some other alternative.
Another alternative is to do a define
"resolutions"
in the package.json file in addition to
"preinstall": "npm-force-resolutions"
but this adds an additional dependency of npm-force-resolutions which is not desired

Imported class in angular is undefined

I have an npm project with the following structure:
app-dep
├── dist
│ ├── bundle.js
│ └── bundle.js.map
├── node_modules/
├── package.json
├── package-lock.json
├── src
│ ├── base-component.ts
│ ├── factory.ts
│ ├── app.ts
│ └── modules/
├── tsconfig.json
└── webpack.config.js
My app.ts code has an App class that utilizes all .ts files inside src/
export default class App extends HTMLElement { //some-content }
I use webpack to build a bundle.js inside the dist/ folder.
I have another angular project where I install this app-dep project using
npm install --save ../app-dep
When I try to use it in my angular component:
import App from 'app-renderer/dist/bundle';
ngOnInit() {
window.customElements.define('micro-app', App);
}
I get this error:
Failed to execute 'define' on 'CustomElementRegistry': parameter 2 is not of type 'Function'
When I try to log it on console, all I can see is undefined.
It seems that I can't import basic functions even.
Can you tell me what's wrong?
I think the problem has to do with the import or the pack, I ellaborate below with the steps to follow.
Also, make sure that you are exporting all the classes that you want to make available from the outside.
For TypeScript libraries
You can simply use "tsc" and then pack the generated code inside the dist folder with "npm pack" and install the dependency in your application with "npm install ".
This may get complicated due to the different module systems and bundlers, check this links for more info on Webpack:
https://marcobotto.com/blog/compiling-and-bundling-typescript-libraries-with-webpack/
https://webpack.js.org/guides/author-libraries/
For CSS libraries
The "npm pack" has to be executed in the root folder. You may want to process your styles with sass before and only pack the result.
For Angular libraries
By default with Angular CLI when you build a library project the code is generated in /dist/mylibrary folder.
If you want to use that code in other project, the steps are:
Build your library with "ng build mylibrary" (add --prod for production).
From your library, move into /dist/mylibrary folder and then execute a "npm pack", that will generate a tgz package.
From your application in which you want to use the library execute "npm install " to install the dependency.
To import code from the library use "import * from 'mylibrary'"
Other option would be using "npm link", that creates a link between your node_nodules and the library code as explained here:
https://www.willtaylor.blog/complete-guide-to-angular-libraries/
That would be the way to go with local libraries, usually these libraries are published into Npm repository (public or private) and installed remotely with "npm install ". These steps are only for local usage.

REACT - not working npm while installing it

Did all the command on command line
from installing the npm globally npm install -g create-react-app along with following codes:
npx create-react-app <myprojectname>
and also cleared the npm cache but it always get stuck on the line in the image showing the following error. Along with the error:
┌──────────────────────────────────────────────────────────────────┐
│ npm update check failed │
│ Try running with sudo or get access │
│ to the local update config store via │
│ sudo chown -R $USER:$(id -gn $USER) C:\Users\Smart Sites\.config │
└──────────────────────────────────────────────────────────────────
along with 65 packages are looking for funding
[1]: https://i.stack.imgur.com/KJVVp.png
Here is what i found form the getting started page of create react app.
If you've previously installed create-react-app globally via
npm install -g create-react-app,
we recommend you uninstall the package using
npm uninstall -g create-react-app
to ensure that npx always uses the latest version.
npx create-react-app my-app
cd my-app
npm start
But always ensure that you have an existing folder before using npx.

Create React App with Storybook creating webpack issue

So I have a large React app created and set up with Create React App. Recently we started using Storybook to work on and create components. Its been great. However, we keep getting this weird webpack issue when we try to run or build the app. Here is the console log out:
> react-scripts start
There might be a problem with the project dependency tree.
It is likely not a bug in Create React App, but something you need to fix locally.
The react-scripts package provided by Create React App requires a dependency:
"webpack": "4.41.2"
Don't try to install it manually: your package manager does it automatically.
However, a different version of webpack was detected higher up in the tree:
/Users/alexanderkaran/Documents/ClimateClever/cc_school_web/node_modules/webpack (version: 4.41.5)
Manually installing incompatible versions is known to cause hard-to-debug issues.
If you would prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
To fix the dependency tree, try following the steps below in the exact order:
1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
2. Delete node_modules in your project folder.
3. Remove "webpack" from dependencies and/or devDependencies in the package.json file in your project folder.
4. Run npm install or yarn, depending on the package manager you use.
In most cases, this should be enough to fix the problem.
If this has not helped, there are a few other things you can try:
5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
This may help because npm has known issues with package hoisting which may get resolved in future versions.
6. Check if /Users/alexanderkaran/Documents/ClimateClever/cc_school_web/node_modules/webpack is outside your project directory.
For example, you might have accidentally installed something in your home folder.
7. Try running npm ls webpack in your project folder.
This will tell you which other package (apart from the expected react-scripts) installed webpack.
If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That would permanently disable this preflight check in case you want to proceed anyway.
P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! cc_school_web#0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the cc_school_web#0.1.0 start 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:
npm ERR! /Users/alexanderkaran/.npm/_logs/2020-01-23T05_20_16_003Z-debug.log
alexanderkaran#AlexandersMBP2 cc_school_web %
I know what your thinking you must have installed webpack, well I didn't. When I run:
npm ls webpack
I get this log out:
cc_school_web#0.1.0 /Users/alexanderkaran/Documents/ClimateClever/cc_school_web
├─┬ #storybook/react#5.3.8
│ ├─┬ #storybook/core#5.3.8
│ │ ├─┬ corejs-upgrade-webpack-plugin#2.2.0
│ │ │ └── webpack#4.41.5 deduped
│ │ └── webpack#4.41.5 deduped
│ └── webpack#4.41.5
└─┬ react-scripts#3.3.0
└── webpack#4.41.2
alexanderkaran#AlexandersMBP2 cc_school_web %
So the issue seems to be to do with storybook. If I uninstall all npm modules and remove storybook, then re-add both everything works again but eventually, it breaks.
Does anyone have a fix for this? Or am I missing something?
Looks like this an issue with the version of Webpack Create React App uses being a few steps behind the version Storybook uses sometimes.
There is no fix, but there are some workarounds.
You can find more info on the issue here:
https://github.com/storybookjs/storybook/issues/6505
One workaround is to add SKIP_PREFLIGHT_CHECK=true to a .env file in your project.
This can cause issue though.
Another workaround is to keep using a version of storybook and create react app that works with each other and doesn't cause these issues. So do not just upgrade storybook straight if it contains a Webpack version bump.

How do I only put dependencies into a specific folder for yarn?

Background
I have been using bower for handling dependencies, however now I would like to migrate to yarn. The main hurdle I am having is migrating from the below .bowerc file to .yarnrc.
.bowerrc
{
"directory": "src/vendors"
}
The issue is I could make a .yarnrc file, like below that will put any dependency into src/vendors, but that includes devDependencies.
.yarnrc
--modules-folder src/vendors
Question
How do I only put dependencies into src/vendors and putdevDependencies in node_modules?
An alternative way to accomplish the same thing as in the answer above (but without the .yarnrc files) is by adding two different scripts to package.json, something along these lines:
"scripts": {
"install-depends": "yarn install --production=true --modules-folder ./src/vendors",
"install-devDepends": "yarn install --production=false"
}
Then you just run them in that same order (if you do it the other way around, it will wipe out everything in the node_modules:
yarn run install-depends
yarn run install-devDepends
You could use --production option to tell yarn which dependencies you want to install; if set to true it will just install dependencies.
So in your src folder make a .yarnrc file with the following content:
--modules-folder vendors
--production true
and in your project dir, in .yarnrc file, set --production to false:
--production false
folder structure:
.
├── package.json
├── src
│   └── .yarnrc
└── .yarnrc

Categories