I get a dir structure like below.
|-root
|-package.json
|-node_modules
|-01
|-package.json
|-02
|-package.json
Could I just share the node_modules which locate in root?because the dependencies of other subDir(like 01 and 02) is same with the root's. I don't want to install the same npm package again. I need the separate package.json in subDir because it will contain different scripts to run different tasks. put all the script inside the root package.json is messy.
I have try symlink, but it dosen't work. npm log package not found.
export NODE_PATH='yourdir'/node_modules
Refer Node documentation https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
problem solved. symlink is work. I made a mistake when use command ln.
# in root dir
# wrong
> ln -s ./node_modules ./01
# right
> ln -s /full/path/to/root/node_modules /full/path/to/root/01
Related
I am trying to replicate the behavior of npm pack because it has a limitation where it does not write to stdout, it can only write to a local file (see this issue: https://github.com/npm/npm/issues/12039)
Now, I can tar the current directory and write to stdout like so:
tar --exclude='./node_modules/' -cv . | gzip > archive.tar.gz
however, when you extract npm pack tarballs, all the contents of a your package should be in a directory called 'package'.
So my question is - how can I tarball the current directory, but put the current directory inside a directory called 'package' before it gets archived?
Is there some tar -flag that lets you do that?
I did some legwork and as far as my testing goes, npm will accept a tarball with everything in the root, or everything in a subdirectory called 'package'.
To test the above theory, you can tar a NPM project directory with:
tar --exclude='node_modules' -c . > archive.tar
then install it somewhere else with
npm install /path/to/archive.tar
you can't install in the same project though, NPM will complain about circular deps, so install it in another project.
When I run npm install I find in some installed packages some folders like : docs, samples, node_modules ...
When I was using bower I used this :
"ignore": [
"node_modules/**",
"**/node_modules",
".settings/**",
"src/test/**",
"target/**",
"src/main/resources/**",
"samples/**",
"docs/**"
]
How can I do this using npm ?
As per the NPM documentation on ignore (https://github.com/npm/cli/wiki/Files-&-Ignores#npmignore), in a file named .npmignore you can set the ignore list:
tests/
src/
samples/
Just as with bower, you list the files and folders you want to ignore.
Create a .npmignore and add files to ignore
docs/
files_to_ignore/
I have been using an NPM for angular-4 which support drag and drop objects (ng2-drag-drop). I found a missing functionality and decide to add it to the package.
What I did is forking the original project and adding my changes. after commit/push to my git I then used the following command to install my NPM :
npm install https://github.com/..... --save
the NPM installed successfully however when looking in my node_modules I see that the source files are missing and I have only the root directory including the package.json and some other files . any source files are missing.
I then tried to instal the NPM directly from the author git so instead of running :
npm install ng2-drag-drop --save
I used
npm install https://github.com/ObaidUrRehman/ng2-drag-drop.git --save
and I had the same issue with my fork.
Why the installation is different between the author git and the named package ? isn't it taking the files from the same location ? if no, what should I do to make it work ?
The reason you are not able to see the src folder is
If you see the git repo you will find two files
gitignore & npmignore.
In that npm ignore file you will find the src has been ignored to be prevent it from being added to the package when running npm commands .
Keeping files out of your package
Use a .npmignore file to keep stuff out of your package. If there's no
.npmignore file, but there is a .gitignore file, then npm will ignore
the stuff matched by the .gitignore file. If you want to include
something that is excluded by your .gitignore file, you can create an
empty .npmignore file to override it. Like git, npm looks for
.npmignore and .gitignore files in all subdirectories of your package,
not only the root directory.
You need to overwrite these settings to be able to get src contents in node modules when you do npm install
I have deleted the folder: "node_modules" from root folder(gave the source code to someone) because I think this contain packages that we can get any time.
How can I get those files back?
thanks in advance!
Do you have a package.json in your directory? If so, you can run npm i to reinstall the project dependencies ( a.k.a bring back your node_modules ).
You must have a package.json in your source's root folder. If that's the case, do $ npm install, it will rebuild all modules.
If you don't have package.json, run $ npm init, add your modules, then run $npm install.
How to streamline npm release when I want to include only specific path(s)?
I have ./src and ./dist files in my repository. I want to effectively publish only the contents of ./dist + ./package.json.
Using .npmignore to ignore ./src will simply ignore the ./src folder. I want to include only the contents of ./dist, i.e. now user would need to do require('my-package/dist/something'). I want to make it require('my-package/something'). ./something is contained in ./dist.
The way I have done it at the moment is, I have created a bash script:
npm run build
cp package.json ./dist
# or, if you need to have package.json "main" entry different,
# e.g. for being able to use `npm link`, you need to replace "main" value:
# sed 's#"main": "./dist/index.js"#"main": "./index.js"#' package.json > ./dist/package.json
cd ./dist
npm publish
For cross-platform compatibilty use shx:
npm run build
shx cp package.json ./dist
shx cd ./dist
npm publish