当前位置: 首页 > 面试题库 >

Angular4 + Universal + ng-bootstrap出现“意外令牌导入”错误

夏经武
2023-03-14
问题内容

因此,我升级到了angular 4+,以便可以将通用包用于SEO的服务器端渲染。我通过将其升级到angular 4+在现有的angular
2+项目中实现了这一点,并从https://github.com/evertonrobertoauler/cli-universal-
demo/commit/a2610286bd3db5d4f4cce4318d7c220c11963eb6
进行了所有必要的配置。

我使用ng-bootstrap https://ng-
bootstrap.github.io/#/home(v1.0.0-alpha.22)只有一个区别。但是,当我使用 npm run start-u-dev
运行节点服务器时,在ng-bootstrap中出现import关键字错误。

app-ui@0.0.0 start-u-dev /Users/giric/Projects/apnaDoctor/webapp/appUI  
ts-node src/server.ts

/appUI/node_modules/@ng-bootstrap/ng-bootstrap/accordion/accordion.module.js:1
(function (exports, require, module, __filename, __dirname) { import { NgModule } from '@angular/core';  
                                                              ^^^^^^  
SyntaxError: Unexpected token import  
    at createScript (vm.js:53:10)  
    at Object.runInThisContext (vm.js:95:10)  
    at Module._compile (module.js:543:28)  
    at Object.Module._extensions..js (module.js:580:10)  
    at Module.load (module.js:488:32)  
    at tryModuleLoad (module.js:447:12)  
    at Function.Module._load (module.js:439:3)  
    at Module.require (module.js:498:17)  
    at require (internal/module.js:20:19)  
    at Object.<anonymous> (/appUI/dist/ngfactory/src/app/app.server.module.ngfa
ctory.ts:18:1)  
    at Module._compile (module.js:571:32)  
    at Module.m._compile (/appUI/node_modules/ts-node/src/index.ts:406:23)  
    at Module._extensions..js (module.js:580:10)  
    at Object.require.extensions.(anonymous function) [as .ts]
(/webapp/appUI/node_mod
ules/ts-node/src/index.ts:409:12)  
    at Module.load (module.js:488:32)

pm ERR! Darwin 16.6.0  
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "start-u-dev"  
npm ERR! node v7.7.4  
npm ERR! npm  v4.1.2  
npm ERR! code ELIFECYCLE  
npm ERR! app-ui@0.0.0 start-u-dev: `ts-node src/server.ts`  
npm ERR! Exit status 1  
npm ERR!  
npm ERR! Failed at the app-ui@0.0.0 start-u-dev script 'ts-node src/server.ts'.  
npm ERR! Make sure you have the latest version of node.js and npm installed.  
npm ERR! If you do, this is most likely a problem with the app-ui package,  
npm ERR! not with npm itself.  
npm ERR! Tell the author that this fails on your system:  
npm ERR!     ts-node src/server.ts  
npm ERR! You can get information on how to open an issue for this project with:  
npm ERR!     npm bugs app-ui  
npm ERR! Or if that isn't available, you can get their info via:  
npm ERR!     npm owner ls app-ui  
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:

这就是我的package.json的样子

{  
  "name": "app-ui",  
  "version": "0.0.0",  
  "license": "MIT",  
  "scripts": {  
    "ng": "ng",  
    "start": "ng serve",  
    "build": "ng build",  
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "prestart": "npm install",
    "postinstall": "bower install",
    "prestart-u-dev": "npm install && ng build && ngc",
    "start-u-dev": "ts-node src/server.ts",
    "prestart-u-prod": "npm install && ng build --prod && ngc",
    "start-u-prod": "ts-node src/server.ts"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.1",
    "@angular/cli": "^1.0.0-rc.1",
    "@angular/common": "^4.0.1",
    "@angular/compiler": "^4.0.1",
    "@angular/compiler-cli": "^4.0.1",
    "@angular/core": "^4.0.1",
    "@angular/forms": "^4.0.1",
    "@angular/http": "^4.0.1",
    "@angular/platform-browser": "^4.0.1",
    "@angular/platform-browser-dynamic": "^4.0.1",
    "@angular/platform-server": "^4.0.1",
    "@angular/router": "^4.0.1",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.22",
    "angular2-toaster": "^3.0.1",
    "core-js": "^2.4.1",
    "ng2-webstorage": "^1.5.1",
    "rxjs": "^5.1.0",
    "rxjs-es": "^5.0.0-beta.12",
    "typescript": "^2.2.2",
    "ui-router-ng2": "^1.0.0-beta.4",
    "zone.js": "^0.7.6"
  },
  "devDependencies": {
    "@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",
    "node-sass": "^4.5.0",
    "protractor": "~5.1.0",
    "raw-loader": "^0.5.1",
    "sass-loader": "^6.0.3",
    "ts-node": "~2.0.0",
    "tslint": "~4.4.2"
  }
}

更新:早些时候我删除了angular2-toaster,但是后来我再次添加了它,只是为了确保它不仅仅是抛出相同错误的toaster模块。事实证明,angular2-toaster和ng-
bootstrap都引发了相同的错误。

更新2:当我使用 ng server 运行 服务器时 ,它工作正常,使用ng-
bootstrap和angular2-toaster以及angular 4+都没有问题

有什么建议?提前致谢。


问题答案:

对于那些试图了解解决方案是什么的人,您基本上想使用和白名单angular2-toaster以及其他模块来解决此问题。

  1. 如果使用angular-cli,请使用ng eject创建一个webpack.config.js
  2. webpack-node-externals通过安装依赖项,npm install并在webpack.config.js文件中添加以下行

```
const nodeExternals = require(‘webpack-node-externals’);

module.exports = {
  ...
  target: 'node',
  externals: [nodeExternals({
    whitelist: [
      /^@ng-bootstrap\/ng-bootstrap/,
      /^angular2\-toaster/,
    ]
  })],
  ...
}

```



 类似资料:
  • 我已经为连接的组件创建了测试。在一个文件中,我导入位于项目根文件夹外部的“本地化”模块。它没有被编译到ES5,Jest报告了同步错误:意外令牌导出。如何强制Jest转换该文件? 我有。带环境预设的巴别LRC。 package.json

  • 下面是我在GitHub上的项目位置: https://github.com/nandy2013/mern-stack-dev 我只是尝试将该方法从js文件导入到另一个js文件中,实现如下: 请帮忙!

  • 在设置Javascript Dev环境时,我遇到了以下错误。 我想使用代替。 我用了巴别塔。 我从以下方法尝试了许多解决方案,但是,不起作用或我。 节点错误:语法错误:意外的令牌导入 Node.js -语法错误:意外的标记导入 srcServer.js 。巴伯尔克

  • 问题内容: 我不明白怎么了。我在其他论坛上讨论了翻译和通天塔。我需要做什么? 我的代码: 和错误 问题答案: ES6导入是最近引入的功能,并且Node的当前稳定版本尚不支持它们。Node.js问题跟踪器对此存在一个未解决的问题 -但在V8和Node添加对此功能的支持之前,您将需要使用编译器(最受欢迎的是babel)才能使用导入。 为了快速尝试转译,babel提供了基于Web的REPL。这段演示了您

  • 问题内容: 我不明白怎么了。节点v5.6.0 NPM v3.10.6 代码: 错误: 问题答案: 更新3: 从Node 13开始 ,您可以使用.mjs扩展名,也可以在package.json中设置“ type”:“ module”。你 并不 需要使用标志。 更新2: 从Node 12开始 ,您可以使用扩展名,也可以在package.json中进行设置。并且您需要运行带有标志的节点。 更新: 在节点

  • 我开始了一个新的反应项目,我想使用笑话作为测试平台。尽管有文档、博客和许多其他资源,如stackoverflow,我总是有一个“意外令牌导入”错误,可能与巴贝尔问题有关,但我的conf似乎还不错。欢迎任何帮助。 我的Jest配置文件(在package.json中)。My package.json具有类似babel jest、babel-preset-es2015、babel preset reac