Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

--prod build doesn't understand require('./package') (in e.g. got library) #1148

Open
janpio opened this issue Jul 26, 2017 · 3 comments
Open

Comments

@janpio
Copy link

janpio commented Jul 26, 2017

Short description of the problem:

The Ionic build process with --prod does not handle packages with require('./package') (as for example in got) right now, but crashes with an error message like this:

Error: ./node_modules/got/index.js
Module not found: Error: Can't resolve './package' in 'C:\Users\Jan\Documents\gotTest\node_modules\got'
resolve './package' in 'C:\Users\Jan\Documents\gotTest\node_modules\got'
  using description file: C:\Users\Jan\Documents\gotTest\node_modules\got\package.json (relative path: .)
  after using description file: C:\Users\Jan\Documents\gotTest\node_modules\got\package.json (relative path: .)
    using description file: C:\Users\Jan\Documents\gotTest\node_modules\got\package.json (relative path: ./package)
      no extension
        C:\Users\Jan\Documents\gotTest\node_modules\got\package doesn't exist
      .js
        C:\Users\Jan\Documents\gotTest\node_modules\got\package.js doesn't exist
      .ts
        C:\Users\Jan\Documents\gotTest\node_modules\got\package.ts doesn't exist
      as directory
        C:\Users\Jan\Documents\gotTest\node_modules\got\package doesn't exist
[C:\Users\Jan\Documents\gotTest\node_modules\got\package]
[C:\Users\Jan\Documents\gotTest\node_modules\got\package.js]
[C:\Users\Jan\Documents\gotTest\node_modules\got\package.ts]
[C:\Users\Jan\Documents\gotTest\node_modules\got\package]
 @ ./node_modules/got/index.js 22:12-32
 @ ./src/pages/home/home.js
 @ ./src/app/app.module.js
 @ ./src/app/app.module.ngfactory.js
 @ ./src/app/main.ts
    at new BuildError (C:\Users\Jan\Documents\gotTest\node_modules\@ionic\app-scripts\dist\util\errors.js:16:28)
    at C:\Users\Jan\Documents\gotTest\node_modules\@ionic\app-scripts\dist\preprocess.js:18:21
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:169:7)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gotTest@0.0.1 ionic:build: `ionic-app-scripts build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the gotTest@0.0.1 ionic:build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

The suggested workaround is to configure webpack.config.js to include this:

resolve: {
    extensions: ['.js', '.json']
}

As I understood it this makes webpack look not only for package but also package.js and package.json.

Unfortunately this already seems to be included in the default webpack.confis.js of app-scripts:

  resolve: {
    extensions: ['.ts', '.js', '.json'],
    modules: [path.resolve('node_modules')]
  },

https://github.com/ionic-team/ionic-app-scripts/blob/master/config/webpack.config.js#L22-L25

What should I do now?

What behavior are you expecting?

This should not be a problem at all as a) package seems to be valid instead of package.json and b) app-scripts webpack is already configure to understand this.

Steps to reproduce:

  1. ionic start gotTest blank
  2. npm install got
  3. npm install electron --save-dev (workaround for [another webpack issue with got}(https://github.com/Using got in a project built with webpack crashes as electron is used, but not part of package.json sindresorhus/got#345), will be fixed with next release)
  4. Add the following code to home.ts:
import * as got from 'got';
...
const req = { method: 'GET', url: 'http://example.org' };
    got(req.url, req);
  1. Run npm run ionic:build --prod to see the error

(Or clone this repo: https://github.com/janpio/ionic-webpack-got-problem where I did exactly that)

Which @ionic/app-scripts version are you using?

2.0.2

Other information:

global packages:

@ionic/cli-utils : 1.5.0
Ionic CLI        : 3.5.0

local packages:

@ionic/app-scripts              : 2.0.2
@ionic/cli-plugin-ionic-angular : 1.3.2
Ionic Framework                 : ionic-angular 3.5.3

System:

Node       : v8.1.3
OS         : Windows 10
Xcode      : not installed
ios-deploy : not installed
ios-sim    : not installed
npm        : 5.1.0
@janpio
Copy link
Author

janpio commented Jul 28, 2017

@janpio
Copy link
Author

janpio commented Jul 28, 2017

Yes indeed, adding .json to that array allows the project to build.

I should have looked at the output a bit better:

...
[09:56:06]  deeplinks started ...
[09:56:07]  deeplinks finished in 478 ms
[09:56:07]  optimization started ...
[09:56:07]  copy finished in 16.79 s
[09:56:20]  ionic-app-script task: "build"
[09:56:20]  Error: ./node_modules/got/index.js Module not found: Error: Can't resolve './package' in
            'C:\Users\Jan\Documents\yatsa\node_modules\got' resolve './package' in
            'C:\Users\Jan\Documents\yatsa\node_modules\got' using description file:

This was have shown me that the problem was in the "optimization" step, not "webpack".

I will PR this.

@tlaverdure
Copy link

@danbucholtz I see that you've started to make some changes: 24cb5ca#diff-1a40e6c8e8713d4af22007160e07a446 that look related to this issue and PR

Any idea on when a new release will be shipped? I've got a dependency that requires a package file and I need to add to my production build.

Code Example

const version = require('./package').version

Error

Error: ./node_modules/analytics-node/index.js
Module not found: Error: Can't resolve './package' in...

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants