cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

how to include node_modules for the MTA build

lucky_li
Associate
Associate
2,507

By default, the node_modules will not be included into the content during the mbt build process. Whether there are some setting to force the mta to include the node_modules? As our project need to change some files after do the normal npm install'.

One trick I can figure out is that: we rename the "node_modules" to another name, and in the "start" script we build a softlink.

Accepted Solutions (0)

Answers (4)

Answers (4)

gregorw
SAP Mentor
SAP Mentor

Please check if the modules in your mta.yaml contain:

    build-parameters:
      ignore: ["node_modules/"]
or the source folders of your module contain a .cfignore with this content:
node_modules
But you should not include the node_modules into your Git repository. Only add it to the MTAR.
lucky_li
Associate
Associate
0 Likes

I find a way that can work:

1: use the "zip" builder, so it will zip all content

- name: test-simple-express-oo

type: nodejs

build-parameters:

builder: zip

path: ./app

parameters:

buildpack: nodejs_buildpack

2: Put the "node_modules" in the second level ( if put it in the first level, then the node-buildpack will overwrite it)

3: the "start" script in package.json will start the file in the second level. As it has the "node_modules" in the dir, so it can start successfully.

lucky_li
Associate
Associate
0 Likes

Just try another way to resolve the issue, but it failed also.

1: First install the library via "npm i""

2: Change the "node_modules" to another name like "node_modules_content"

3: In the package.json, change the script to create a soft link from the node_modules_content to 'node_modules' like the following

"scripts": {

"start": "ln -s node_modules_content node_modules; node --inspect ./index.js"

},

4: Run the "mbt build" to build the mta. So now the node_modules_content will be included

I guess this way should work. But during the test, it will fail with the error

"Cannot find module".

lucky_li
Associate
Associate
0 Likes

I think the reason (just guess,need to check the steps provided by the nodejs-buildpacks): for the nodejs application, even the package.json don't include any dependency, it will create the node_modules. this node_modules will overwrite the original node_module.

I try another way, it still not work:

try {

if (!fs.existsSync("./node_modules")){

console.error(" !! node_modules not existed, rename it");

fs.renameSync("./node_modules_content", "./node_modules", "dir");

// in window, it runs fine. But in BTP, maybe it is not really sync

while (true) {

if (!fs.existsSync("./node_modules")) {

console.error(" node_modules not exist afer the symlinkSync");

} else {

console.error(" **** now node_modules exists,");

break;

}

}

tryStartExpress();

} else {

console.error(" ** node_modules existed");

tryStartExpress();

}

} catch (error) {

console.error("$$$ error happend:", error);

}

Yogananda
Product and Topic Expert
Product and Topic Expert
0 Likes
lucky.li

Node Modules will consume high storage and what is the puropose and advantage of having node modules into cf app directory ??

lucky_li
Associate
Associate
0 Likes

Our use case is special. We use the Ghost CMS solution and you can get the the install process from https://ghost.org/docs/install/local/. In short, it will first install "ghost-cli", then use the installed application to do the install. The install process is very very slow. We tested it in the ali-cloud and found that it will fail due to the "timeout" error. And another reason is that we need to do some code change based on the installed library.

So we need to store the node_modules into the githhub and include it into the mta during the build process.