Pact-js: Angular2 / TypeScript -> Module not found: Error: Can't resolve 'child_process

Created on 30 Jan 2017  Â·  26Comments  Â·  Source: pact-foundation/pact-js

I have recently added PACT using depreciate DSL in our Angular2 project. We are in process of upgrading to current version and as per this repository instructions. This is how the output looks like

`$ ng test

WARNING in ./~/@pact-foundation/pact-node/src/publisher.js
163:12-24 Critical dependency: the request of a dependency is an expression

WARNING in ./~/pact/src/common/config.js
21:19-44 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@pact-foundation/pact-node/~/@pact-foundation/pact-mock-service/index.js
7:17-37 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@pact-foundation/pact-node/~/@pact-foundation/pact-provider-verifier/index.js
7:17-37 Critical dependency: the request of a dependency is an expression

ERROR in ./~/@pact-foundation/pact-node/src/server.js
Module not found: Error: Can't resolve 'child_process' in '/tmp/AngularProject/node_modules/@pact-foundation/pact-node/src'
@ ./~/@pact-foundation/pact-node/src/server.js 8:6-30
@ ./~/@pact-foundation/pact-node/src/pact.js
@ ./src/app/api/booking.service.spec.ts
@ ./src .spec.ts
@ ./src/test.ts

ERROR in ./~/@pact-foundation/pact-node/src/verifier.js
Module not found: Error: Can't resolve 'child_process' in '/tmp/AngularProject/node_modules/@pact-foundation/pact-node/src'
@ ./~/@pact-foundation/pact-node/src/verifier.js 6:6-30
@ ./~/@pact-foundation/pact-node/src/pact.js
@ ./src/app/api/booking.service.spec.ts
@ ./src .spec.ts
@ ./src/test.ts
30 01 2017 15:33:05.350:WARN [karma]: No captured browser, open http://localhost:9876/
`

Any help on this?

Awaiting feedback

Most helpful comment

I had the same problem, not with pact, but Ionic + Angular + Karma. The problem for me was caused by Webstorm sticking a bad import in one of my spec files:

import { describe } from "selenium-webdriver/testing";

Removed it and all got back to normal

All 26 comments

Hi @NomiJ, I'm not familiar with Angular2 testing practices, but based on the log file you've provided it seems to use Karma under the hood. Have you looked at the karma examples for any hints?

In any case, child_process is only available in a proper Node process, not any JS execution environment (such as a browser). You will need to start the mock service _independently_ of your test suite for this to work. Typically, this is orchestrated via bash/grunt/gulp/.

If you could provide a working example to repro that'd be even better.

@NomiJ to further what @mefellows is saying, Pact should never be run in the browser and needs to be run in a node environment. If you are doing unit tests with angular, you'll need something to run pact-node first (could be done with the build tool or npm scripts), then use pact-js to communicate the interactions with it. It could also be done using protractor if you want to do end to end tests since protractor runs fully in node.

@NomiJ have you got any update on this?

Hi,
Sorry for late reply. Explanation make sense. Due to shortage of time I ended up moving to Depreciated DSL here (https://github.com/DiUS/pact-consumer-js-dsl), yet would definitely like to upgrade to maintained package. I guess, i can always spawn the server as a separate process or gulp task. I will be creating a example soon and share it with you.

I think i am missing something basic. Here I have created a basic angular2 App created with Angular-Cli (Release 1.0). I added @pact-foundation/pact-js and pact dependencies and created a basic "SayHello" test scenario.

https://github.com/NomiJ/Pact-Angular2-Example/blob/master/src/app/animal.service.spec.ts

Now running ng test outputs this
`
WARNING in ./~/source-map-support/source-map-support.js
Module not found: Error: Can't resolve 'module' in '/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/source-map-support'
@ ./~/source-map-support/source-map-support.js 474:15-32
@ ./~/bunyan/lib/bunyan.js
@ ./~/@pact-foundation/pact-node/src/logger.js
@ ./~/@pact-foundation/pact-node/src/pact.js
@ ./~/pact/src/pact.js
@ ./src/app/animal.service.spec.ts
@ ./src .spec.ts$
@ ./src/test.ts

WARNING in ./~/pact/src/common/config.js
21:19-44 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@pact-foundation/pact-node/src/publisher.js
163:12-24 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@pact-foundation/pact-mock-service/index.js
7:17-37 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@pact-foundation/pact-provider-verifier/index.js
7:17-37 Critical dependency: the request of a dependency is an expression

ERROR in ./~/@pact-foundation/pact-node/src/server.js
Module not found: Error: Can't resolve 'child_process' in '/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/@pact-foundation/pact-node/src'
@ ./~/@pact-foundation/pact-node/src/server.js 8:6-30
@ ./~/@pact-foundation/pact-node/src/pact.js
@ ./~/pact/src/pact.js
@ ./src/app/animal.service.spec.ts
@ ./src .spec.ts$
@ ./src/test.ts

ERROR in ./~/@pact-foundation/pact-node/src/verifier.js
Module not found: Error: Can't resolve 'child_process' in '/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/@pact-foundation/pact-node/src'
@ ./~/@pact-foundation/pact-node/src/verifier.js 6:6-30
@ ./~/@pact-foundation/pact-node/src/pact.js
@ ./~/pact/src/pact.js
@ ./src/app/animal.service.spec.ts
@ ./src .spec.ts$
@ ./src/test.ts
28 02 2017 22:42:02.590:ERROR [karma]: { Error
at MemoryFileSystem.readFileSync (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/memory-fs/lib/MemoryFileSystem.js:107:10)
at MemoryFileSystem.(anonymous function) [as readFile] (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/memory-fs/lib/MemoryFileSystem.js:300:34)
at doRead (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/karma-webpack/lib/karma-webpack.js:205:29)
at Plugin.readFile (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/karma-webpack/lib/karma-webpack.js:210:7)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
code: 'ENOENT',
errno: 34,
message: 'no such file or directory',
path: '/_karma_webpack_/src/polyfills.ts' }
28 02 2017 22:42:02.607:ERROR [karma]: { Error
at MemoryFileSystem.readFileSync (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/memory-fs/lib/MemoryFileSystem.js:107:10)
at MemoryFileSystem.(anonymous function) [as readFile] (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/memory-fs/lib/MemoryFileSystem.js:300:34)
at doRead (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/karma-webpack/lib/karma-webpack.js:205:29)
at Plugin.readFile (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/karma-webpack/lib/karma-webpack.js:210:7)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
code: 'ENOENT',
errno: 34,
message: 'no such file or directory',
path: '/_karma_webpack_/src/test.ts' }`

My Environment:

@angular/cli: 1.0.0-rc.0 node: 7.0.0 os: darwin x64 @angular/common: 2.4.8 @angular/compiler: 2.4.8 @angular/core: 2.4.8 @angular/forms: 2.4.8 @angular/http: 2.4.8 @angular/platform-browser: 2.4.8 @angular/platform-browser-dynamic: 2.4.8 @angular/router: 3.4.8 @angular/cli: 1.0.0-rc.0 @angular/compiler-cli: 2.4.8 npm: 4.0.1

If i uninstall "@pact-foundation/pact-node", I get pact complaining like
ERROR in ./~/pact/src/pact.js Module not found: Error: Can't resolve '@pact-foundation/pact-node' in '/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/pact/src' @ ./~/pact/src/pact.js 16:23-60 @ ./src/app/animal.service.spec.ts @ ./src \.spec\.ts$ @ ./src/test.ts

Any update?

OK part of the problem here is that both the consumer and the provider side of Pact JS must have access to a runtime environment outside of the browser (it needs to be able to spin up web servers and command-line utilities) - this use case is similar to Karma.

Take a look at the karma test for an example of how to do this, but for now I don't think it is possible to do what you want to do with the pact npm package.

That being said, the code exists and is published as a release. You could certainly pull this down, and use the pact-karma package for this. I'll raise an issue to rename it because AFAIK there is nothing Karma specific in it.

(You might be able to do something dodgey like const PactWeb = require('pact/dist/pact.web.js'); but this is getting into juggling chainsaw territory - do it at your own risk).

I am in the same boat, any help is appreciated

Hi there,

I've just pushed up a test branch https://github.com/pact-foundation/pact-js/tree/feature/pact-web that avoids pulling in the Pact Node library - the cause of all of the above errors.

Could you please test out pointing to this GH branch instead of the published package to see if this addresses your issues?

You'll need to do something like the following to your package.json:

"devDependencies": {
   "pact": "pact-foundation/pact-js#feature\/pact-web",
}

And when you create your Pact object:

    provider = Pact({
      consumer: 'Karma Jasmine',
      provider: 'Hello',
      web: true // <- this is the bit that matters
    });

The bits that use pact-node are now wrapped inside functions that shouldn't be called by your code, I'm _hoping_ that dead code elimination from tools like Webpack, Browserify or the Angular tooling should prune these imports. Else, we'll need a separate module release for this purpose.

sure, i will test and report back

Thanks!

On Apr 17, 2017 10:38 PM, "Nomi" notifications@github.com wrote:

sure, i will test and report back

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/pact-foundation/pact-js/issues/20#issuecomment-294476375,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AADSjME9ahFM9s6Llc_Px2vr_jiOhgnMks5rw10ygaJpZM4LxCqJ
.

do i need keep this?
>> "@pact-foundation/pact-node": "4.8.0",
Thats how the current package.json looks like
"devDependencies": { "@angular/cli": "1.0.0-rc.0", "@angular/compiler-cli": "^2.4.0", "@pact-foundation/pact-node": "4.8.0", "@types/jasmine": "2.5.38", "@types/node": "~6.0.60", "codelyzer": "~2.0.0", "jasmine-core": "~2.5.2", "jasmine-spec-reporter": "~3.2.0", "karma": "~1.4.1", "karma-chrome-launcher": "~2.0.0", "karma-cli": "~1.0.1", "karma-coverage-istanbul-reporter": "^0.2.0", "karma-jasmine": "~1.1.0", "karma-jasmine-html-reporter": "^0.2.2", "pact": "pact-foundation/pact-js#feature\/pact-web", "protractor": "~5.1.0", "ts-node": "~2.0.0", "tslint": "~4.4.2", "typescript": "~2.0.0" }

You will still need to start a mock server before running the tests and
stop it after. You can use that package to do it (recommend) or manually
run the Ruby mock server yourself

On Apr 17, 2017 10:49 PM, "Nomi" notifications@github.com wrote:

do i need keep this?

"@pact-foundation/pact-node": "4.8.0",
Thats how the current package.json looks like
"devDependencies": { "@angular/cli": "1.0.0-rc.0",
"@angular/compiler-cli": "^2.4.0", "@pact-foundation/pact-node": "4.8.0",
"@types/jasmine": "2.5.38", "@types/node": "~6.0.60", "codelyzer":
"~2.0.0", "jasmine-core": "~2.5.2", "jasmine-spec-reporter": "~3.2.0",
"karma": "~1.4.1", "karma-chrome-launcher": "~2.0.0", "karma-cli":
"~1.0.1", "karma-coverage-istanbul-reporter": "^0.2.0", "karma-jasmine":
"~1.1.0", "karma-jasmine-html-reporter": "^0.2.2", "pact":
"pact-foundation/pact-js#feature\/pact-web", "protractor": "~5.1.0",
"ts-node": "~2.0.0", "tslint": "~4.4.2", "typescript": "~2.0.0" }

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/pact-foundation/pact-js/issues/20#issuecomment-294480042,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AADSjKpES7rFyaOJYJZJ_LV-WFiB9uX1ks5rw1_mgaJpZM4LxCqJ
.

Probably its a must a to run the mock server for tests to execute correctly, yet, i did not do it, considering the error
`
ERROR in ./~/@pact-foundation/pact-node/src/server.js
Module not found: Error: Can't resolve 'child_process' in '/private/tmp/Pact-Angular2-Example/node_modules/@pact-foundation/pact-node/src'
@ ./~/@pact-foundation/pact-node/src/server.js 8:6-30
@ ./~/@pact-foundation/pact-node/src/pact.js
@ ./~/pact/src/pact.js
@ ./src/app/animal.service.spec.ts
@ ./src .spec.ts$
@ ./src/test.ts

ERROR in ./~/@pact-foundation/pact-node/src/verifier.js
Module not found: Error: Can't resolve 'child_process' in '/private/tmp/Pact-Angular2-Example/node_modules/@pact-foundation/pact-node/src'
@ ./~/@pact-foundation/pact-node/src/verifier.js 6:6-30
@ ./~/@pact-foundation/pact-node/src/pact.js
@ ./~/pact/src/pact.js
@ ./src/app/animal.service.spec.ts
@ ./src .spec.ts$
@ ./src/test.ts`

Just to be sure, after npm install, i verified existence of your recent changes in pact.js, (addition of web ).

What is pact-karma.js is about? Since Angular2 uses karma in my template. I checked your sample for karma-jasmine, yet it seems to load file from dist, in karma.conf.js, which probably require runningnpm dist, to load pact.web.js from it. Is that how it suppose to be?
Would really appreciate a Typescript or angular2 sample from pact team.

Pact Karma is a module that will automatically start a Mock Server for Karma tests. You can pull that in with the npm package karma-pact.

The pact-karma.js file is the way to run Pact in a Browser (or non-Node environment). I'm thinking that it might make more sense to rename this, since there's no reason why it can't be used in non-Karma environments.

yet it seems to load file from dist, in karma.conf.js, which probably require runningnpm dist, to load pact.web.js from it. Is that how it suppose to be?

You could, although it has a release for the DSL. I'm not sure why this isn't released as an npm or bower style package, I'm following this up separately.

The Pact Karma plugin which manages the Mock Server has an npm package that you can pull down.

This is the ideal way to approach this.

OK, just had a chat with the original author of the Karma stuff. I'm looking into how we can release that thing separately. For now, you'll need to find a manual way of pulling down that pact.web.js file.

Sorry about the confusion!

Hi @NomiJ, @JimiShah , I have renamed the karma package to pact-web and published a test package to https://www.npmjs.com/package/pact-web.

Would either of you care to give this a try?

Hey @mefellows ,
Thanks, I will give it a go and let you know soon.

Hey @mefellows,
Thanxx
Got this working with the following package.json:
{
..
"karma-pact": "0.0.7",
"karma-phantomjs-launcher": "~1.0.4",
"pact": "^2.5.0",
"pact-web": "^2.5.0",
..
}
In my client.spec.ts I use:
let Pact = require('pact-web')
let provider = Pact({ consumer: 'Karma Jasmine', provider: 'Hello', web: true })

Excellent!

Also works for me. Anyone looking for example can look at

https://github.com/NomiJ/Pact-Angular2-Example

Great! Thanks Nomi.

On May 23, 2017 10:50 PM, "Nomi" notifications@github.com wrote:

Also works for me. Anyone looking for example can look at

https://github.com/NomiJ/Pact-Angular2-Example

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/pact-foundation/pact-js/issues/20#issuecomment-303387492,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AADSjJY_2xrIF1a7rF2CZ9-ZZxKP6v60ks5r8tYLgaJpZM4LxCqJ
.

Hey @NomiJ this is not working for me. I'm using angular2 and Karma+Jasmine. is this outdated?

I did not update the example, afterwards. I am sorry, its not working for you. Whats the problem that you are facing ?

I had the same problem, not with pact, but Ionic + Angular + Karma. The problem for me was caused by Webstorm sticking a bad import in one of my spec files:

import { describe } from "selenium-webdriver/testing";

Removed it and all got back to normal

Was this page helpful?
0 / 5 - 0 ratings