1

我正在尝试将我的角度模块导出为单独的 npm 模块。

所以我试图用 npm ( "packagr": "ng-packagr -p ng-package.json"

一切顺利,直到“编译到 FESM15”:

我在下面粘贴日志:


ng-packagr -p ng-package.json

Building Angular library
Generating bundle for widget
Cleaning bundle build directory
Processing assets
Running ngc
Compiling to FESM15
'fs' is imported by node_modules/jsdom/lib/jsdom.js, but could not be resolved – treating it as an external dependency
'path' is imported by node_modules/jsdom/lib/jsdom.js, but could not be resolved – treating it as an external dependency
'net' is imported by node_modules/tough-cookie/lib/cookie.js, but could not be resolved – treating it as an external dependency
preferring built-in module 'url' over local alternative at '/home/test/codeProjects/widget/node_modules/url/url.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'url' over local alternative at '/home/test/codeProjects/widget/node_modules/url/url.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
'url' is imported by node_modules/tough-cookie/lib/cookie.js, but could not be resolved – treating it as an external dependency
preferring built-in module 'punycode' over local alternative at '/home/test/codeProjects/widget/node_modules/punycode/punycode.js',pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'punycode' over local alternative at '/home/test/codeProjects/widget/node_modules/punycode/punycode.js',pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
'punycode' is imported by node_modules/tough-cookie/lib/pubsuffix.js, but could not be resolved – treating it as an external dependency
preferring built-in module 'punycode' over local alternative at '/home/test/codeProjects/widget/node_modules/punycode/punycode.js',pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'punycode' over local alternative at '/home/test/codeProjects/widget/node_modules/punycode/punycode.js',pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'punycode' over local alternative at '/home/test/codeProjects/widget/node_modules/punycode/punycode.js',pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
'punycode' is imported by commonjs-external:punycode, but could not be resolved – treating it as an external dependency
preferring built-in module 'util' over local alternative at '/home/test/codeProjects/widget/node_modules/util/util.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'util' over local alternative at '/home/test/codeProjects/widget/node_modules/util/util.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
'util' is imported by node_modules/tough-cookie/lib/memstore.js, but could not be resolved – treating it as an external dependency
preferring built-in module 'util' over local alternative at '/home/test/codeProjects/widget/node_modules/util/util.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'util' over local alternative at '/home/test/codeProjects/widget/node_modules/util/util.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
preferring built-in module 'util' over local alternative at '/home/test/codeProjects/widget/node_modules/util/util.js', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning
'util' is imported by commonjs-external:util, but could not be resolved – treating it as an external dependency

BUILD ERROR
Unexpected token
Error: Unexpected token
    at error (/home/test/codeProjects/widget/node_modules/rollup/dist/rollup.js:185:14)
    at Module.error (/home/test/codeProjects/widget/node_modules/rollup/dist/rollup.js:16526:3)
    at tryParse (/home/test/codeProjects/widget/node_modules/rollup/dist/rollup.js:16220:10)
    at new Module (/home/test/codeProjects/widget/node_modules/rollup/dist/rollup.js:16264:15)
    at load.catch.then.then.then.source (/home/test/codeProjects/widget/node_modules/rollup/dist/rollup.js:18189:20)
    at 

npm ERR! code ELIFECYCLE
npm ERR! errno 111
npm ERR! widget@0.0.0 packagr: `ng-packagr -p ng-package.json`
npm ERR! Exit status 111
npm ERR!
npm ERR! Failed at the widget@0.0.0 packagr script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

tsconfig.json

  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

在我的 package.json 下面:


{
  "name": "widget",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "packagr": "ng-packagr -p ng-package.json",
    "prepublish": "tsc"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.0.0",
    "@angular/common": "^5.0.0",
    "@angular/compiler": "^5.0.0",
    "@angular/core": "^5.0.0",
    "@angular/forms": "^5.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "^5.0.0",
    "@angular/platform-browser-dynamic": "^5.0.0",
    "@angular/router": "^5.0.0",
    "core-js": "^2.4.1",
    "fabric": "^2.0.0-rc.3",
    "rxjs": "^5.5.2",
    "zone.js": "^0.8.14"
  },
  "devDependencies": {
    "@angular/cli": "1.5.4",
    "@angular/compiler-cli": "^5.0.0",
    "@angular/language-service": "^5.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "ng-packagr": "^1.6.0",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.4.2"
  },

}

不知道为什么该软件包不想构建。当我正常构建模块(npm run build)或什至用于生产(npm run build -- --prod)时,一切似乎都可以正常工作。

谢谢。

4

1 回答 1

0

我遇到过同样的问题。我通过将错误中提到的模块添加到ng-package.json. IE:

{
  "$schema": "./node_modules/ng-packagr/ng-package.schema.json",
  "lib": {
    "entryFile": "public_api.ts",
    "externals": {
        "jsdom": "jsdom",
        "tough-cookie": "tough-cookie"
        // etc (or maybe you can localize the module that is using
        // them and use that module instead)
    }
  }
}
于 2018-01-24T15:45:21.443 回答