【问题标题】:Cannot find module ".package.json" when requiring a module in Angular 2?在 Angular 2 中需要模块时找不到模块“.package.json”?
【发布时间】:2016-12-16 14:40:51
【问题描述】:

我正在尝试将节点模块导入到我的 Angular 2 应用程序中:

const node-module = require('some-module');

在控制台我总是得到错误

ERROR in ./~/some-module/package.json
Module parse failed: /home/tom/Projects/project/node_modules/some-module     /package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:9)

我正在使用 Webpack 来捆绑模块,但我真的不明白为什么会失败。 还有其他方法可以在 Angular 2 应用程序中导入节点模块吗?

【问题讨论】:

    标签: angular webpack node-modules


    【解决方案1】:

    我认为它甚至会尝试加载它不应该加载的 package.json。如果可以的话,你可以试试 json-loader,但是它不应该编译 package.json,package.json 应该从捆绑中排除。

    【讨论】:

    • 不,模块中的package.json文件是有效的,我按照你说的用网上各种工具查过了。一定是其他问题。
    • 当你这样做时 const node-module = require('some-module');它需要一切。您正在使用 webpack 您可以简单地使用 import {yourModule} from "some-module"
    • Please take a look 也试试这个 {test: /\.json$/, loader: "json-loader"}
    • 感谢您的回答 MrJSingh,在我添加 {test: /\.json$/, loader: "json-loader"} 后没有错误,但我也会看看angular2-webpack-种子。再次感谢!我会尽快让你的答案正确。
    猜你喜欢
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    相关资源
    最近更新 更多