Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How include node_modules in output directory with TypeScript

I want to know if it possible copy node_modules folder into output directory after run tsc command.

My situation it that I have a project with TypeScript and use some npm packages. And i need that my output directory has all npm dependencies, because i need to compress it and send by http (to AWS Lambda).

My project structure is like this:

|-.vscode --> visual studio code |-lib --> output dir |-node_modules --> npm dependencies |-src --> .ts files |-jsconfig.json |-tsconfig.json 

How can achieve it?

Thanks a lot!

like image 914
Vladimir Venegas Avatar asked Dec 12 '16 21:12

Vladimir Venegas


People also ask

Should I include node_modules in git?

You should not include folder node_modules in your . gitignore file (or rather you should include folder node_modules in your source deployed to Heroku). If folder node_modules: exists then npm install will use those vendored libraries and will rebuild any binary dependencies with npm rebuild .

Where do I put node modules?

Node Modules Packages are dropped into the node_modules folder under the prefix . When installing locally, this means that you can require("packagename") to load its main module, or require("packagename/lib/path/to/sub/module") to load other modules. Global installs on Unix systems go to {prefix}/lib/node_modules .

Are node modules included in build?

Node. js has a set of built-in modules which you can use without any further installation.

What is .staging folder in node_modules?

While doing npm install, inside node_modules . staging folder is getting created. Reasons: This is a temporary folder where the modules will be kept untill npm downloads all the modules specified in the package. json.


1 Answers

I like Peopleware's solution a lot more than the accepted solution, but you don't even need gulp for that. You can simply do this in your package.json:

{   "scripts": {     "build": "tsc <your command line options>",     "postbuild": "cp package.json dist/package.json && cd dist && npm install --only=production"   } } 

The benefit of doing it this way is that you're not copying the entirety of your node_modules folder, because it might have a ton of dependencies only used during development.

You can do the same with static assets such as images or what not with:

"copy-statics": "cp -r static dist/static" 

Update: instead of using npm install --only-production it is safer to copy both package.json and package-lock.json and then run npm ci --production. This ensures that you only install the dependency snapshot that you have in your package-lock.json. So the command would look like this:

"postbuild": "cp package.json dist/package.json && cp package-lock.json dist/package-lock.json && cd dist && npm ci --production" 
like image 193
Kerim Güney Avatar answered Sep 19 '22 13:09

Kerim Güney