我已经构建了一个 Kotlin 多平台库并将其发布到 bintray。当尝试将它包含在示例 node.js 应用程序中时,它将能够找到依赖项,但编译将失败,并出现以下异常:
internal/modules/cjs/loader.js:800
throw err;
^
Error: Cannot find module 'storyblok-mp-sdk'
Require stack:
- /Users/.../storyblok-mp-SDK/nodejsapp/node/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
at Function.Module._load (internal/modules/cjs/loader.js:690:27)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at /Users/.../storyblok-mp-SDK/nodejsapp/node/index.js:5:48
at Object.<anonymous> (/Users/.../storyblok-mp-SDK/nodejsapp/node/index.js:18:2)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/.../storyblok-mp-SDK/nodejsapp/node/index.js'
]
}
看起来好像依赖项本身已正确下载并在编译期间进行了处理(因为它在构建目录中以 js 文件的形式提供)。但即使事情似乎已经到位,问题仍然存在。
整个示例项目在此分支发布:https ://github.com/mikepenz/storyblok-mp-SDK/tree/feature/nodejssample/nodejsapp
为了简单起见,我尽量保持build.gradle
简单
buildscript {
...
}
apply plugin: 'kotlin2js'
apply plugin: 'kotlin-dce-js'
repositories { ... }
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
implementation "com.mikepenz:storyblok-mp-sdk-js:0.0.3"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.3.3"
}
compileKotlin2Js.kotlinOptions {
moduleKind = "umd"
outputFile = "node/index.js"
}
task npmInit(type: Exec) {
commandLine "npm", "init", "-y"
}
task npmInstall(type: Exec) {
commandLine "npm", "install", "kotlin", "kotlinx-coroutines-core", "express", "--save"
}
task npmRun(type: Exec) {
commandLine "node", "node/index.js"
}
npmRun.dependsOn(build)
我能找到的所有资源对于如何正确配置项目似乎都非常模糊。删除依赖项本身并且不从依赖项中引用一个类似乎有效。因此,据我所知,一般项目设置应该没问题