简体   繁体   English

更改没有index.js的NPM入口点

[英]Change NPM entry point without an index.js

I recently published a private module to NPM which has some common code we use across a few of our services. 我最近向NPM发布了一个私有模块,其中包含一些我们在一些服务中使用的通用代码。

The code is written in ES6 and so we need to transpile it using babel before publishing to NPM. 该代码是用ES6编写的,因此在发布到NPM之前,我们需要使用babel对其进行编译。 I've got a prepublish script which transpiles src in to lib . 我有一个预发布脚本,可将srclib

There's no index.js file in this module since it's just some common code. 该模块中没有index.js文件,因为它只是一些通用代码。

The problem I'm having is that when I install the module from NPM, using require('@ourorg/ourmodule/somecode') doesn't work (module can't be found). 我遇到的问题是,当我从NPM安装模块时,使用require('@ourorg/ourmodule/somecode')不起作用(找不到模块)。 I instead have to use require('@ourorg/ourmodule/lib/somecode') . 我不得不使用require('@ourorg/ourmodule/lib/somecode')

I've tried changing the main field in package.json to many variations of lib , but it doesn't seem to work unless I include an index.js file, in which case require('@ourorg/ourmodule') returns whatever is exported there. 我试图将package.jsonmain字段更改为lib许多变体,但是除非我包含index.js文件,否则它似乎无法工作,在这种情况下require('@ourorg/ourmodule')返回什么出口到那里。 One workaround I can see would be to export all the common code in an index.js file, but this isn't maintainable at all. 我可以看到的一种解决方法是将所有通用代码导出到index.js文件中,但这根本无法维护。

The main field in package.json follows the same rules as normal Node imports - either it should point at a single file in particular, or it can point at a directory that has an index.js in it. package.jsonmain字段遵循与普通Node导入相同的规则-它应该特别指向单个文件,或者可以指向其中包含index.js的目录。

As far as I know, there is no way to make importing your package simply be an alias for a directory. 据我所知,没有办法使导入包仅仅是目录的别名。 If there was, what would require("@ourorg/ourmodule") return? 如果存在, require("@ourorg/ourmodule")返回什么?

If it's absolutely driving you crazy having to type lib every time you import something, maybe you could add a step to your build process that autogenerates an index.js that re-exports everything at the root? 如果这绝对使您发疯,每次导入内容时都必须输入lib ,那么也许可以在构建过程中添加一个步骤,以自动生成index.js并重新导出根目录中的所有内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改/添加 index.js 的 create-react-app 入口点或位置 - Change/add create-react-app entry point or location of index.js 即使我在 package.json 中将主入口点更改为 app.js,为什么还需要 index.js? - Why index.js is necessary even if I change my main entry point to app.js in package.json? 使用没有'entry' index.js 的 webpack 插件 - Use webpack plugins without 'entry' index.js 反应入口点:Index.html vs index.js? 节点代码在哪里? - React Entry Point: Index.html vs index.js? Where is the node code? 无法识别入口点:无法解析“文件”或“目录”“ ./src/index.js” - Fail on recognising entry point: Can not resolve 'file' or 'directory' './src/index.js' 这将如何工作...... npm 与 index.js 和 index.html? - How this will work... npm with index.js and index.html? 如何设置 Babel,然后在命令提示符下点击“npm run dev”,而不会出现“ERROR in./src/js/index.js”? - How do I set up Babel, then hit “npm run dev” in the command prompt without getting the “ERROR in ./src/js/index.js”? 将 /username 指向 /[user]/[tab].js 而不是 nextjs 中的 [user]/index.js - point /username to /[user]/[tab].js and not [user]/index.js in nextjs npm 仅在 index.js 中进行更改时更新 - npm is only updating when changes are made in index.js 使用babel转译的浏览器的npm软件包的index.js - index.js for npm package for browser transpiled with babel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM