我有一个包含一些打字稿类的程序包,这些程序包被编译为.js文件,并带有d.ts文件作为类型。
到目前为止,我一直在npm link my-models-package
中本地使用它。现在,我为该程序包创建了一个github存储库,并使用npm i git+https://git@github.com/Username/my-models-package.git
安装它。但是突然它不再起作用了。我收到错误消息:
(function (exports, require, module, __filename, __dirname) { import { ContentItem } from './content-item.model';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Module._extensions..js (module.js:664:10)
at Object.require.extensions.(anonymous function) [as .js] (node_modules/ts-node/src/index.ts:431:14)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
当我恢复为npm link my-models-package
时,它仍然可以正常工作。另外,当我在另一个有角度的项目中测试程序包时,它也可以正常工作。因此,这肯定是一个麻烦问题。
这是我的模特包中的tsconfig.json
{
"compilerOptions": {
"target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"strict": false, /* Enable all strict type-checking options. */
"strictPropertyInitialization": false, /* Enable strict checking of property initialization in classes. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
}
}
这是我的nuxt项目中的tsconfig.json
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": [
"esnext",
"esnext.asynciterable",
"dom"
],
"esModuleInterop": true,
"experimentalDecorators": true,
"allowJs": true,
"sourceMap": true,
"strict": true,
"noImplicitAny": false,
"noEmit": true,
"baseUrl": ".",
"paths": {
"~/*": [
"./*"
],
"@/*": [
"./*"
]
},
"types": [
"@types/node",
"@nuxt/vue-app"
]
}
}