gpt4 book ai didi

javascript - 包未在项目中正确导入

转载 作者:行者123 更新时间:2023-12-01 17:17:35 26 4
gpt4 key购买 nike

我最近创建了一个 Typescript 包,我想在将其发布到 NPM 之前在应用中对其进行测试。

入口文件 (index.ts) 看起来像这样 =>

import Builder from './core/builder';

export default Builder;

基本上,有很多文件,但我希望用户只使用我在索引中导出的“静态”类 Builder。所以用户只需执行 import Builder from 'builderts'

我的package.json文件夹如下

  "name": "builderts",
"main": "./dist/builderts.js",
"types": "./dist/builderts.d.ts",
"files": [
"dist/*"
],
"scripts": {
"preversion" : "npm run lint",
"prepare": "npm run build",
"prepublishOnly": "npm test && npm run lint",
"version" : "npm run format && git add -A src",
"postversion" : "git push && git push --tags",
"build": "tsc",
"watch": "tsc-watch",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"lint": "eslint tsconfig.json"
},

和我的tsconfig.json

{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "amd", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outFile":"./dist/builderts.js", /* Concatenate and emit output to single file. */
"outDir": "dist", /* Redirect output structure to the directory. */
"strict": true, /* Enable all strict type-checking options. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}

我面临的问题是,我尝试创建一个小型测试项目并使用

安装模块

npm 安装绝对/路径

它安装正确,但是当我尝试使用

导入包时

从'builderts'导入生成器;

我没有自动完成功能,我有一个错误说

File 'D:/User/Local/gitRepo/builderts/dist/builderts.d.ts' is not a module.

我不太明白我在这个过程中遗漏了什么。

最佳答案

在我看来,您需要更新 package.json 中的 types

你上面说文件名是index.ts,你有没有考虑更新package.json? :

{ 
...
"types": "./dist/index.ts",
...
}

关于javascript - 包未在项目中正确导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61378567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com